Ghost/core/server/errors.js

179 lines
5.4 KiB
JavaScript
Raw Normal View History

var _ = require('lodash'),
uuid = require('node-uuid'),
util = require('util');
function GhostError(options) {
options = options || {};
var self = this;
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.id = uuid.v1();
/**
* 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;
✨ migrations: seeding is part of init db task (#7545) * 🎨 move heart of fixtures to schema folder and change user model - add fixtures.json to schema folder - add fixture utils to schema folder - keep all the logic! --> FIXTURE.JSON - add owner user with roles --> USER MODEL - add password as default - findAll: allow querying inactive users when internal context (defaultFilters) - findOne: do not remove values from original object! - add: do not remove values from original object! * 🔥 remove migrations key from default_settings.json - this was a temporary invention for an older migration script - sephiroth keep alls needed information in a migration collection * 🔥 add code property to errors - add code property to errors - IMPORTANT: please share your opinion about that - this is a copy paste behaviour of how node is doing that (errno, code etc.) - so code specifies a GhostError * 🎨 change error handling in versioning - no need to throw specific database errors anymore (this was just a temporary solution) - now: we are throwing real DatabaseVersionErrors - specified by a code - background: the versioning unit has not idea about seeding and population of the database - it just throws what it knows --> database version does not exist or settings table does not exist * 🎨 sephiroth optimisations - added getPath function to get the path to init scripts and migration scripts - migrationPath is still hardcoded (see TODO) - tidy up database naming to transacting * ✨ migration init scripts are now complete - 1. add tables - 2. add fixtures - 3. add default settings * 🎨 important: make bootup script smaller! - remove all TODO'S except of one - no seeding logic in bootup script anymore 🕵🏻 * ✨ sephiroth: allow params for init command - param: skip (do not run this script) - param: only (only run this script) - very simple way * 🎨 adapt tests and test env - do not use migrate.populate anymore - use sephiroth instead - jscs/jshint * 🎨 fix User model status checks
2016-10-12 18:18:57 +03:00
this.code = options.code || null;
// @TODO: ?
this.property = options.property;
this.value = options.value;
this.message = options.message;
this.hideStack = options.hideStack;
// error to inherit from, override!
// nested objects are getting copied over in one piece (can be changed, but not needed right now)
if (options.err) {
Object.getOwnPropertyNames(options.err).forEach(function (property) {
if (['errorType', 'name', 'statusCode'].indexOf(property) !== -1) {
return;
}
self[property] = options.err[property] || self[property];
});
}
}
// 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));
},
DatabaseNotSeededError: function DatabaseNotSeededError(options) {
GhostError.call(this, _.merge({
statusCode: 500,
errorType: 'DatabaseNotSeededError'
}, 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));
},
ThemeValidationError: function ThemeValidationError(options) {
GhostError.call(this, _.merge({
statusCode: 422,
errorType: 'ThemeValidationError',
errorDetails: {}
}, options));
}
};
util.inherits(GhostError, Error);
_.each(errors, function (error) {
util.inherits(error, GhostError);
});
module.exports = errors;
module.exports.GhostError = GhostError;