Skip to content
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

refactor: rename errors to validations #2105

Merged
merged 4 commits into from
Jun 7, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion coderd/httpapi/httpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type Response struct {
// Validations are form field-specific friendly error messages. They will be
// shown on a form field in the UI. These can also be used to add additional
// context if there is a set of errors in the primary 'Message'.
Validations []Error `json:"errors,omitempty"`
Validations []Error `json:"validations,omitempty"`
}

// Error represents a scoped error to a user input.
Expand Down
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,10 @@ 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,10 @@ 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 +71,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