GithubHelp home page GithubHelp logo

anthrax3 / human-error Goto Github PK

View Code? Open in Web Editor NEW

This project forked from franciscop/human-error

0.0 0.0 0.0 544 KB

Allows you to write errors so people can understand

License: MIT License

JavaScript 100.00%

human-error's Introduction

human-error CircleCI

Allows you to write errors so people can understand:

Show an error in the code Show an error in the code
An error while running node app.js Same error while testing with jest

Getting started

Install it with NPM:

npm install human-error

Then create your error codes:

// myfun-errors.js
const errors = require('human-error')({
  // [optional] a url where the error is explained in-depth
  url: name => `http://example.com/error/${name}`
});

// "NAMESPACE" can be whatever you want (or nothing at all)
errors['NAMESPACE.missingcallback'] = () => `
  myFun() expects a callback to be passed but nothing was passed.
`;

errors['NAMESPACE.invalidcallback'] = ({ type }) => `
  myFun() expects the argument to be a callback function.
  ${type ? `"${type}" was passed instead.` : ''}
`;

module.exports = errors;

Then use them in your code:

// myfun.js
const errors = require('./myfun-errors');

module.exports = (cb) => {
  if (!cb) {
    throw errors('NAMESPACE.missingcallback');
  }
  if (!(cb instanceof Function)) {
    throw errors('NAMESPACE.invalidcallback', { type: typeof cb });
  }
  cb('Cool library!');
};

Options

  • url [false]: if there's an url to show more info. It can be a string in which case it will just be printed or a function that build the error such as (key) => 'https://example.com/errors#' + key so you can show the appropriate support url.
  • extra [{}]: an object that defaults to all of the options in the namespace. Useful for example for status code errors (status: 500) etc.
  • width [80]: the minimum width of the row. It cannot cut strings since links shouldn't be cut.
  • plain [false]: avoid generating a table and using plain-text only, in case some things break.

human-error's People

Contributors

franciscop avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.