Skip to content

refactor: rename errors to validations #2105

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 4 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Update validation error unpacking
  • Loading branch information
presleyp committed Jun 6, 2022
commit ae8a5858c72192c033c3b72f088da97b409953ee
2 changes: 1 addition & 1 deletion site/src/api/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe("mapApiErrorToFieldErrors", () => {
expect(
mapApiErrorToFieldErrors({
message: "Invalid entry",
errors: [{ detail: "Username is already in use", field: "username" }],
validations: [{ detail: "Username is already in use", field: "username" }],
}),
).toEqual({
username: "Username is already in use",
Expand Down
9 changes: 5 additions & 4 deletions site/src/api/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export type FieldErrors = Record<FieldError["field"], FieldError["detail"]>

export interface ApiErrorResponse {
message: string
errors?: FieldError[]
detail?: string
validations?: FieldError[]
}

export type ApiError = AxiosError<ApiErrorResponse> & { response: AxiosResponse<ApiErrorResponse> }
Expand All @@ -39,13 +40,13 @@ export const isApiError = (err: any): err is ApiError => {
* @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 hasApiFieldErrors = (error: ApiError): boolean => Array.isArray(error.response.data.validations)

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

if (apiErrorResponse.errors) {
for (const error of apiErrorResponse.errors) {
if (apiErrorResponse.validations) {
for (const error of apiErrorResponse.validations) {
result[error.field] = error.detail || Language.errorsByCode.defaultErrorCode
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe("AccountPage", () => {
jest.spyOn(API, "updateProfile").mockRejectedValueOnce({
isAxiosError: true,
response: {
data: { message: "Invalid profile", errors: [{ detail: "Username is already in use", field: "username" }] },
data: { message: "Invalid profile", validations: [{ detail: "Username is already in use", field: "username" }] },
},
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe("SecurityPage", () => {
jest.spyOn(API, "updateUserPassword").mockRejectedValueOnce({
isAxiosError: true,
response: {
data: { message: "Incorrect password.", errors: [{ detail: "Incorrect password.", field: "old_password" }] },
data: { message: "Incorrect password.", validations: [{ detail: "Incorrect password.", field: "old_password" }] },
},
})

Expand All @@ -68,7 +68,7 @@ describe("SecurityPage", () => {
jest.spyOn(API, "updateUserPassword").mockRejectedValueOnce({
isAxiosError: true,
response: {
data: { message: "Invalid password.", errors: [{ detail: "Invalid password.", field: "password" }] },
data: { message: "Invalid password.", validations: [{ detail: "Invalid password.", field: "password" }] },
},
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe("Create User Page", () => {
ctx.status(400),
ctx.json({
message: "invalid field",
errors: [
validations: [
{
detail: fieldErrorMessage,
field: "username",
Expand Down