Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Improve error handling by using ErrorWithCause #581

Merged
merged 32 commits into from
Mar 6, 2025

Conversation

ZhongpinWang
Copy link
Contributor

@ZhongpinWang ZhongpinWang commented Mar 4, 2025

Context

Closes SAP/ai-sdk-js-backlog#235.

What this PR does and why it is needed

This PR introduces ErrorWithCause for all request execution using executeRequest() from core.

The most useful information is contained in error.stack which will be by default printed out if not caught using try-catch.

I also have to make changes to sample-code since accessing response data is now a layer deeper wrapper by cause.

Also cleaned up sample-code a bit.

Docs are added for general error handling in case of a failing request.

@ZhongpinWang ZhongpinWang changed the title chore: Improve error handling by using error with cause chore: Improve error handling by using ErrorWithCause Mar 4, 2025
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some docs were missing.

Also mask grounding input should have been placed as part of the orchestration group.

@@ -196,11 +198,12 @@ export async function orchestrationInputFiltering(): Promise<void> {
});
throw new Error('Input was not filtered as expected.');
} catch (error: any) {
if (error.response?.status === 400) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to make these changes due to the introduction of ErrorWithCause

}
});

/* Foundation Models (Azure OpenAI) */
app.get('/azure-openai/chat-completion', async (req, res) => {
try {
const response = await chatCompletion();
res.send(response.getContent());
res.header('Content-Type', 'text/plain').send(response.getContent());
Copy link
Contributor Author

@ZhongpinWang ZhongpinWang Mar 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed because by default for string response, res.send treats it like html. So line breakers \n won't work. Instead, we should have used <br />.

@ZhongpinWang ZhongpinWang marked this pull request as ready for review March 4, 2025 16:06
Copy link
Member

@tomfrenken tomfrenken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM, but the ## Error Handling section needs a bit more detail, so that users do not have to jump to the cloud SDK docs to understand how to use it properly.

Copy link
Contributor

@jjtang1985 jjtang1985 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would accept the breaking change, and treat that as a bug fix, as I did not know the response body was not included.
Please have a look at the suggestion for the release notes.

Copy link
Member

@tomfrenken tomfrenken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tomfrenken tomfrenken merged commit bfed500 into main Mar 6, 2025
11 checks passed
@tomfrenken tomfrenken deleted the improve-error-handling-by-using-error-with-cause branch March 6, 2025 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants