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

[#IOPSC-118] fixed utils #204

Merged
merged 1 commit into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 24 additions & 0 deletions utils/__tests__/apim.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { isErrorStatusCode } from "../apim";

class ErrorWithCode extends Error {
public statusCode: number;
constructor(statusCode, ...args) {
super(...args);
this.statusCode = statusCode;
}
}

describe("isErrorStatusCode", () => {
it.each`
scenario | error | statusCode | expected
${"a null error"} | ${null} | ${123} | ${false}
${"any error"} | ${new Error()} | ${123} | ${false}
${"any error with different status code"} | ${new ErrorWithCode(456)} | ${123} | ${false}
${"any error with same status code"} | ${new ErrorWithCode(123)} | ${123} | ${true}
${"any object with different status code"} | ${{ foo: "any field", statusCode: 456 }} | ${123} | ${false}
${"any object with same status code"} | ${{ foo: "any field", statusCode: 123 }} | ${123} | ${true}
`("$scenario", ({ error, statusCode, expected }) => {
const result = isErrorStatusCode(error, statusCode);
expect(result).toBe(expected);
});
});
13 changes: 10 additions & 3 deletions utils/apim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,19 @@ export const isErrorStatusCode = (
if (error === null) {
return false;
}
if (!(error instanceof RestError)) {
if (
!(
error instanceof RestError ||
(typeof error === "object" && "statusCode" in error)
)
) {
return false;
}
if (!error.statusCode) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (!(error as any).statusCode) {
return false;
}

return error.statusCode === statusCode;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (error as any).statusCode === statusCode;
};