Skip to content

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

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,28 @@ export class APIError<
readonly request_id: string | null | undefined;

constructor(status: TStatus, error: TError, message: string | undefined, headers: THeaders) {
super(`${APIError.makeMessage(status, error, message)}`);
// Always format message when constructing
const formattedMessage = APIError.makeMessage(status, error, message);
super(formattedMessage); // Use formatted message directly
this.status = status;
this.headers = headers;
this.request_id = headers?.['x-request-id'];
this.error = error;
this.error = formattedMessage;

const data = error as Record<string, any>;
this.code = data?.['code'];
this.param = data?.['param'];
this.type = data?.['type'];

}

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"])

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?

Copy link
Author

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

Copy link
Author

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

: JSON.stringify(error.message)
: error ? JSON.stringify(error)
: message;
Expand Down Expand Up @@ -100,6 +105,7 @@ export class APIError<
return new InternalServerError(status, error, message, headers);
}

// Default to a generic APIError if no specific handling
return new APIError(status, error, message, headers);
}
}
Expand Down