Name: js-handler/node_modules/restify/lib/errors/rest_error.js 
1:
// Copyright 2012 Mark Cavage, Inc.  All rights reserved.
2:
 
3:
var util = require('util');
4:
 
5:
var assert = require('assert-plus');
6:
 
7:
var httpErrors = require('./http_error');
8:
 
9:
 
10:
 
11:
///--- Globals
12:
 
13:
var slice = Function.prototype.call.bind(Array.prototype.slice);
14:
 
15:
var HttpError = httpErrors.HttpError;
16:
 
17:
var CODES = {
18:
        BadDigest: 400,
19:
        BadMethod: 405,
20:
        Internal: 500, // Don't have InternalErrorError
21:
        InvalidArgument: 409,
22:
        InvalidContent: 400,
23:
        InvalidCredentials: 401,
24:
        InvalidHeader: 400,
25:
        InvalidVersion: 400,
26:
        MissingParameter: 409,
27:
        NotAuthorized: 403,
28:
        PreconditionFailed: 412,
29:
        RequestExpired: 400,
30:
        RequestThrottled: 429,
31:
        ResourceNotFound: 404,
32:
        WrongAccept: 406
33:
};
34:
 
35:
 
36:
 
37:
///--- API
38:
 
39:
function RestError(options) {
40:
        assert.object(options, 'options');
41:
 
42:
        options.constructorOpt = options.constructorOpt || RestError;
43:
        HttpError.apply(this, arguments);
44:
 
45:
        var self = this;
46:
        this.restCode = options.restCode || 'Error';
47:
        this.body = options.body || {
48:
                code: self.restCode,
49:
                message: options.message || self.message
50:
        };
51:
}
52:
util.inherits(RestError, HttpError);
53:
 
54:
 
55:
 
56:
///--- Exports
57:
 
58:
module.exports = {
59:
        RestError: RestError
60:
};
61:
 
62:
Object.keys(CODES).forEach(function (k) {
63:
        var name = k;
64:
        if (!/\w+Error$/.test(name))
65:
                name += 'Error';
66:
 
67:
        module.exports[name] = function (cause, message) {
68:
                var index = 1;
69:
                var opts = {
70:
                        restCode: (k === 'Internal' ? 'InternalError' : k),
71:
                        statusCode: CODES[k]
72:
                };
73:
 
74:
                opts.constructorOpt = arguments.callee;
75:
 
76:
                if (cause && cause instanceof Error) {
77:
                        opts.cause = cause;
78:
                } else if (typeof (cause) === 'object') {
79:
                        opts.body = cause.body;
80:
                        opts.cause = cause.cause;
81:
                        opts.message = cause.message;
82:
                        opts.statusCode = cause.statusCode || CODES[k];
83:
                } else {
84:
                        index = 0;
85:
                }
86:
 
87:
                var args = slice(arguments, index);
88:
                args.unshift(opts);
89:
                RestError.apply(this, args);
90:
        };
91:
        util.inherits(module.exports[name], RestError);
92:
        module.exports[name].displayName =
93:
                module.exports[name].prototype.name =
94:
                name;
95:
});