So I have the need to surface an exception that currently gets throw 2 classes away from an @AuraEnabled method. The basic flow of the execution cycle goes like this:
OAuth Class(REST callout to 3rd party) -> Service Class(Handles parsed HTTP Response -> Lightning Apex Controller(@AuraEnabled, returns the data).
If the OAuth Class return a 429 error code for example(rate limit), it throws a custom exception, and houses the error code/message from the web service. How can I surface that code/message from the OAuth Class up to the AuraHandledException in the @AuraEnabled class?
Some codes:
OAuth Class:
//Callout happens, response comes back
if (res.getStatusCode() > 299) {
throw new PNR_OAuthException('Failed to retrieve data. HTTP Code: ' + res.getStatusCode() + '. Message: ' + res.getStatus() + '. Response Body: ' + res.getBody());
}
Service Class:
try {
//Run method from OAuth class to get data back
} catch (Exception e) {
throw new ServiceException(e);
}
Lightning Apex Controller:
try {
//Run service class method
} catch (Exception e) {
throw new AuraHandledException('Example error message');
}