Skip to content

Commit

Permalink
fix: axios errors missing response status (#248)
Browse files Browse the repository at this point in the history
* fix: axios errors missing response status

* chore: remove useless log error
  • Loading branch information
QRuhier authored Oct 14, 2024
1 parent b42884d commit 4ccffd3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
2 changes: 1 addition & 1 deletion drama-queen/src/core/adapters/queenApi/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function createApiClient(params: {
if (!(error instanceof AxiosError)) {
return Promise.reject(error)
}
handleAxiosError(error)
return Promise.reject(handleAxiosError(error))
}
)
return { axiosInstance }
Expand Down
39 changes: 17 additions & 22 deletions drama-queen/src/core/tools/axiosError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,25 @@ const { t } = getTranslation('errorMessage')

export function handleAxiosError(error: AxiosError) {
if (!error.response) {
throw new AxiosError(
error.message =
"Une erreur s'est produite lors du traitement de la requête. Veuillez réessayer plus tard."
)
return error
}

const status = error.response.status
switch (status) {
case 400:
throw new AxiosError(t('400'))
case 401:
throw new AxiosError(t('401'))
case 403:
throw new AxiosError(t('403'))
case 404:
throw new AxiosError(t('404'))
break
case 500:
throw new AxiosError(t('500'))
case 502:
throw new AxiosError(t('502'))
case 503:
throw new AxiosError(t('503'))
case 504:
throw new AxiosError(t('504'))
default:
throw new AxiosError(t('longUnknownError'))

const messages: { [key: number]: string } = {
400: t('400'),
401: t('401'),
403: t('403'),
404: t('404'),
500: t('500'),
502: t('502'),
503: t('503'),
504: t('504'),
}

error.message = messages[status] || t('longUnknownError')

return error
}

0 comments on commit 4ccffd3

Please sign in to comment.