Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6d96696

Browse files
authored
refactor: rename errors to validations (#2105)
* Update validation error unpacking * Rename validations on backend * Format
1 parent 3616c62 commit 6d96696

File tree

6 files changed

+17
-10
lines changed

6 files changed

+17
-10
lines changed

coderd/httpapi/httpapi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ type Response struct {
6767
// Validations are form field-specific friendly error messages. They will be
6868
// shown on a form field in the UI. These can also be used to add additional
6969
// context if there is a set of errors in the primary 'Message'.
70-
Validations []Error `json:"errors,omitempty"`
70+
Validations []Error `json:"validations,omitempty"`
7171
}
7272

7373
// Error represents a scoped error to a user input.

site/src/api/errors.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe("mapApiErrorToFieldErrors", () => {
2929
expect(
3030
mapApiErrorToFieldErrors({
3131
message: "Invalid entry",
32-
errors: [{ detail: "Username is already in use", field: "username" }],
32+
validations: [{ detail: "Username is already in use", field: "username" }],
3333
}),
3434
).toEqual({
3535
username: "Username is already in use",

site/src/api/errors.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export type FieldErrors = Record<FieldError["field"], FieldError["detail"]>
1515

1616
export interface ApiErrorResponse {
1717
message: string
18-
errors?: FieldError[]
18+
detail?: string
19+
validations?: FieldError[]
1920
}
2021

2122
export type ApiError = AxiosError<ApiErrorResponse> & { response: AxiosResponse<ApiErrorResponse> }
@@ -39,13 +40,13 @@ export const isApiError = (err: any): err is ApiError => {
3940
* @param error ApiError
4041
* @returns true if the ApiError contains error messages for specific form fields.
4142
*/
42-
export const hasApiFieldErrors = (error: ApiError): boolean => Array.isArray(error.response.data.errors)
43+
export const hasApiFieldErrors = (error: ApiError): boolean => Array.isArray(error.response.data.validations)
4344

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

47-
if (apiErrorResponse.errors) {
48-
for (const error of apiErrorResponse.errors) {
48+
if (apiErrorResponse.validations) {
49+
for (const error of apiErrorResponse.validations) {
4950
result[error.field] = error.detail || Language.errorsByCode.defaultErrorCode
5051
}
5152
}

site/src/pages/UserSettingsPage/AccountPage/AccountPage.test.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ describe("AccountPage", () => {
5454
jest.spyOn(API, "updateProfile").mockRejectedValueOnce({
5555
isAxiosError: true,
5656
response: {
57-
data: { message: "Invalid profile", errors: [{ detail: "Username is already in use", field: "username" }] },
57+
data: {
58+
message: "Invalid profile",
59+
validations: [{ detail: "Username is already in use", field: "username" }],
60+
},
5861
},
5962
})
6063

site/src/pages/UserSettingsPage/SecurityPage/SecurityPage.test.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ describe("SecurityPage", () => {
4949
jest.spyOn(API, "updateUserPassword").mockRejectedValueOnce({
5050
isAxiosError: true,
5151
response: {
52-
data: { message: "Incorrect password.", errors: [{ detail: "Incorrect password.", field: "old_password" }] },
52+
data: {
53+
message: "Incorrect password.",
54+
validations: [{ detail: "Incorrect password.", field: "old_password" }],
55+
},
5356
},
5457
})
5558

@@ -68,7 +71,7 @@ describe("SecurityPage", () => {
6871
jest.spyOn(API, "updateUserPassword").mockRejectedValueOnce({
6972
isAxiosError: true,
7073
response: {
71-
data: { message: "Invalid password.", errors: [{ detail: "Invalid password.", field: "password" }] },
74+
data: { message: "Invalid password.", validations: [{ detail: "Invalid password.", field: "password" }] },
7275
},
7376
})
7477

site/src/pages/UsersPage/CreateUserPage/CreateUserPage.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe("Create User Page", () => {
5858
ctx.status(400),
5959
ctx.json({
6060
message: "invalid field",
61-
errors: [
61+
validations: [
6262
{
6363
detail: fieldErrorMessage,
6464
field: "username",

0 commit comments

Comments
 (0)