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

mecanizou-errors

v1.1.0

Published

Options for setting errors properties

Downloads

550

Readme

Mecanizou Errors Library

This module creates errors instances and functions for more easily deal and handle errors across the service.

Install

npm install --save mecanizou-errors

How to use

5 different variables are exported from this library:

Warning, Exception, MecaniError, isMecaniError, throwMecaniError

MecaniError

This one is a class extended from 'Error', and its constructor requires 4 variables:

errorEnum (string): A code for dealing with errors in FrontEnd, which can be used to create an error dictionary;

message (string): Same as an Error.message;

errorType ('Warning' | 'Exception'): Which kind of error it is. This type can be used, for example, to distinguish which errors should be displayed on a RollbarService;

statusCode (number): same as an Error.statusCode. Will be used as the http Status Code;


Once it is thrown, a MecaniError will display the following information:

errorDetails (string): same as the string passed as 'message' to the MecaniError constructor;

name ('Warning' | 'Exception'): same as the string passed as 'errorType' to the MecaniError constructor;

errorEnum (string): same as constructor;

statusCode (number): same as constructor;

message (string): same as constructor;

Warning

Warning is an object where its keys are http errors (both written or numbers) and its atributes are functions that return a new instance of a MecaniError. An example is shown as it follows:


    Warning.BadRequest = (errorEnum: string, message: string) => new MecaniError(errorEnum, message, 'Warning', 400)

    Warning.400 = (errorEnum: string, message: string) => new MecaniError(errorEnum, message, 'Warning', 400)

In case the user wants to throw a warning Error with a status 404 (NotFound), for example, all it takes is to do as follows:


    import { Warning } from 'mecanizou-errors';

    throw Warning.NotFound('Example of ErrorEnum', 'Example of message');

Exception

Just like the Warning, Exception is an object where its keys are http errors (both written or numbers) and its atributes are functions that return a new instance of a MecaniError. However, it is a common sense that an Exception error is more severe than a Warning error. It must be determined by the team which error should be considered a Warning and which should be considered an Exception.

An example of how to use an Exception error is shown as follows:


    import { Exception } from 'mecanizou-errors';

    throw Exception.BadRequest('Example of ErrorEnum', 'Example of message');

isMecaniError

This is a simple function that receives an Error and returns a boolean value. If the given error is an instance of MecaniError, the returned value will be true. Otherwise, it will be false. This function can be used in order to find out if an specific error was expected across the code or it was caused by a code mistake (As trying to find a key from an undefined element, for example). That of course would only be the case considering that every thrown error across the code is an instance of MecaniError.


    import { isMecaniError } from 'mecanizou-errors';

    try {
        const object = {}
        const teste = object.element.element
    } catch (error) {
        return isMecaniError(error)
    }

    // expect false

    // ...

    import { Warning, isMecaniError } from 'mecanizou-errors';

    try {
        const object = {}
        throw Warning.NotFound('testing', 'message');
    } catch (error) {
        return isMecaniError(error)
    }

    // expect true

throwMecaniError

This is a function that requires an error and will return a thrown MecaniError. It can be used to avoid cases where an code error is thrown with a statusCode = 200. Internally, the function will check if the received error is an instance of MecaniError. If it is, it will simplu throw the error as it was received. If not, it will throw an Exception error, with statusCode = 400. The simple function is described as follow:


const throwMecaniError = (error: Error) => {
    if (isMecaniError(error)) {
        throw error;
    } else {
        throw Exception.BadRequest('UndefinedError', error.message);
    }
}