fxa-profile-server/lib/error.js

143 строки
3.0 KiB
JavaScript
Исходник Обычный вид История

2014-05-12 22:45:12 +04:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const util = require('util');
const DEFAULTS = {
code: 500,
error: 'Internal Server Error',
errno: 999,
message: 'Unspecified error'
};
function AppError(options, extra, headers) {
this.message = options.message || DEFAULTS.message;
this.isBoom = true;
this.stack = options.stack;
2014-07-25 03:11:23 +04:00
if (!this.stack) {
Error.captureStackTrace(this, AppError);
}
2014-05-12 22:45:12 +04:00
this.errno = options.errno || DEFAULTS.errno;
2014-07-25 03:11:23 +04:00
var code = options.code || DEFAULTS.code;
2014-05-12 22:45:12 +04:00
this.output = {
2014-07-25 03:11:23 +04:00
statusCode: code,
2014-05-12 22:45:12 +04:00
payload: {
2014-07-25 03:11:23 +04:00
code: code,
2014-05-12 22:45:12 +04:00
errno: this.errno,
error: options.error || DEFAULTS.error,
message: this.message,
info: options.info || DEFAULTS.info
},
headers: headers || {}
};
var keys = Object.keys(extra || {});
for (var i = 0; i < keys.length; i++) {
this.output.payload[keys[i]] = extra[keys[i]];
}
2014-07-25 03:11:23 +04:00
2014-05-12 22:45:12 +04:00
}
util.inherits(AppError, Error);
AppError.prototype.toString = function () {
return 'Error: ' + this.message;
};
AppError.prototype.header = function (name, value) {
this.output.headers[name] = value;
};
AppError.from = function from(obj) {
var err = new AppError(obj);
err.cause = obj.cause;
return err;
};
2014-05-12 22:45:12 +04:00
AppError.translate = function translate(response) {
if (response instanceof AppError) {
return response;
}
var error;
var payload = response.output.payload;
if (payload.validation) {
error = AppError.invalidRequestParameter(payload.validation);
} else {
error = new AppError({
message: payload.message,
code: payload.statusCode,
error: payload.error,
errno: payload.errno,
stack: response.stack
});
}
return error;
};
2014-08-23 03:33:58 +04:00
AppError.notFound = function notFound() {
return new AppError({
code: 404,
error: 'Bad Request',
message: 'Not Found'
});
};
2014-05-12 22:45:12 +04:00
AppError.unauthorized = function unauthorized(msg) {
return new AppError({
2014-08-23 03:33:58 +04:00
code: 401,
2014-05-12 22:45:12 +04:00
error: 'Bad Request',
errno: 100,
message: 'Unauthorized'
}, {
reason: msg
});
};
AppError.invalidRequestParameter = function invalidRequestParameter(val) {
return new AppError({
code: 400,
error: 'Bad Request',
errno: 101,
message: 'Invalid request parameter'
}, {
validation: val
});
};
2014-07-25 03:11:23 +04:00
AppError.unsupportedProvider = function unsupportedProvider(url) {
return new AppError({
code: 400,
error: 'Bad Request',
errno: 102,
message: 'Unsupported image provider'
}, {
url: url
});
};
AppError.processingError = function processingError(res) {
return new AppError({
code: 500,
error: 'Internal Server Error',
errno: 103,
message: 'Image processing error'
}, {
_res: res
});
};
2014-08-27 20:38:09 +04:00
AppError.oauthError = function oauthError(err) {
return new AppError({
code: 503,
error: 'Service Unavailable',
errno: 104,
message: 'OAuth server error'
2014-08-27 20:38:09 +04:00
}, {
cause: err
});
};
2014-05-12 22:45:12 +04:00
module.exports = AppError;