fleet/frontend/utilities/format_error_response/index.ts

37 lines
762 B
TypeScript
Raw Normal View History

import { get, join } from "lodash";
import { IError } from "interfaces/errors";
const formatServerErrors = (errors: IError[]) => {
if (!errors || !errors.length) {
return {};
}
const result: { [key: string]: string } = {};
errors.forEach((error) => {
const { name, reason } = error;
if (result[name]) {
result[name] = join([result[name], reason], ", ");
} else {
result[name] = reason;
}
});
return result;
};
const formatErrorResponse = (errorResponse: any) => {
const errors =
get(errorResponse, "message.errors") ||
get(errorResponse, "data.errors") ||
[];
return {
...formatServerErrors(errors),
http_status: errorResponse.status,
} as any;
};
export default formatErrorResponse;