Comparing version 0.5.1 to 0.5.3
@@ -127,4 +127,11 @@ restify-response(7) -- The Response Object | ||
You can create one of these with `restify.newError` as such: | ||
There are built-ins for all codes defined in `restify.RestCodes`, and you can | ||
chuck them into `next()` like so: | ||
server.get('/foo', function(req, res, next) { | ||
return next(new restify.BadRequestError('you did something wrong')); | ||
}); | ||
In previous versions of restify, you used like `restify.newError`: | ||
response.sendError(restify.newError({ | ||
@@ -131,0 +138,0 @@ httpCode: 409, |
@@ -483,3 +483,3 @@ // Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved. | ||
error: err, | ||
message: 'HTTP Client error event' | ||
message: err.message | ||
})); | ||
@@ -486,0 +486,0 @@ } |
@@ -9,22 +9,3 @@ // Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved. | ||
///--- Error Base class | ||
function RESTError(restCode, httpCode, message, caller) { | ||
if (Error.captureStackTrace) | ||
Error.captureStackTrace(this, caller || RESTError); | ||
this.__defineGetter__('httpCode', function() { | ||
return httpCode; | ||
}); | ||
this.__defineGetter__('restCode', function() { | ||
return restCode; | ||
}); | ||
this.__defineGetter__('message', function() { | ||
return message || restCode; | ||
}); | ||
} | ||
util.inherits(RESTError, Error); | ||
module.exports = { | ||
@@ -76,19 +57,42 @@ | ||
Object.keys(RestCodes).forEach(function(code) { | ||
///--- Error Base class | ||
var err = '' + code; | ||
function RESTError(httpCode, restCode, message, extra, caller) { | ||
if (Error.captureStackTrace) | ||
Error.captureStackTrace(this, caller); | ||
this.__defineGetter__('httpCode', function() { | ||
return httpCode; | ||
}); | ||
this.__defineGetter__('restCode', function() { | ||
return restCode; | ||
}); | ||
this.__defineGetter__('name', function() { | ||
return restCode; | ||
}); | ||
this.__defineGetter__('message', function() { | ||
return message || restCode; | ||
}); | ||
this.__defineGetter__('details', function() { | ||
return extra || {}; | ||
}); | ||
} | ||
util.inherits(RESTError, Error); | ||
module.exports.RESTError = RESTError; | ||
Object.keys(RestCodes).forEach(function(c) { | ||
var err = '' + c; | ||
if (!/\w+Error$/.test(err)) | ||
err += 'Error'; | ||
// At this point LDAP_OPERATIONS_ERROR is now OperationsError in $err | ||
// and 'Operations Error' in $msg | ||
module.exports[err] = function(code, message, caller) { | ||
module.exports[err] = function(code, name, message, details) { | ||
RESTError.call(this, | ||
err, | ||
code, | ||
(typeof(message) === 'string' ? message : ''), | ||
caller || module.exports[err]); | ||
typeof(code) === 'number' ? code : RestCodes[c].httpCode, | ||
c, | ||
typeof(message) === 'string' ? message : err, | ||
typeof(details) === 'object' ? details : null, | ||
arguments.callee); | ||
}; | ||
module.exports[err].constructor = module.exports[err]; | ||
util.inherits(module.exports[err], RESTError); | ||
}); |
@@ -105,3 +105,3 @@ // Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved. | ||
_pad(d.getUTCDate()) + '/' + | ||
_pad(d.getUTCMonth()) + '/' + | ||
_pad(d.getUTCMonth() + 1) + '/' + | ||
d.getUTCFullYear() + ':' + | ||
@@ -108,0 +108,0 @@ _pad(d.getUTCHours()) + ':' + |
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved. | ||
var HttpCodes = require('./http_codes'); | ||
function RestCode(code, message) { | ||
this.__defineGetter__('httpCode', function() { | ||
return code; | ||
}); | ||
this.__defineGetter__('message', function() { | ||
return message; | ||
}); | ||
} | ||
RestCode.prototype.toString = function() { | ||
return this.message; | ||
}; | ||
RestCode.prototype.valueOf = function() { | ||
return this.toString().valueOf(); | ||
}; | ||
///--- API | ||
module.exports = { | ||
BadRequest: 'BadRequest', | ||
InternalError: 'InternalError', | ||
InvalidArgument: 'InvalidArgument', | ||
InvalidCredentials: 'InvalidCredentials', | ||
InvalidHeader: 'InvalidHeader', | ||
InvalidVersion: 'InvalidVersion', | ||
MissingParameter: 'MissingParameter', | ||
NotAuthorized: 'NotAuthorized', | ||
RequestThrottled: 'RequestThrottled', | ||
RequestTooLarge: 'RequestTooLarge', | ||
ResourceMoved: 'ResourceMoved', | ||
ResourceNotFound: 'ResourceNotFound', | ||
RetriesExceeded: 'RetriesExceeded', | ||
UnknownError: 'UnknownError' | ||
BadRequest: new RestCode(HttpCodes.BadRequest, 'BadRequest'), | ||
InternalError: new RestCode(HttpCodes.InternalError, 'InternalError'), | ||
InvalidArgument: new RestCode(HttpCodes.Conflict, 'InvalidArgument'), | ||
InvalidCredentials: new RestCode(HttpCodes.NotAuthorized, | ||
'InvalidCredentials'), | ||
InvalidHeader: new RestCode(HttpCodes.BadRequest, 'InvalidHeader'), | ||
InvalidVersion: new RestCode(HttpCodes.RetryWith, 'InvalidVersion'), | ||
MissingParameter: new RestCode(HttpCodes.Conflict, 'MissingParameter'), | ||
NotAuthorized: new RestCode(HttpCodes.Forbidden, 'NotAuthorized'), | ||
RequestThrottled: new RestCode(HttpCodes.Throttle, 'RequestThrottled'), | ||
RequestTooLarge: new RestCode(HttpCodes.RequestTooLarge, 'RequestTooLarge'), | ||
ResourceMoved: new RestCode(HttpCodes.Redirect, 'ResourceMoved'), | ||
ResourceNotFound: new RestCode(HttpCodes.NotFound, 'ResourceNotFound'), | ||
RetriesExceeded: new RestCode(HttpCodes.ServiceUnavailable, | ||
'RetriesExceeded'), | ||
UnknownError: new RestCode(HttpCodes.InternalError, 'UnknownError') | ||
}; |
@@ -13,2 +13,3 @@ // Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved. | ||
var uuid = require('node-uuid'); | ||
var xml2js = require('xml2js'); | ||
@@ -19,2 +20,3 @@ var HttpCodes = require('./http_codes'); | ||
var newError = require('./error').newError; | ||
var sprintf = require('./sprintf').sprintf; | ||
@@ -328,2 +330,26 @@ // Just force this to extend http.ServerResponse | ||
request.on('end', function() { | ||
function done(err, bParams) { | ||
if (err) { | ||
return response.sendError(newError({ | ||
httpCode: HttpCodes.BadRequest, | ||
restCode: RestCodes.InvalidArgument, | ||
message: 'Invalid Content: ' + err.message | ||
})); | ||
} | ||
Object.keys(bParams).forEach(function(k) { | ||
if (request.params.hasOwnProperty(k)) { | ||
return response.sendError(newError({ | ||
httpCode: HttpCodes.Conflict, | ||
restCode: RestCodes.InvalidArgument, | ||
message: 'duplicate parameter detected: ' + k | ||
})); | ||
} | ||
request.params[k] = bParams[k]; | ||
}); | ||
log.trace('_parseRequest: params parsed as: %o', request.params); | ||
return next(); | ||
} | ||
if (request.body) { | ||
@@ -346,38 +372,23 @@ log.trace('_parseRequest: req.body=%s', request.body); | ||
var bParams; | ||
try { | ||
if (request._config.contentHandlers[contentType]) { | ||
if (request._config.contentHandlers[contentType]) { | ||
try { | ||
bParams = request._config.contentHandlers[contentType](request.body, | ||
request, | ||
response); | ||
} else if (contentType) { | ||
return response.sendError(newError({ | ||
httpCode: HttpCodes.UnsupportedMediaType, | ||
restCode: RestCodes.InvalidArgument, | ||
message: contentType + ' unsupported' | ||
})); | ||
response, | ||
done); | ||
if (bParams) | ||
return done(null, bParams); | ||
} catch (e) { | ||
return done(e); | ||
} | ||
} catch (e) { | ||
} else if (contentType) { | ||
return response.sendError(newError({ | ||
httpCode: HttpCodes.BadRequest, | ||
httpCode: HttpCodes.UnsupportedMediaType, | ||
restCode: RestCodes.InvalidArgument, | ||
message: 'Invalid Content: ' + e.message | ||
message: contentType + ' unsupported' | ||
})); | ||
} | ||
for (var k in bParams) { | ||
if (bParams.hasOwnProperty(k)) { | ||
if (request.params.hasOwnProperty(k)) { | ||
return response.sendError(newError({ | ||
httpCode: HttpCodes.Conflict, | ||
restCode: RestCodes.InvalidArgument, | ||
message: 'duplicate parameter detected: ' + k | ||
})); | ||
} | ||
request.params[k] = bParams[k]; | ||
} | ||
} | ||
} else { | ||
return done(null, {}); | ||
} | ||
log.trace('_parseRequest: params parsed as: %o', request.params); | ||
return next(); | ||
}); | ||
@@ -713,2 +724,85 @@ } | ||
if (!options.contentHandlers['application/xml']) | ||
options.contentHandlers['application/xml'] = | ||
function(body, req, res, callback) { | ||
var parser = new xml2js.Parser(); | ||
parser.addListener('end', function(result) { | ||
if (!result) | ||
result = {}; | ||
Object.keys(result).forEach(function(k) { | ||
try { | ||
if (typeof(result[k]) === 'object' && | ||
typeof(result[k]['@']) === 'object' && | ||
result[k]['@'].type && | ||
result[k]['#']) { | ||
switch (result[k]['@'].type) { | ||
case 'integer': | ||
result[k] = parseInt(result[k]['#'], 10); | ||
break; | ||
case 'boolean': | ||
result[k] = /^true$/i.test(result[k]['#']); | ||
break; | ||
default: | ||
result[k] = result[k]['#']; | ||
break; } | ||
} | ||
} catch (e) {} | ||
}); | ||
if (result.id && typeof(result.id) === 'object') | ||
delete result.id; | ||
return callback(null, result); | ||
}); | ||
parser.parseString(body); | ||
}; | ||
if (!options.contentWriters['application/xml']) { | ||
options.contentWriters['application/xml'] = | ||
function(obj) { | ||
assert.equal(typeof(obj), 'object'); | ||
var res = '<?xml version="1.0" encoding="UTF-8"?>\n'; | ||
function serialize(key, val, indent) { | ||
var str = ''; | ||
switch (typeof(val)) { | ||
case 'string': | ||
case 'boolean': | ||
str += sprintf('%s<%s>%s</%s>\n', indent, key, val + '', key); | ||
break; | ||
case 'number': | ||
str += sprintf('%s<%s type="integer">%s</%s>\n', | ||
indent, key, val + '', key); | ||
break; | ||
case 'object': | ||
if (Array.isArray(val)) { | ||
val.forEach(function(v) { | ||
str += serialize(key, v, indent + ' '); | ||
}); | ||
} else if (val === null) { | ||
str += sprintf('%s<%s/>\n', indent, key); | ||
} else { | ||
str += sprintf('%s<%s>\n', indent, key); | ||
Object.keys(val).forEach(function(k) { | ||
str += serialize(k, val[k], indent + ' '); | ||
}); | ||
str += sprintf('%s</%s>\n', indent, key); | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
return str; | ||
} | ||
Object.keys(obj).forEach(function(key) { | ||
res += serialize(key, obj[key], ''); | ||
}); | ||
return res; | ||
}; | ||
} | ||
if (!options.contentWriters['application/javascript']) | ||
@@ -715,0 +809,0 @@ options.contentWriters['application/javascript'] = |
@@ -5,7 +5,5 @@ // Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved. | ||
var newError = require('./error').newError; | ||
var error = require('./error'); | ||
var log = require('./log'); | ||
var HttpCodes = require('./http_codes'); | ||
var RestCodes = require('./rest_codes'); | ||
var sprintf = require('./sprintf').sprintf; | ||
var utils = require('./utils'); | ||
@@ -15,2 +13,9 @@ | ||
///--- Globals | ||
var MSG = 'You have exceeded your request rate of %s r/s.'; | ||
///--- Internal Class (TokenBucket) | ||
@@ -214,8 +219,3 @@ | ||
req.url); | ||
res.sendError(newError({ | ||
httpCode: HttpCodes.Throttle, | ||
restCode: RestCodes.RequestThrottled, | ||
message: 'You have exceeded your request rate of ' + rate + ' r/s.' | ||
})); | ||
return next(); | ||
return next(new error.RequestThrottledError(sprintf(MSG, rate))); | ||
} | ||
@@ -222,0 +222,0 @@ |
{ | ||
"name": "restify", | ||
"description": "REST framework specifically meant for web service APIs", | ||
"version": "0.5.1", | ||
"version": "0.5.3", | ||
"repository": { | ||
@@ -16,3 +16,4 @@ "type": "git", | ||
"retry": "0.4.0", | ||
"semver": "~1.0.8" | ||
"semver": "~1.0.8", | ||
"xml2js": "0.1.10" | ||
}, | ||
@@ -19,0 +20,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
230336
4408
6
+ Addedxml2js@0.1.10
+ Addedsax@1.4.1(transitive)
+ Addedxml2js@0.1.10(transitive)