Skip to main content

Error Handling

FIYGE will return a status 200 OK, regardless of if the request was successful. Inside the request body is a key named errors. If errors is empty the request was successful. The items in errors may be a string or an object.

{
"errors": {
"users.user_name": [
"User Name should not be empty"
],
"users.user_password": [
"User Password should not be empty"
]
}
}

In this case errors is an object, with each key the field which contains the error.

{
"errors": ["Invalid Password"]
}

In this case errors is an array of strings, containing an error message.

Here is a code snipper to generically format the error variable into a string

function formatError(errors: unknown): string {
let errorMessage;
if (Array.isArray(errors)) {
errorMessage = errors.join("\n");
} else {
errorMessage = Object.values(errors).reduce(
(_, message: string[]) => message.join("\n"),
"",
);
}
return errorMessage as string;
}