-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix: Malformed error message formatting in makeMessage() function #1313
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
Conversation
src/error.ts
Outdated
} | ||
|
||
private static makeMessage(status: number | undefined, error: any, message: string | undefined) { | ||
const msg = | ||
error?.message ? | ||
typeof error.message === 'string' ? | ||
error.message | ||
.replace(/'/g, '"') // Convert single quotes to double quotes | ||
.replace(/\(\s*([^,]+),\s*([^,]+)\s*\)/g, '[$1, $2]') // Convert tuples to arrays (e.g., ('body', 'input') -> ["body", "input"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we extract the error message formatting logic into a separate function for better readability and maintainability?
function formatErrorMessage(error: any): string {
if (!error) return '';
if (typeof error.message === 'string') {
return error.message
.replace(/'/g, '"') // Convert single quotes to double quotes
.replace(/\(\s*([^()]+?)\s*\)/g, (_, content) => `[${content.split(/\s*,\s*/).join(', ')}]`);
// Convert tuples of any length to arrays
}
return JSON.stringify(error.message ?? error);
}
Additionally, if we have existing tests, could we add test cases for the change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i made a separate function for formatmessage inside the class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nsaiprakash90 I couldn't find any tests specifically targeting the APIError class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great, thanks!
Thanks for the PR and really sorry for the delayed review. Could you share some examples of the error messages before and after your change? I don't quite see how this will make a meaningful improvement? I'm going to close this for now but please re-open if you'd like this to get another review. |
This PR fixes an issue with the library where the message field in the error response from the API was not properly formatted as valid JSON. The
makeMessage()
function in the error.ts file was updated to ensure that error messages are formatted correctly, allowing developers to parse them without encountering JSON parsing issues.Solution:
Fixes #1140