Skip to content

Instantly share code, notes, and snippets.

@maiordom
Created March 27, 2018 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maiordom/a6f36ba8e3f048473216606253417580 to your computer and use it in GitHub Desktop.
Save maiordom/a6f36ba8e3f048473216606253417580 to your computer and use it in GitHub Desktop.
const handleError = (err: AxiosError): Promise<ISignUpResponse> => {
if (!err.response) {
return Promise.reject({
error: {
code: 'UnresolvedError',
fields: [],
description: i18n.t('common:error.unresolvedError')
}
});
}
const {
error,
error_code,
error_description
} = err.response.data;
let property = null;
let description = null;
if (error_code) {
if (error.data) {
property = error.data[0].property;
description = error.data[0].description;
}
return Promise.reject({
error: {
code: property || error_code || error,
fields: ['Login', 'Password'],
description: description || error_description
} as IError
});
}
if (error.code) {
return Promise.reject({
error: {
...error,
fields: ['Login', 'Password']
} as IError
});
}
const { data: errorData } = error;
const fields = errorData.fields || [];
if (fields.length) {
description = fields[0].error;
}
return Promise.reject({
error: {
code: error.code,
fields: fields.map((item) => item.field),
description
} as IError
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment