Files
coder/site/src/api/errors.ts
T
Michael Smith 940afa1ab1 fix: let workspace pages download partial logs for unhealthy workspaces (#13761)
* fix: get basic fix in for  preventing download logs from blowing up UI

* fix: make sure blob units can't go out of bounds

* fix: make sure timeout is cleared on component unmount

* fix: reduce risk of shared cache state breaking useAgentLogs

* fix: allow partial downloading of logs

* fix: make sure useMemo cache is used properly

* wip: commit current progress on updated logs functionality

* docs: rewrite comment for clarity

* refactor: clean up current code

* fix: update styles for unavailable logs

* fix: resolve linter violations

* fix: update type signature of getErrorDetail

* fix: revert log/enabled logic for useAgentLogs

* fix: remove memoization from DownloadLogsDialog

* fix: update name of timeout state

* refactor: make log web sockets logic more clear

* docs: reword comment for clarity

* fix: commit current style update progress

* fix: finish style updates
2024-07-03 10:17:54 -04:00

128 lines
3.1 KiB
TypeScript

import { type AxiosError, type AxiosResponse, isAxiosError } from "axios";
const Language = {
errorsByCode: {
defaultErrorCode: "Invalid value",
},
};
export interface FieldError {
field: string;
detail: string;
}
export type FieldErrors = Record<FieldError["field"], FieldError["detail"]>;
export interface ApiErrorResponse {
message: string;
detail?: string;
validations?: FieldError[];
}
export type ApiError = AxiosError<ApiErrorResponse> & {
response: AxiosResponse<ApiErrorResponse>;
};
export const isApiError = (err: unknown): err is ApiError => {
return (
isAxiosError(err) &&
err.response !== undefined &&
isApiErrorResponse(err.response.data)
);
};
export const isApiErrorResponse = (err: unknown): err is ApiErrorResponse => {
return (
typeof err === "object" &&
err !== null &&
"message" in err &&
typeof err.message === "string" &&
(!("detail" in err) ||
err.detail === undefined ||
typeof err.detail === "string") &&
(!("validations" in err) ||
err.validations === undefined ||
Array.isArray(err.validations))
);
};
export const hasApiFieldErrors = (error: ApiError): boolean =>
Array.isArray(error.response.data.validations);
export const isApiValidationError = (error: unknown): error is ApiError => {
return isApiError(error) && hasApiFieldErrors(error);
};
export const hasError = (error: unknown) =>
error !== undefined && error !== null;
export const mapApiErrorToFieldErrors = (
apiErrorResponse: ApiErrorResponse,
): FieldErrors => {
const result: FieldErrors = {};
if (apiErrorResponse.validations) {
for (const error of apiErrorResponse.validations) {
result[error.field] =
error.detail || Language.errorsByCode.defaultErrorCode;
}
}
return result;
};
/**
*
* @param error
* @param defaultMessage
* @returns error's message if ApiError or Error, else defaultMessage
*/
export const getErrorMessage = (
error: unknown,
defaultMessage: string,
): string => {
// if error is API error
// 404s result in the default message being returned
if (isApiError(error) && error.response.data.message) {
return error.response.data.message;
}
if (isApiErrorResponse(error)) {
return error.message;
}
// if error is a non-empty string
if (error && typeof error === "string") {
return error;
}
return defaultMessage;
};
/**
*
* @param error
* @returns a combined validation error message if the error is an ApiError
* and contains validation messages for different form fields.
*/
export const getValidationErrorMessage = (error: unknown): string => {
const validationErrors =
isApiError(error) && error.response.data.validations
? error.response.data.validations
: [];
return validationErrors.map((error) => error.detail).join("\n");
};
export const getErrorDetail = (error: unknown): string | undefined => {
if (error instanceof Error) {
return "Please check the developer console for more details.";
}
if (isApiError(error)) {
return error.response.data.detail;
}
if (isApiErrorResponse(error)) {
return error.detail;
}
return undefined;
};