PageRenderTime 3ms CodeModel.GetById 2ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/nano/error.js

https://github.com/vastus/blogode
JavaScript | 122 lines | 86 code | 2 blank | 34 comment | 10 complexity | 3b75947731f92942c56907f9be4661a0 MD5 | raw file
  1var EventEmitter = require('events').EventEmitter;
  2var STATUS_CODES = { '100': 'Continue'
  3                   , '101': 'Switching Protocols'
  4                   , '102': 'Processing'
  5                   , '200': 'OK'
  6                   , '201': 'Created'
  7                   , '202': 'Accepted'
  8                   , '203': 'Non-Authoritative Information'
  9                   , '204': 'No Content'
 10                   , '205': 'Reset Content'
 11                   , '206': 'Partial Content'
 12                   , '207': 'Multi-Status'
 13                   , '300': 'Multiple Choices'
 14                   , '301': 'Moved Permanently'
 15                   , '302': 'Moved Temporarily'
 16                   , '303': 'See Other'
 17                   , '304': 'Not Modified'
 18                   , '305': 'Use Proxy'
 19                   , '307': 'Temporary Redirect'
 20                   , '400': 'Bad Request'
 21                   , '401': 'Unauthorized'
 22                   , '402': 'Payment Required'
 23                   , '403': 'Forbidden'
 24                   , '404': 'Not Found'
 25                   , '405': 'Method Not Allowed'
 26                   , '406': 'Not Acceptable'
 27                   , '407': 'Proxy Authentication Required'
 28                   , '408': 'Request Time-out'
 29                   , '409': 'Conflict'
 30                   , '410': 'Gone'
 31                   , '411': 'Length Required'
 32                   , '412': 'Precondition Failed'
 33                   , '413': 'Request Entity Too Large'
 34                   , '414': 'Request-URI Too Large'
 35                   , '415': 'Unsupported Media Type'
 36                   , '416': 'Requested Range Not Satisfiable'
 37                   , '417': 'Expectation Failed'
 38                   , '418': 'I\'m a teapot'
 39                   , '422': 'Unprocessable Entity'
 40                   , '423': 'Locked'
 41                   , '424': 'Failed Dependency'
 42                   , '425': 'Unordered Collection'
 43                   , '426': 'Upgrade Required'
 44                   , '500': 'Internal Server Error'
 45                   , '501': 'Not Implemented'
 46                   , '502': 'Bad Gateway'
 47                   , '503': 'Service Unavailable'
 48                   , '504': 'Gateway Time-out'
 49                   , '505': 'HTTP Version not supported'
 50                   , '506': 'Variant Also Negotiates'
 51                   , '507': 'Insufficient Storage'
 52                   , '509': 'Bandwidth Limit Exceeded'
 53                   , '510': 'Not Extended' 
 54                   };
 55/*
 56 * generic error
 57 *
 58 * e.g. missing rev information:
 59 *
 60 * { "stack": "Error: Document update conflict. at gen_err(error.js:14:43)",
 61 *   "message": "Document update conflict.",
 62 *   "error": "conflict",
 63 *   "http_code": 409,
 64 *   "namespace": "couch",
 65 *   "request": {
 66 *       "method": "PUT",
 67 *       "headers": {
 68 *           "content-type": "application/json",
 69 *           "accept": "application/json",
 70 *           "authorization": "BasicYWRtaW46YWRtaW4=",
 71 *           "content-length": 13
 72 *       },
 73 *       "body": {"foo": "baz"},
 74 *       "uri": "http://admin:admin@localhost: 5984/doc_up1/foo",
 75 *       "callback": [Function]
 76 *   }
 77 * }
 78 * 
 79 * extension on error to support more complex logic.
 80 * 
 81 * @param {error:error|string} the error or a reason for the error
 82 * @param {code:string} the recognizable error code
 83 * @param {http_code:integer:optional} the http code from couchdb
 84 * @param {request:object} the request that was made to couch
 85 * @param {type:string} a namespace for the error, e.g. couch 
 86 *
 87 * @return an augmented error that helps you know more than the stack trace
 88 */
 89function gen_err(scope,error,code,request,status_code) {
 90  error       = error             || STATUS_CODES[status_code] || 'Unknown Error';
 91  code        = code                                           || 'unknown';
 92  status_code = typeof status_code === 'number' && status_code || 500;
 93  request     = request                                        || {};
 94  if(typeof error === 'string') { error = new Error(error); }
 95  error.error          = code;
 96  error['status-code'] = status_code;
 97  error.scope          = scope;
 98  error.request        = request;
 99  return error;
100}
101
102function request_err(error, code, request, callback) {
103  if(typeof request === 'function') {
104    callback = request;
105    request  = {};
106  }
107  error = gen_err('request', error, code, request);
108  if(callback) {
109    callback(error); 
110    return request;
111  } else {
112    var em = new EventEmitter();
113    process.nextTick(function() { em.emit('error', error); });
114    return em;
115  }
116}
117
118exports.uncaught = function (e,c,r,s) { return gen_err('uncaught',e,c,r,s); };
119exports.request  = function (e,c,r,s) { return gen_err('request',e,c,r,s);  };
120exports.couch    = function (e,c,r,s) { return gen_err('couch',e,c,r,s);    };
121exports.init     = function (e,c,r,s) { return gen_err('init',e,c,r,s);     };
122exports.request_err = request_err;