Skip to content

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
}

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

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
Contributor

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
Contributor

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

SkinnnyJay pushed a commit to SkinnnyJay/openai-node that referenced this pull request Sep 13, 2025
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