Skip to content

Commit 75ca576

Browse files
JalinaHmttrbrts
andauthored
test: add unit test for extracting string error from prefixed payload (#718)
* test: add unit test for extracting string error from prefixed payload * DCO Remediation Commit for Jalina Hirushan <139668262+JalinaH@users.noreply.github.com> I, Jalina Hirushan <139668262+JalinaH@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 6767681 Signed-off-by: Jalina Hirushan <139668262+JalinaH@users.noreply.github.com> --------- Signed-off-by: Jalina Hirushan <139668262+JalinaH@users.noreply.github.com> Co-authored-by: Matt Roberts <7544022+mttrbrts@users.noreply.github.com>
1 parent 9d2b8d6 commit 75ca576

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/tests/utils/helpers/errorUtils.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ describe("extractErrorMessage", () => {
5353
expect(result).toBe("invalid x-api-key");
5454
});
5555

56+
it("should extract string error from prefixed payload", () => {
57+
const error = new Error(
58+
'AuthenticationError: 401 {"error":"Simple error string"}'
59+
);
60+
61+
const result = extractErrorMessage(error);
62+
expect(result).toBe("Simple error string");
63+
});
64+
5665
it("should extract message from Anthropic pure JSON error", () => {
5766
const error = new Error(JSON.stringify({
5867
type: "error",

src/utils/helpers/errorUtils.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ export const extractErrorMessage = (error: Error | unknown): string => {
2626
}
2727
}
2828
}
29-
// Handle error as string
30-
if (typeof parsed.error === 'string') {
31-
return parsed.error;
32-
}
29+
}
30+
// Handle error as string
31+
if (typeof parsed.error === 'string') {
32+
return parsed.error;
3333
}
3434
}
3535

@@ -86,4 +86,4 @@ export const extractErrorMessage = (error: Error | unknown): string => {
8686
}
8787

8888
return errorMessage;
89-
};
89+
};

0 commit comments

Comments
 (0)