Giter VIP home page Giter VIP logo

venzee-errors's Introduction

venzee-errors

Generates Venzee-specific Error objects, and converts those that are less endowed to the Venzee Error Hotness.

Installation

$ npm install venzee/venzee-errors --save

Usage

Generate a Venzee Error from scratch, based on the defined error codes in this package.

'use strict';
var util = require('util');
var venzeeError = require('venzee-errors');
var E = venzeeError.E;

var err = venzeeError(E.USERNAME_UNAVAILABLE);
if (util.isError(err)) {
  console.log('util.isError calls this an bona-fide error!');
}

// look at the error like an error response payload
console.log(JSON.stringify(err.toBody(), null, 2));

Output

{
  "error": {
    "name": "UsernameUnavailable",
    "message": "The username you requested is unavailable.",
    "@context": "/contexts/Error.jsonld",
    "@id": "/errors/UsernameUnavailableError",
    "code": 4000002,
    "statusDescription": "Conflict",
    "status": 409,
    "statusCode": 409
  }
}

What about complex errors?

We retain all that, while adding our sugar. A regular JSON representation of a loopback-datasource-juggler ValidationError looks like this:

{
  "error": {
    "name": "ValidationError",
    "message": "The `user` instance is not valid. Details: `firstName` can't be blank; `lastName` can't be blank.",
    "statusCode": 422,
    "details": {
      "context": "user",
      "codes": {
        "firstName": [
          "presence"
        ],
        "lastName": [
          "presence"
        ]
      },
      "messages": {
        "firstName": [
          "can't be blank"
        ],
        "lastName": [
          "can't be blank"
        ]
      }
    }
  }
}

Process that error as a Venzee Error, and you get this:

var err = venzeeError(e);
console.log(JSON.stringify(err.toBody(), null, 2));

Output:

{
  "error": {
    "name": "ValidationError",
    "message": "The `user` instance is not valid. Details: `firstName` can't be blank; `lastName` can't be blank.",
    "@context": "/contexts/Error.jsonld",
    "@id": "/errors/ValidationError",
    "code": 4004220,
    "statusDescription": "Unprocessable Entity",
    "status": 422,
    "statusCode": 422,
    "details": {
      "context": "user",
      "codes": {
        "firstName": [
          "presence"
        ],
        "lastName": [
          "presence"
        ]
      },
      "messages": {
        "firstName": [
          "can't be blank"
        ],
        "lastName": [
          "can't be blank"
        ]
      }
    }
  }
}

A few more examples await you in example.js!


Adding new error codes

$ grunt new

You'll be prompted for an error code string, and a human-friendly message. The code will be added to lib/codes.json, with the next-available 400* series code number.

grunt new

Options

logger

Type: String optional

Default: venzee-logger

Name of a logging module to use internally. For example, a value of winston will load the winston log package.

codes

Type: String optional

Default: ./codes.json codes.json from inside lib directory

File path to your own custom error codes.

Reference

http status code: http://www.restapitutorial.com/httpstatuscodes.html

venzee-errors's People

Contributors

claylo avatar xinv avatar cleondz avatar

Watchers

James Cloos avatar  avatar Kate avatar  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.