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

fix(client): correctly handle errors during streaming #379

Merged
merged 1 commit into from
Oct 16, 2023

Conversation

stainless-bot
Copy link
Collaborator

No description provided.

@stainless-bot stainless-bot merged commit 9ced580 into next Oct 16, 2023
@stainless-bot stainless-bot deleted the fix-client-correctly-handle-erro branch October 16, 2023 10:13
@stainless-bot stainless-bot mentioned this pull request Oct 16, 2023
} catch (e) {
console.error(`Could not parse message into JSON:`, sse.data);
console.error(`From chunk:`, sse.raw);
throw e;
}

if (data && data.error) {
throw new APIError(undefined, data.error, undefined, undefined);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like we're not setting the APIError.message here, is that intentional?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, I was under the impression that the API error data will include a message property. That property will then be used by the APIError class if a message isn't explicitly passed in the constructor. If the message property isn't set, then we'll default to stringifying the error object to raw JSON.

Is that assumption correct?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The assumption is correct, but I don't see code in APIError to assign message from the error object:

openai-node/src/error.ts

Lines 16 to 41 in 6484039

constructor(
status: number | undefined,
error: Object | undefined,
message: string | undefined,
headers: Headers | undefined,
) {
super(`${status} ${APIError.makeMessage(error, message)}`);
this.status = status;
this.headers = headers;
const data = error as Record<string, any>;
this.error = data;
this.code = data?.['code'];
this.param = data?.['param'];
this.type = data?.['type'];
}
private static makeMessage(error: any, message: string | undefined) {
return (
error?.message ?
typeof error.message === 'string' ? error.message
: JSON.stringify(error.message)
: error ? JSON.stringify(error)
: message || 'status code (no body)'
);
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's this line

typeof error.message === 'string' ? error.message

The error argument there is the data that the API responded with

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah I see, thanks!

When status is undefined, should we skip the space in the error message here?

super(`${status} ${APIError.makeMessage(error, message)}`);

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah we can probably clean that up; I'll file a ticket

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.

None yet

4 participants