0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Removed usages of new Error & i18n in legacy code

- cleaning up a handful of usages of i18n and new Error in some really old codepaths
- pushing our new patterns forward wherever we can
This commit is contained in:
Hannah Wolfe 2021-07-01 12:37:32 +01:00
parent b94c8bcfd4
commit 6f1a3e1774
No known key found for this signature in database
GPG key ID: 9F8C7532D0A6BA55
4 changed files with 28 additions and 21 deletions

View file

@ -2,10 +2,16 @@ const _ = require('lodash');
const Promise = require('bluebird');
const models = require('../../models');
const errors = require('@tryghost/errors');
const i18n = require('../../../shared/i18n');
const tpl = require('@tryghost/tpl');
const providers = require('./providers');
const parseContext = require('./parse-context');
const actionsMap = require('./actions-map-cache');
const messages = {
noPermissionToAction: 'You do not have permission to perform this action',
noActionsMapFoundError: 'No actions map found, ensure you have loaded permissions into database and then call permissions.init() before use.'
};
let canThis;
let CanThisResult;
@ -104,7 +110,7 @@ CanThisResult.prototype.buildObjectTypeHandlers = function (objTypes, actType, c
return;
}
return Promise.reject(new errors.NoPermissionError({message: i18n.t('errors.permissions.noPermissionToAction')}));
return Promise.reject(new errors.NoPermissionError({message: tpl(messages.noPermissionToAction)}));
});
};
@ -122,7 +128,7 @@ CanThisResult.prototype.beginCheck = function (context) {
context = parseContext(context);
if (actionsMap.empty()) {
throw new Error(i18n.t('errors.permissions.noActionsMapFound.error'));
throw new errors.GhostError({message: tpl(messages.noActionsMapFoundError)});
}
// Kick off loading of user permissions if necessary

View file

@ -1,6 +1,6 @@
const errors = require('@tryghost/errors');
const events = require('../lib/common/events');
const i18n = require('../../shared/i18n');
const tpl = require('@tryghost/tpl');
const logging = require('@tryghost/logging');
const request = require('@tryghost/request');
const {blogIcon} = require('../lib/image');
@ -10,6 +10,11 @@ const settingsCache = require('../../shared/settings-cache');
const schema = require('../data/schema').checks;
const moment = require('moment');
const messages = {
requestFailedError: 'The {service} service was unable to send a ping request, your site will continue to function.',
requestFailedHelp: 'If you get this error repeatedly, please seek help on {url}.'
};
const defaultPostSlugs = [
'welcome',
'the-editor',
@ -131,8 +136,8 @@ function ping(post) {
}).catch(function (err) {
logging.error(new errors.GhostError({
err: err,
context: i18n.t('errors.services.ping.requestFailed.error', {service: 'slack'}),
help: i18n.t('errors.services.ping.requestFailed.help', {url: 'https://ghost.org/docs/'})
context: tpl(messages.requestFailedError, {service: 'slack'}),
help: tpl(messages.requestFailedHelp, {url: 'https://ghost.org/docs/'})
}));
});
}

View file

@ -4,12 +4,17 @@ const config = require('../../shared/config');
const urlService = require('../../frontend/services/url');
const errors = require('@tryghost/errors');
const events = require('../lib/common/events');
const i18n = require('../../shared/i18n');
const tpl = require('@tryghost/tpl');
const logging = require('@tryghost/logging');
const request = require('@tryghost/request');
const settingsCache = require('../../shared/settings-cache');
const sentry = require('../../shared/sentry');
const messages = {
requestFailedError: 'The {service} service was unable to send a ping request, your site will continue to function.',
requestFailedHelp: 'If you get this error repeatedly, please seek help on {url}.'
};
const defaultPostSlugs = [
'welcome',
'the-editor',
@ -80,7 +85,7 @@ function ping(post) {
if (!goodResponse.test(res.body)) {
const matches = res.body.match(errorMessage);
const message = matches ? matches[1] : res.body;
throw new Error(message);
throw new errors.GhostError({message});
}
})
.catch(function (err) {
@ -89,15 +94,15 @@ function ping(post) {
error = new errors.TooManyRequestsError({
err,
message: err.message,
context: i18n.t('errors.services.ping.requestFailed.error', {service: 'xmlrpc'}),
help: i18n.t('errors.services.ping.requestFailed.help', {url: 'https://ghost.org/docs/'})
context: tpl(messages.requestFailedError, {service: 'xmlrpc'}),
help: tpl(messages.requestFailedHelp, {url: 'https://ghost.org/docs/'})
});
} else {
error = new errors.GhostError({
err: err,
message: err.message,
context: i18n.t('errors.services.ping.requestFailed.error', {service: 'xmlrpc'}),
help: i18n.t('errors.services.ping.requestFailed.help', {url: 'https://ghost.org/docs/'})
context: tpl(messages.requestFailedError, {service: 'xmlrpc'}),
help: tpl(messages.requestFailedHelp, {url: 'https://ghost.org/docs/'})
});
}
logging.error(error);

View file

@ -271,9 +271,6 @@
}
},
"permissions": {
"noActionsMapFound": {
"error": "No actions map found, ensure you have loaded permissions into database and then call permissions.init() before use."
},
"applyStatusRules": {
"error": "You do not have permission to retrieve {docName} with that status"
},
@ -561,12 +558,6 @@
}
},
"services": {
"ping": {
"requestFailed": {
"error": "The {service} service was unable to send a ping request, your site will continue to function.",
"help": "If you get this error repeatedly, please seek help on {url}."
}
},
"settings": {
"yaml": {
"error": "Could not parse {file}: {context}.",