Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Error creation #7477

Merged
merged 6 commits into from
Oct 6, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 additions & 0 deletions core/server/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
var _ = require('lodash'),
util = require('util');

function GhostError(options) {
options = options || {};

if (_.isString(options)) {
throw new Error('Please instantiate Errors with the option pattern. e.g. new errors.GhostError({message: ...})');
}

Error.call(this);
Error.captureStackTrace(this, GhostError);

/**
* defaults
*/
this.statusCode = 500;
this.errorType = 'InternalServerError';
this.level = 'normal';

This comment was marked as abuse.


/**
* custom overrides
*/
this.statusCode = options.statusCode || this.statusCode;
this.level = options.level || this.level;
this.context = options.context || this.context;
this.help = options.help || this.help;
this.errorType = this.name = options.errorType || this.errorType;
this.errorDetails = options.errorDetails;

// @TODO: ?
this.property = options.property;
this.value = options.value;

this.message = options.message;
this.hideStack = options.hideStack;

// error to inherit from, override!
if (options.err) {
this.message = options.err.message;
this.stack = options.err.stack;
}
}

// jscs:disable
var errors = {
DataExportError: function DataExportError(options) {
GhostError.call(this, _.merge({
statusCode: 500,
errorType: 'DataExportError'
}, options));
},
DataImportError: function DataImportError(options) {
GhostError.call(this, _.merge({
statusCode: 500,
errorType: 'DataImportError'
}, options));
},
IncorrectUsageError: function IncorrectUsageError(options) {
GhostError.call(this, _.merge({
statusCode: 400,
level: 'critical',
errorType: 'IncorrectUsageError'
}, options));
},
NotFoundError: function NotFoundError(options) {
GhostError.call(this, _.merge({
statusCode: 404,
errorType: 'NotFoundError'
}, options));
},
BadRequestError: function BadRequestError(options) {
GhostError.call(this, _.merge({
statusCode: 400,
errorType: 'BadRequestError'
}, options));
},
DatabaseVersionError: function DatabaseVersionError(options) {
GhostError.call(this, _.merge({
hideStack: true,
statusCode: 500,
errorType: 'DatabaseVersionError'
}, options));
},
DatabaseNotPopulatedError: function DatabaseNotPopulatedError(options) {
GhostError.call(this, _.merge({
statusCode: 500,
errorType: 'DatabaseNotPopulatedError'
}, options));
},
UnauthorizedError: function UnauthorizedError(options) {
GhostError.call(this, _.merge({
statusCode: 401,
errorType: 'UnauthorizedError'
}, options));
},
NoPermissionError: function NoPermissionError(options) {
GhostError.call(this, _.merge({
statusCode: 403,
errorType: 'NoPermissionError'
}, options));
},
ValidationError: function ValidationError(options) {
GhostError.call(this, _.merge({
statusCode: 422,
errorType: 'ValidationError'
}, options));
},
UnsupportedMediaTypeError: function UnsupportedMediaTypeError(options) {
GhostError.call(this, _.merge({
statusCode: 415,
errorType: 'UnsupportedMediaTypeError'
}, options));
},
VersionMismatchError: function VersionMismatchError(options) {
GhostError.call(this, _.merge({
statusCode: 400,
errorType: 'VersionMismatchError'
}, options));
},
TokenRevocationError: function TokenRevocationError(options) {
GhostError.call(this, _.merge({
statusCode: 503,
errorType: 'TokenRevocationError'
}, options));
},
EmailError: function EmailError(options) {
GhostError.call(this, _.merge({
statusCode: 500,
errorType: 'EmailError'
}, options));
},
TooManyRequestsError: function TooManyRequestsError(options) {
GhostError.call(this, _.merge({
statusCode: 429,
errorType: 'TooManyRequestsError'
}, options));
},
MaintenanceError: function MaintenanceError(options) {
GhostError.call(this, _.merge({
statusCode: 503,
errorType: 'MaintenanceError'
}, options));
}
};

_.each(errors, function (error) {
util.inherits(error, GhostError);
});

module.exports = errors;
module.exports.GhostError = GhostError;