Skip to content

fix: display error message from backend on Users page #1938

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 1 commit into from
Jun 1, 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
9 changes: 9 additions & 0 deletions site/src/api/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,12 @@ export const mapApiErrorToFieldErrors = (apiErrorResponse: ApiErrorResponse): Fi

return result
}

/**
*
* @param error
* @param defaultMessage
* @returns error's message if ApiError or Error, else defaultMessage
*/
export const getErrorMessage = (error: Error | ApiError | unknown, defaultMessage: string): string =>
isApiError(error) ? error.response.data.message : error instanceof Error ? error.message : defaultMessage
20 changes: 20 additions & 0 deletions site/src/pages/UsersPage/UsersPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,26 @@ describe("Users Page", () => {
expect(API.updateUserRoles).toBeCalledTimes(1)
expect(API.updateUserRoles).toBeCalledWith([...currentRoles, MockAuditorRole.name], MockUser.id)
})
it("shows an error from the backend", async () => {
render(
<>
<UsersPage />
<GlobalSnackbar />
</>,
)

server.use(
rest.put(`/api/v2/users/${MockUser.id}/roles`, (req, res, ctx) => {
return res(ctx.status(401), ctx.json({ message: "message from the backend" }))
}),
)

// eslint-disable-next-line @typescript-eslint/no-empty-function
await updateUserRole(() => {}, MockAuditorRole)

// Check if the error message is displayed
await screen.findByText("message from the backend")
})
})
})
})
17 changes: 10 additions & 7 deletions site/src/xServices/users/usersXService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assign, createMachine } from "xstate"
import * as API from "../../api/api"
import { ApiError, FieldErrors, isApiError, mapApiErrorToFieldErrors } from "../../api/errors"
import { ApiError, FieldErrors, getErrorMessage, isApiError, mapApiErrorToFieldErrors } from "../../api/errors"
import * as TypesGen from "../../api/typesGenerated"
import { displayError, displaySuccess } from "../../components/GlobalSnackbar/utils"
import { generateRandomString } from "../../util/random"
Expand Down Expand Up @@ -292,17 +292,20 @@ export const usersMachine = createMachine(
displaySuspendSuccess: () => {
displaySuccess(Language.suspendUserSuccess)
},
displaySuspendedErrorMessage: () => {
displayError(Language.suspendUserError)
displaySuspendedErrorMessage: (context) => {
const message = getErrorMessage(context.suspendUserError, Language.suspendUserError)
displayError(message)
},
displayResetPasswordSuccess: () => {
displaySuccess(Language.resetUserPasswordSuccess)
},
displayResetPasswordErrorMessage: () => {
displayError(Language.resetUserPasswordError)
displayResetPasswordErrorMessage: (context) => {
const message = getErrorMessage(context.resetUserPasswordError, Language.resetUserPasswordError)
displayError(message)
},
displayUpdateRolesErrorMessage: () => {
displayError(Language.updateUserRolesError)
displayUpdateRolesErrorMessage: (context) => {
const message = getErrorMessage(context.updateUserRolesError, Language.updateUserRolesError)
displayError(message)
},
generateRandomPassword: assign({
newUserPassword: (_) => generateRandomString(12),
Expand Down