1.1.0 • Published 1 year ago

mecanizou-errors v1.1.0

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

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);
    }
}