Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
presleyp merged 1 commit intomainfromrole-error/presleyp/1932
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionssite/src/api/errors.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssite/src/pages/UsersPage/UsersPage.test.tsx
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssite/src/xServices/users/usersXService.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp