Skip to content

fix(site): ensure empty string error shows default message #10196

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

Merged
merged 5 commits into from
Oct 11, 2023
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
18 changes: 18 additions & 0 deletions site/src/api/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getValidationErrorMessage,
isApiError,
mapApiErrorToFieldErrors,
getErrorMessage,
} from "./errors";

describe("isApiError", () => {
Expand Down Expand Up @@ -82,4 +83,21 @@ describe("getValidationErrorMessage", () => {
),
).toEqual("");
});

it("returns default message for error that is empty string", () => {
expect(getErrorMessage("", "Something went wrong.")).toBe(
"Something went wrong.",
);
});

it("returns default message for 404 API response", () => {
expect(
getErrorMessage(
mockApiError({
message: "",
}),
"Something went wrong.",
),
).toBe("Something went wrong.");
});
});
7 changes: 5 additions & 2 deletions site/src/api/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ export const getErrorMessage = (
error: unknown,
defaultMessage: string,
): string => {
if (isApiError(error)) {
// if error is API error
// 404s result in the default message being returned
if (isApiError(error) && error.response.data.message) {
return error.response.data.message;
}
if (typeof error === "string") {
// if error is a non-empty string
if (error && typeof error === "string") {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even though this wasn't the fix for this bug, I'm keeping it because we should guard against empty strings anyway

return error;
}
return defaultMessage;
Expand Down