Error Handling
A common error scenario is Request failed with status code STATUS_CODE coming from AxiosError.
In this case, SAP Cloud SDK for AI uses ErrorWithCause to provide more detailed error information.
Accessing Error Information
For example, for the following nested ErrorWithCause
const rootCause = new Error('The root cause is a bug!');
const lowerLevelErrorWithCause = new ErrorWithCause(
'Failed to call function foo().',
rootCause
);
const upperLevelErrorWithCause = new ErrorWithCause(
'Process crashed.',
lowerLevelErrorWithCause
);
throw upperLevelErrorWithCause;
The error stack will look like this:
ErrorWithCause: Process crashed.
at ...
Caused by:
ErrorWithCause: Failed to call function foo().
at ...
Caused by:
Error: The root cause is a bug!
at ...
error.stackwill contain the above stack trace.error.messagewill beProcess crashed..error.cause.messagewill beFailed to call function foo()..error.rootCause.messagewill beThe root cause is a bug!.
In case of AxiosError, the response data will be part of the error stack and can be accessed via error.cause.response.data.