-
Notifications
You must be signed in to change notification settings - Fork 259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix parse api errors #699
Fix parse api errors #699
Conversation
🦋 Changeset detectedLatest commit: 26859b6 The changes in this PR will be included in the next version bump. This PR includes changesets to release 10 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
type AxiosApiError = AxiosError<{ message?: string | string[] }> & { | ||
response: NonNullable<AxiosError<{ message: string | string[] }>['response']>; | ||
}; | ||
|
||
const isAxiosApiError = (error: unknown): error is AxiosApiError => { | ||
// Check if the error is an axios error | ||
if (!(error instanceof AxiosError)) { | ||
return false; | ||
} | ||
|
||
// Check if the error is a result of a 400 or 500 response | ||
if (error.code !== AxiosError.ERR_BAD_REQUEST && error.code !== AxiosError.ERR_BAD_RESPONSE) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
const getApiMessageFromError = (error: AxiosApiError) => { | ||
const { message } = error.response.data; | ||
|
||
if (Array.isArray(message)) { | ||
return message.join(', '); | ||
} | ||
|
||
if (typeof message === 'string') { | ||
return message; | ||
} | ||
|
||
return 'Unknown error (no error info returned from API)'; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That part could be in the other place. New file? AxiosError.ts
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cleaned it up to a separate file, renamed it and add description.
I'm not too happy with this approach though. As it couples api behaviour in our core package. Ideally we want this specified in the separate api packages or generally in the apiUtils package. Maybe by providing a custom errorhandler. So that could be a nice future improvement.
name: 'Pull request'
about: A new pull request
New Pull Request
Checklist
Issue Description
Parse API errors when api returns a 'message' in the error response