Skip to content

Commit 845f448

Browse files
committed
doc: improve error handling doc
1 parent 5b623e7 commit 845f448

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

README.md

+23-26
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Setup your SAP AI Core instance with SAP Cloud SDK for AI.
1919
- [@sap-ai-sdk/orchestration](#sap-ai-sdkorchestration)
2020
- [SAP Cloud SDK for AI Sample Project](#sap-cloud-sdk-for-ai-sample-project)
2121
- [Error Handling](#error-handling)
22+
- [Accessing Error Information](#accessing-error-information)
2223
- [Local Testing](#local-testing)
2324
- [Support, Feedback, Contribution](#support-feedback-contribution)
2425
- [Security / Disclosure](#security--disclosure)
@@ -99,41 +100,37 @@ The [project README](https://github.com/SAP/ai-sdk-js/blob/main/sample-code/READ
99100
A common error scenario is `Request failed with status code STATUS_CODE` coming from `AxiosError`.
100101
In this case, SAP Cloud SDK for AI uses [`ErrorWithCause`](https://sap.github.io/cloud-sdk/docs/js/features/error-handling) to provide more detailed error information.
101102

102-
The following example shows how to access useful information from a nested `ErrorWithCause`.
103+
### Accessing Error Information
104+
105+
For example, for the following nested `ErrorWithCause`
103106

104107
```ts
105-
try {
106-
... // execute request
107-
} catch (e) {
108-
/* Print error messages from different layers */
109-
// Example: "Error: Failed to fetch the deployments."
110-
console.error(e.message);
111-
// Example: "Cause: executeRequest() function failed."
112-
console.error(e.cause?.message);
113-
// Example: "Root cause: Request failed with status code 404"
114-
console.error(e.rootCause?.message);
115-
116-
/* Print error response from the server */
117-
console.error(e.cause?.response?.data);
118-
/* Print error stack */
119-
console.error(e.stack);
120-
}
108+
const rootCause = new Error('The root cause is a bug!');
109+
const lowerLevelErrorWithCause = new ErrorWithCause('Failed to call function foo().', rootCause);
110+
const upperLevelErrorWithCause = new ErrorWithCause('Process crashed.', lowerLevelErrorWithCause);
111+
throw upperLevelErrorWithCause;
121112
```
122113

123-
The error stack consists of the error message, call stack, the causes of the error, and error response from the server if exists.
114+
The error stack will look like this:
124115

125116
```txt
126-
ErrorWithCause: Failed to ....
127-
at ... (some-file.ts:11:12)
128-
at ... (...)
117+
ErrorWithCause: Process crashed.
118+
at ...
119+
Caused by:
120+
ErrorWithCause: Failed to call function foo().
121+
at ...
129122
Caused by:
130-
HTTP Response: Request failed with status code 400
131-
{
132-
"error": "Bad Request",
133-
"message": "Invalid request body"
134-
}
123+
Error: The root cause is a bug!
124+
at ...
135125
```
136126

127+
- `error.stack` will contain the above stack trace.
128+
- `error.message` will be `Process crashed.`.
129+
- `error.cause.message` will be `Failed to call function foo().`.
130+
- `error.rootCause.message` will be `The root cause is a bug!`.
131+
132+
In case of `AxiosError`, the response data will be part of the error stack and can be accessed via `error.cause.response.data`.
133+
137134
## Local Testing
138135

139136
To test SAP Cloud SDK for AI features locally during application development, follow these steps:

0 commit comments

Comments
 (0)