npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@paysera/error-message-resolver

v2.0.3

Published

Library that helps resolve error messages

Downloads

22

Readme

@paysera/error-message-resolver

Build Status Coverage Status dependencies devDependencies

Services

isHttpError

Checks if given error is axios http error.

Arguments:

| argument | type | default | required | description | |-----------|-------|---------|--------------------|-------------| | error | any | | :white_check_mark: | Threw error |

Returns:

| type | description | |-------------|------------------------------------------------| | boolean | True if error is http error, false - otherwise |

Example:

const error = { config: {}, request: {} };
isHttpError(error); // => true

isNetworkError

Checks if given error is axios network error. Network error can happen for a number of reasons, for example poor network, CSP, etc.

Arguments:

| argument | type | default | required | description | |-----------|-------|---------|--------------------|-------------| | error | any | | :white_check_mark: | Threw error |

Returns:

| type | description | |-------------|---------------------------------------------------| | boolean | True if error is network error, false - otherwise |

Example:

const error = { request: { status: 0 } };
isNetworkError(error); // => true

resolveErrorMessage

resolveErrorMessage checks if an error is HTTP and if it is, passes to the resolveHttpErrorMessage to be handled. If error isn't HTTP, resolver will try to resolve correct message from message.custom or message.default.

Arguments:

| argument | type | default | required | description | |-------------|----------------------|---------|--------------------|--------------------------------------------------------------------| | error | any | | :white_check_mark: | Error. resolveErrorMessage checks if it http error | | message | ErrorMessageConfig | | :white_check_mark: | Messages for error |

Returns:

ErrorMessage entity Object with keys main and additional:

| type | description | |------------------|-------------------------------------------------------------------------| | ErrorMessage | Instance of ErrorMessage which contains primary and secondary message |

Example of message config:

const error = { config: {}, request: {}, response: { status: 400 } };

resolveErrorMessage(
    error,
    (new ErrorMessageConfig(
        (new HttpErrorMessageConfig('Http default message'))
            .setCustom((errorResponse) => {
                if (errorResponse.getStatus() <= 0 || errorResponse.getStatus() >= 500) {
                    return 'message';
                }

                if (
                    isObject(errorResponse.getData())
                    && isUndefined((errorResponse.getData()).error_description)
                ) {
                    return 'other_message';
                }

                return null;
            }),
        'Default message'
    ))
        .setCustom((errorData) => {
            if (errorData instanceof SomeError) {
                return 'any_message';
            }

            return null;
        }),
    );

resolveHttpErrorMessage (private function)

resolveHttpErrorMessage trying to find correct message for given error. It only resolves message for http errors so if given error is not http error it will return message.default message. If it can't resolve message - returns message.default.

Flow:

  • checks if it is http error
  • checks if it is network error
  • checks response.data.error
  • checks response.status if it is 429
  • checks response.status
  • checks response.data.errors
  • checks response.data.error_description
  • checks response.data.error with default translations

Arguments:

| argument | type | default | required | description | |-------------|--------------------------|---------|--------------------|--------------------------------------------------------------------------------------------------------------| | error | any | | :white_check_mark: | Error. resolveHttpErrorMessage checks if it http error | | message | HttpErrorMessageConfig | | :white_check_mark: | Messages for error. Default message and network error must be provided(message.default, message.network) |

message.status.429 - if function is provided this function will receive an additional argument waitForDuration(if it find gf-wait-for property in response header). waitForDuration will be moment duration.

Returns:

| type | description | |------------|-------------------------| | string | Message for given error |

Example:

const error = { config: {}, request: {}, response: { status: 400 } };
resolveHttpErrorMessage(error, { default: 'Default message', status: { 400: 'Not found' } }); // => Not found

Example of message config:

resolveHttpErrorMessage(
    error,
    (new HttpErrorMessageConfig())
        .setDefault('Default message')
        .setNetwork('Network error message')
        .setErrorData((new ErrorDataMessageConfig()).setConfig({
            'unknown_error': 'Error: unknown_error', // string
            'rate_limit_exceeded': (errorResponse) => { // function
               return 'Error: rate_limit_exceeded';
            },
            'invalid_credentials': (errorResponse) => { // async function
               return Promise.resolve('Error: invalid_credentials');
            },
            'invalid_request': Promise.resolve('Error: invalid_request'), // async string
        }))
        .setStatus((new ErrorStatusMessageConfig()).setConfig({
            429: (errorResponse, waitForDuration) => { // too many request function
                if (waitForDuration !== null) {
                    return `Too many requests, wait for: ${waitFor.asSeconds()}`;
                }
                return 'Too many requests';
            },
            500: 'Response status: 500', // string
            501: (errorResponse) => { // function
                return 'Response status: 501';
            },
            502: (errorResponse) => { // async function
                return Promise.resolve('Response status: 502');
            },
            503: Promise.resolve('Response status: 503'), // async string
        }))
);