Created
March 27, 2018 16:03
-
-
Save maiordom/a6f36ba8e3f048473216606253417580 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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