1.0.5 • Published 9 years ago

sails-service-error-codes v1.0.5

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

sails-service-error-codes

Build Status Coverage Downloads npm version dependencies dev dependencies License

Getting Started

Install it via npm:

npm install sails-service-error-codes

Usage

Create a new service:

// api/services/ErrorCodes.js

module.exports = require('sails-service-error-codes').getCodes();

Now you can use ErrorCodes anywhere in your app. For example:

// api/responses/ok.js

module.exports = function (data, code, message, root) {
  var response = _.assign({
    code: code || ErrorCodes.ok.code,
    message: message || ErrorCodes.ok.message,
    data: data || {}
  }, root);

  this.req._sails.log.silly('Sent (200 OK)\n', response);

  this.res.status(200);
  this.res.jsonx(response);
};

Also you can override or add your own error codes. Just pass object with new ones (or overrided old ones) in getCodes function:

// it can be somewhere in config
var newCodes = {
  ok: {
    code: 'OKAY'
  },
  someNewError: {
    code: 'E_SOME_ERROR',
    message: 'Some error'
  }
};

module.exports = require('sails-service-error-codes').getCodes(newCodes);

Default error codes

var defaultCodes = {
  badRequest: {
    code: 'E_BAD_REQUEST',
    message: 'The request cannot be fulfilled due to bad syntax'
  },

  created: {
    code: 'CREATED',
    message: 'The request has been fulfilled and resulted in a new resource being created'
  },

  forbidden: {
    code: 'E_FORBIDDEN',
    message: 'User not authorized to perform the operation'
  },

  notFound: {
    code: 'E_NOT_FOUND',
    message: 'The requested resource could not be found but may be available again in the future'
  },

  ok: {
    code: 'OK',
    message: 'Operation is successfully executed'
  },

  serverError: {
    code: 'E_INTERNAL_SERVER_ERROR',
    message: 'Something bad happened on the server'
  },

  unauthorized: {
    code: 'E_UNAUTHORIZED',
    message: 'Missing or invalid authentication token'
  }
};

License

The MIT License (MIT)

Copyright (c) 2015 IncoCode

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1.0.5

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago

0.1.0

9 years ago