mirror of
https://github.com/fleetdm/fleet
synced 2026-05-23 00:49:03 +00:00
37 lines
762 B
TypeScript
37 lines
762 B
TypeScript
|
|
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;
|