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: Show error message from backend on create existing user#1964

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 2 commits intomainfromcreate-error/presleyp/1889
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
8 changes: 8 additions & 0 deletionssite/src/api/errors.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,6 +33,14 @@ export const isApiError = (err: any): err is ApiError => {
return false
}

/**
* ApiErrors contain useful error messages in their response body. They contain an overall message
* and may also contain errors for specific form fields.
* @param error ApiError
* @returns true if the ApiError contains error messages for specific form fields.
*/
export const hasApiFieldErrors = (error: ApiError): boolean => Array.isArray(error.response.data.errors)

export const mapApiErrorToFieldErrors = (apiErrorResponse: ApiErrorResponse): FieldErrors => {
const result: FieldErrors = {}

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,7 +7,7 @@ import { Language as FooterLanguage } from "../../../components/FormFooter/FormF
import { history, render } from "../../../testHelpers/renderHelpers"
import { server } from "../../../testHelpers/server"
import { Language as UserLanguage } from "../../../xServices/users/usersXService"
import { CreateUserPage, Language } from "./CreateUserPage"
import { CreateUserPage } from "./CreateUserPage"

const fillForm = async ({
username = "someuser",
Expand DownExpand Up@@ -46,7 +46,7 @@ describe("Create User Page", () => {
})
render(<CreateUserPage />)
await fillForm({})
const errorMessage = await screen.findByText(Language.unknownError)
const errorMessage = await screen.findByText(UserLanguage.createUserError)
expect(errorMessage).toBeDefined()
})

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,11 +15,11 @@ export const CreateUserPage: React.FC = () => {
const xServices = useContext(XServiceContext)
const myOrgId = useSelector(xServices.authXService, selectOrgId)
const [usersState, usersSend] = useActor(xServices.usersXService)
const {createUserError, createUserFormErrors } = usersState.context
const {createUserErrorMessage, createUserFormErrors } = usersState.context
const navigate = useNavigate()
// There is no field for organization id in Community Edition, so handle its field error like a generic error
const genericError =
createUserError || createUserFormErrors?.organization_id || !myOrgId ? Language.unknownError : undefined
createUserErrorMessage || createUserFormErrors?.organization_id ||(!myOrgId ? Language.unknownError : undefined)

return (
<Margins>
Expand Down
24 changes: 16 additions & 8 deletionssite/src/xServices/users/usersXService.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
import { assign, createMachine } from "xstate"
import * as API from "../../api/api"
import { ApiError, FieldErrors, getErrorMessage, isApiError, mapApiErrorToFieldErrors } from "../../api/errors"
import {
ApiError,
FieldErrors,
getErrorMessage,
hasApiFieldErrors,
isApiError,
mapApiErrorToFieldErrors,
} from "../../api/errors"
import * as TypesGen from "../../api/typesGenerated"
import { displayError, displaySuccess } from "../../components/GlobalSnackbar/utils"
import { generateRandomString } from "../../util/random"

export const Language = {
createUserSuccess: "Successfully created user.",
createUserError: "Error on creating the user.",
suspendUserSuccess: "Successfully suspended the user.",
suspendUserError: "Error onsuspend the user.",
suspendUserError: "Error onsuspending the user.",
resetUserPasswordSuccess: "Successfully updated the user password.",
resetUserPasswordError: "Error onreset the user password.",
resetUserPasswordError: "Error onresetting the user password.",
updateUserRolesSuccess: "Successfully updated the user roles.",
updateUserRolesError: "Error onupdate the user roles.",
updateUserRolesError: "Error onupdating the user roles.",
}

export interface UsersContext {
// Get users
users?: TypesGen.User[]
getUsersError?: Error | unknown
createUserError?:Error | unknown
createUserErrorMessage?:string
createUserFormErrors?: FieldErrors
// Suspend user
userIdToSuspend?: TypesGen.User["id"]
Expand DownExpand Up@@ -122,7 +130,7 @@ export const usersMachine = createMachine(
onError: [
{
target: "idle",
cond: "isFormError",
cond: "hasFieldErrors",
actions: ["assignCreateUserFormErrors"],
},
{
Expand DownExpand Up@@ -235,7 +243,7 @@ export const usersMachine = createMachine(
},
},
guards: {
isFormError: (_, event) => isApiError(event.data),
hasFieldErrors: (_, event) => isApiError(event.data) && hasApiFieldErrors(event.data),
},
actions: {
assignUsers: assign({
Expand All@@ -258,7 +266,7 @@ export const usersMachine = createMachine(
getUsersError: undefined,
})),
assignCreateUserError: assign({
createUserError: (_, event) => event.data,
createUserErrorMessage: (_, event) =>getErrorMessage(event.data, Language.createUserError),
}),
assignCreateUserFormErrors: assign({
// the guard ensures it is ApiError
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp