Skip to content
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

Merged
merged 7 commits into from
Sep 22, 2022
Merged

Fix parse api errors #699

merged 7 commits into from
Sep 22, 2022

Conversation

ErnoW
Copy link
Member

@ErnoW ErnoW commented Sep 21, 2022


name: 'Pull request'
about: A new pull request

New Pull Request

Checklist

  • I am not disclosing a vulnerability.
  • My code is conform the code style
  • I have made corresponding changes to the documentation
  • I have updated Typescript definitions when needed

Issue Description

Parse API errors when api returns a 'message' in the error response

@ErnoW ErnoW requested a review from b4rtaz September 21, 2022 23:36
@changeset-bot
Copy link

changeset-bot bot commented Sep 21, 2022

🦋 Changeset detected

Latest commit: 26859b6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 10 packages
Name Type
@moralisweb3/core Patch
@moralisweb3/auth Patch
@moralisweb3/evm-api Patch
moralis Patch
@moralisweb3/sol-api Patch
@moralisweb3/streams Patch
@moralisweb3/api-utils Patch
@moralisweb3/evm-utils Patch
@moralisweb3/sol-utils Patch
@moralisweb3/client-firebase-auth Patch

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

@ErnoW ErnoW enabled auto-merge September 21, 2022 23:37
Comment on lines 11 to 41
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)';
};
Copy link
Collaborator

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?

Copy link
Member Author

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.

@ErnoW ErnoW merged commit 33c133a into main Sep 22, 2022
@ErnoW ErnoW deleted the fix-parse-api-errors branch September 22, 2022 09:48
@github-actions github-actions bot mentioned this pull request Sep 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants