@hckrnews/error

Extended Errors

Usage no npm install needed!

<script type="module">
  import hckrnewsError from 'https://cdn.skypack.dev/@hckrnews/error';
</script>

README

@hckrnews/error

NPM version Build Status Coveralls Status Scrutinizer Code Quality

With this package you can throw a custom error with more details. The object extends an Error object.

Types:

  • AppError
  • AuthenticationError
  • NoContent
  • NotFoundError
  • NotImplementedError
  • ServerError
  • TimeoutError
  • ValidationError

You can send:

  • message (type: String)
  • value (type: mixed)
  • type (String|Array|Object|Number|Boolean|Date|...)
  • me (current method, no validation on the value)

It validate the values by this schema:

{
    name: String,
    message: String,
    'value?': 'mixed',
    status: Number,
    'type?': Function,
    date: Date,
};

In the catch method, you can use these fields:

  • name (type: String)
  • message (type: String)
  • value (optional, type: mixed)
  • errorStatus (type: Number)
  • status (type: Number, like errorStatus, but it will return 500 if there are validation errors)
  • type (optional, type: Function)
  • date (type: Date, current timestamp)
  • me (type: mixed)
  • values (type: Object, all fields)
  • hasErrors (type: Boolean, returns true if there are validation errors)

Install the package:

npm install @hckrnews/error or yarn add @hckrnews/error

Example usage:

import { NotFoundError } from '@hckrnews/error'

class Test {
    /**
     * Throw an error.
     */
    doSomethingWrong () {
        throw new NotFoundError({
            value: 'test',
            type: String,
            message: 'Example text',
            me: this.constructor,
        });
    }

    /**
     * Catch the error.
     */
    run() {
        try {
            this.doSomethingWrong()
        } catch(error) {
            console.error(error.message)
            console.log({ value: error.value })
        }
    }
}