0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-17 23:44:39 -05:00

Switch to ES6 classes for errors, use @tryghost/ignition errors

no issue

Replaces the usage of ghost-ignition with @tryghost/ignition-errors,
and switched to using the more modern ES6 class syntax over the
`.call(this, ...)` syntax used previously in both Ignition and here.
This commit is contained in:
Sam Lord 2021-06-17 10:17:55 +01:00
parent e8f7a4c158
commit 095d64b449
2 changed files with 85 additions and 69 deletions

View file

@ -1,87 +1,103 @@
const merge = require('lodash/merge'), const merge = require('lodash/merge');
each = require('lodash/each'), const each = require('lodash/each');
util = require('util'), const util = require('util');
errors = require('ghost-ignition').errors; const errors = require('@tryghost/ignition-errors');
function GhostError(options) { class GhostError extends errors.IgnitionError {
options = options || {}; constructor(options) {
this.value = options.value; options = options || {};
super(options);
errors.IgnitionError.call(this, options); this.value = options.value;
}
} }
const ghostErrors = { const ghostErrors = {
DataExportError: function DataExportError(options) { DataExportError: class DataExportError extends GhostError {
GhostError.call(this, merge({ constructor(options) {
statusCode: 500, super(merge({
errorType: 'DataExportError' statusCode: 500,
}, options)); errorType: 'DataExportError'
}, options));
}
}, },
DataImportError: function DataImportError(options) { DataImportError: class DataImportError extends GhostError {
GhostError.call(this, merge({ constructor(options) {
statusCode: 500, super(merge({
errorType: 'DataImportError' statusCode: 500,
}, options)); errorType: 'DataImportError'
}, options));
}
}, },
DatabaseVersionError: function DatabaseVersionError(options) { DatabaseVersionError: class DatabaseVersionError extends GhostError {
GhostError.call(this, merge({ constructor(options) {
hideStack: true, super(merge({
statusCode: 500, hideStack: true,
errorType: 'DatabaseVersionError' statusCode: 500,
}, options)); errorType: 'DatabaseVersionError'
}, options));
}
}, },
EmailError: function EmailError(options) { EmailError: class EmailError extends GhostError {
GhostError.call(this, merge({ constructor(options) {
statusCode: 500, super(merge({
errorType: 'EmailError' statusCode: 500,
}, options)); errorType: 'EmailError'
}, options));
}
}, },
ThemeValidationError: function ThemeValidationError(options) { ThemeValidationError: class ThemeValidationError extends GhostError {
GhostError.call(this, merge({ constructor(options) {
statusCode: 422, super(merge({
errorType: 'ThemeValidationError', statusCode: 422,
errorDetails: {} errorType: 'ThemeValidationError',
}, options)); errorDetails: {}
}, options));
}
}, },
DisabledFeatureError: function DisabledFeatureError(options) { DisabledFeatureError: class DisabledFeatureError extends GhostError {
GhostError.call(this, merge({ constructor(options) {
statusCode: 409, super(merge({
errorType: 'DisabledFeatureError' statusCode: 409,
}, options)); errorType: 'DisabledFeatureError'
}, options));
}
}, },
UpdateCollisionError: function UpdateCollisionError(options) { UpdateCollisionError: class UpdateCollisionError extends GhostError {
GhostError.call(this, merge({ constructor(options) {
statusCode: 409, super(merge({
errorType: 'UpdateCollisionError' statusCode: 409,
}, options)); errorType: 'UpdateCollisionError'
}, options));
}
}, },
HostLimitError: function HostLimitError(options) { HostLimitError: class HostLimitError extends GhostError {
GhostError.call(this, merge({ constructor(options) {
errorType: 'HostLimitError', super(merge({
hideStack: true, errorType: 'HostLimitError',
statusCode: 403 hideStack: true,
}, options)); statusCode: 403
}, options));
}
}, },
HelperWarning: function HelperWarning(options) { HelperWarning: class HelperWarning extends GhostError {
GhostError.call(this, merge({ constructor(options) {
errorType: 'HelperWarning', super(merge({
hideStack: true errorType: 'HelperWarning',
}, options)); hideStack: true
}, options));
}
}, },
PasswordResetRequiredError: function PasswordResetRequiredError(options) { PasswordResetRequiredError: class PasswordResetRequiredError extends GhostError {
GhostError.call(this, merge({ constructor(options) {
errorType: 'PasswordResetRequiredError', super(merge({
statusCode: 401, errorType: 'PasswordResetRequiredError',
message: 'For security, you need to create a new password. An email has been sent to you with instructions!' statusCode: 401,
}, options)); message: 'For security, you need to create a new password. An email has been sent to you with instructions!'
}, options));
}
} }
}; };
util.inherits(GhostError, errors.IgnitionError);
each(ghostErrors, function (error) {
util.inherits(error, GhostError);
});
// we need to inherit all general errors from GhostError, otherwise we have to check instanceof IgnitionError // we need to inherit all general errors from GhostError, otherwise we have to check instanceof IgnitionError
each(errors, function (error) { each(errors, function (error) {
if (error.name === 'IgnitionError' || typeof error === 'object') { if (error.name === 'IgnitionError' || typeof error === 'object') {

View file

@ -24,7 +24,7 @@
"sinon": "11.0.0" "sinon": "11.0.0"
}, },
"dependencies": { "dependencies": {
"ghost-ignition": "^4.6.1", "@tryghost/ignition-errors": "^0.1.0",
"lodash": "^4.17.21" "lodash": "^4.17.21"
} }
} }