2013-10-22 09:29:57 +02:00
|
|
|
var util = require('util')
|
|
|
|
, utils = require('./utils')
|
2013-06-01 00:57:28 +02:00
|
|
|
|
2014-07-22 21:31:01 +02:00
|
|
|
function parse_error_params(params, status, message) {
|
2013-06-01 00:57:28 +02:00
|
|
|
if (typeof(params) === 'string') {
|
|
|
|
return {
|
2014-07-22 21:31:01 +02:00
|
|
|
message: params,
|
2013-06-01 00:57:28 +02:00
|
|
|
status: status,
|
2013-10-26 14:18:36 +02:00
|
|
|
}
|
2013-06-01 00:57:28 +02:00
|
|
|
} else if (typeof(params) === 'number') {
|
|
|
|
return {
|
2014-07-22 21:31:01 +02:00
|
|
|
message: message,
|
2013-06-01 00:57:28 +02:00
|
|
|
status: params,
|
2013-10-26 14:18:36 +02:00
|
|
|
}
|
2013-10-22 09:29:57 +02:00
|
|
|
} else if (utils.is_object(params)) {
|
2014-07-22 21:31:01 +02:00
|
|
|
if (params.message == null) params.message = message
|
2013-10-26 14:18:36 +02:00
|
|
|
if (params.status == null) params.status = status
|
|
|
|
return params
|
2013-06-01 00:57:28 +02:00
|
|
|
} else {
|
|
|
|
return {
|
2014-07-22 21:31:01 +02:00
|
|
|
message: message,
|
2013-06-01 00:57:28 +02:00
|
|
|
status: status,
|
2013-10-26 14:18:36 +02:00
|
|
|
}
|
2013-06-01 00:57:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Errors caused by malfunctioned code
|
|
|
|
*/
|
|
|
|
var AppError = function(params, constr) {
|
2013-10-26 14:18:36 +02:00
|
|
|
Error.captureStackTrace(this, constr || this)
|
|
|
|
params = parse_error_params(params, 500, 'Internal server error')
|
2014-07-22 21:31:01 +02:00
|
|
|
this.message = params.message
|
2013-10-26 14:18:36 +02:00
|
|
|
this.status = params.status
|
|
|
|
}
|
|
|
|
util.inherits(AppError, Error)
|
|
|
|
AppError.prototype.name = 'Application Error'
|
2013-06-01 00:57:28 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Errors caused by wrong request
|
|
|
|
*/
|
|
|
|
var UserError = function(params, constr) {
|
2013-10-26 14:18:36 +02:00
|
|
|
params = parse_error_params(params, 404, 'The requested resource was not found')
|
2014-07-22 21:31:01 +02:00
|
|
|
this.message = params.message
|
2013-10-26 14:18:36 +02:00
|
|
|
this.status = params.status
|
|
|
|
}
|
|
|
|
util.inherits(UserError, Error)
|
|
|
|
UserError.prototype.name = 'User Error'
|
2013-06-01 00:57:28 +02:00
|
|
|
|
2013-06-20 15:07:34 +02:00
|
|
|
/*
|
|
|
|
* Mimic filesystem errors
|
|
|
|
*/
|
|
|
|
var FSError = function(code) {
|
2013-10-26 14:18:36 +02:00
|
|
|
this.code = code
|
|
|
|
}
|
|
|
|
util.inherits(UserError, Error)
|
|
|
|
UserError.prototype.name = 'FS Error'
|
2013-06-20 15:07:34 +02:00
|
|
|
|
2013-10-26 14:18:36 +02:00
|
|
|
module.exports.AppError = AppError
|
|
|
|
module.exports.UserError = UserError
|
|
|
|
module.exports.FSError = FSError
|
2013-06-01 00:57:28 +02:00
|
|
|
|