I am trying to capture an error thrown from Apex in my LWC promise chain, with an end goal to publish a Slack message with the error details. The slack message template contains the following needed fields:
- Status Code
- Error Message
- Error Stack Trace When using catching the exception and rethrowing it as AuraHandledException, the stack trace is only intermittently populated and the error structure changes depending on the error type.
Here is console log example using AuraHandledException with stack trace being displayed:
{
"status":500,
"body":{
"exceptionType":"System.LimitException",
"isUserDefinedException":false,
"message":"Too many DML statements: 1",
"stackTrace":"Class.MyController.myMethod: line 32, column 1"
},
"headers":{},
"ok":false,
"statusText":"Server Error",
"errorType":"fetchResponse"
}
Here is another example using AuraHandledException, but the stack trace is not displayed:
{
"status":500,
"body":{
"message":"Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [My Field]: [My Field]"
},
"headers":{},
"ok":false,
"statusText":"Server Error",
"errorType":"fetchResponse"
}
Here is another example without using try/catch in apex (only capturing in LWC promise). This one displays the stack trace:
{
"status":500,
"body":{
"exceptionType":"System.LimitException",
"isUserDefinedException":false,
"message":"Too many DML statements: 1",
"stackTrace":"Class.MyController.myMethod: line 31, column 1"
},
"headers":{},
"ok":false,
"statusText":"Server Error",
"errorType":"fetchResponse"
}
Here is another example without try/catch in apex, that does not display the stack trace and the whole error structure changes:
{
"fieldErrors":{
"Company":[{
"statusCode":"REQUIRED_FIELD_MISSING",
"message":"Required fields are missing: [Last Name, Company]"
}]
},
"pageErrors":[{
"statusCode":"REQUIRED_FIELD_MISSING",
"message":"Required fields are missing: [Last Name, Company]"
}],
"index":null,
"duplicateResults":[]},
"headers":{},
"ok":false,
"statusText":"Server Error",
"errorType":"fetchResponse"
}
Here is a example throwing CustomException, the error structure never changes but I cannot use CustomExceptions:
{
"status":500,
"body":{
"exceptionType":"System.LimitException",
"isUserDefinedException":false,
"message":"Too many DML statements: 1",
"stackTrace":"Class.MyController.myMethod: line 33, column 1"
},
"headers":{},
"ok":false,
"statusText":"Server Error",
"errorType":"fetchResponse"
}
{
"status":500,
"body":{
"exceptionType":"MyController.MyCustomException",
"isUserDefinedException":true,
"message":"Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Last Name, Company]: [Last Name, Company]",
"stackTrace":"Class.MyController.myMethod: line 334, column 1"
},
"headers":{},
"ok":false,
"statusText":"Server Error",
"errorType":"fetchResponse"
}
The problem is that I cannot use CustomExceptions, so how can I have the same error structure for different error types, using AuraHandledException? Really appreciate any help.