From e8c5a3bc2664536e9a8fe934d64c5c19116fc423 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Mon, 4 Oct 2021 09:37:34 +0100 Subject: [PATCH] Replaced i18n.t w/ tpl in error handler middleware refs: #13380 - This is part of the ongoing push to get rid of the deprecated i18n.t calls - In this case, it highlights just how little work we've done on API errors - we should have a full list of action messages, but there's just 1 :( --- .../web/shared/middlewares/error-handler.js | 56 ++++++++++++++++--- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/core/server/web/shared/middlewares/error-handler.js b/core/server/web/shared/middlewares/error-handler.js index bd9acedd08..83133e4683 100644 --- a/core/server/web/shared/middlewares/error-handler.js +++ b/core/server/web/shared/middlewares/error-handler.js @@ -2,11 +2,49 @@ const hbs = require('express-hbs'); const _ = require('lodash'); const debug = require('@tryghost/debug')('error-handler'); const errors = require('@tryghost/errors'); +const tpl = require('@tryghost/tpl'); const config = require('../../../../shared/config'); -const i18n = require('../../../../shared/i18n'); const helpers = require('../../../../frontend/services/routing/helpers'); const sentry = require('../../../../shared/sentry'); +const messages = { + oopsErrorTemplateHasError: 'Oops, seems there is an error in the error template.', + encounteredError: 'Encountered the error: ', + whilstTryingToRender: 'whilst trying to render an error page for the error: ', + pageNotFound: 'Page not found', + resourceNotFound: 'Resource not found', + actions: { + images: { + upload: 'upload image' + } + }, + userMessages: { + BookshelfRelationsError: 'Database error, cannot {action}.', + InternalServerError: 'Internal server error, cannot {action}.', + IncorrectUsageError: 'Incorrect usage error, cannot {action}.', + NotFoundError: 'Resource not found error, cannot {action}.', + BadRequestError: 'Request not understood error, cannot {action}.', + UnauthorizedError: 'Authorisation error, cannot {action}.', + NoPermissionError: 'Permission error, cannot {action}.', + ValidationError: 'Validation error, cannot {action}.', + UnsupportedMediaTypeError: 'Unsupported media error, cannot {action}.', + TooManyRequestsError: 'Too many requests error, cannot {action}.', + MaintenanceError: 'Server down for maintenance, cannot {action}.', + MethodNotAllowedError: 'Method not allowed, cannot {action}.', + RequestEntityTooLargeError: 'Request too large, cannot {action}.', + TokenRevocationError: 'Token is not available, cannot {action}.', + VersionMismatchError: 'Version mismatch error, cannot {action}.', + DataExportError: 'Error exporting content.', + DataImportError: 'Duplicated entry, cannot save {action}.', + DatabaseVersionError: 'Database version compatibility error, cannot {action}.', + EmailError: 'Error sending email!', + ThemeValidationError: 'Theme validation error, cannot {action}.', + HostLimitError: 'Host Limit error, cannot {action}.', + DisabledFeatureError: 'Theme validation error, the {{{helperName}}} helper is not available. Cannot {action}.', + UpdateCollisionError: 'Saving failed! Someone else is editing this post.' + } +}; + const escapeExpression = hbs.Utils.escapeExpression; const _private = {}; const errorHandler = {}; @@ -105,8 +143,8 @@ _private.prepareUserMessage = (err, res) => { destroy: 'delete' }; - if (i18n.doesTranslationKeyExist(`common.api.actions.${docName}.${method}`)) { - action = i18n.t(`common.api.actions.${docName}.${method}`); + if (_.get(messages.actions, [docName, method])) { + action = tpl(messages.actions[docName][method]); } else if (Object.keys(actionMap).includes(method)) { let resource = docName; @@ -124,7 +162,7 @@ _private.prepareUserMessage = (err, res) => { userError.context = err.message; } - userError.message = i18n.t(`errors.api.userMessages.${err.name}`, {action: action}); + userError.message = tpl(messages.userMessages[err.name], {action: action}); } } @@ -148,10 +186,10 @@ _private.JSONErrorRendererV2 = (err, req, res, next) => { // eslint-disable-line }); }; -_private.ErrorFallbackMessage = err => `

${i18n.t('errors.errors.oopsErrorTemplateHasError')}

-

${i18n.t('errors.errors.encounteredError')}

+_private.ErrorFallbackMessage = err => `

${tpl(messages.oopsErrorTemplateHasError)}

+

${tpl(messages.encounteredError)}

${escapeExpression(err.message || err)}
-

${i18n.t('errors.errors.whilstTryingToRender')}

+

${tpl(messages.whilstTryingToRender)}

${err.statusCode}
${escapeExpression(err.message || err)}
`; _private.ThemeErrorRenderer = (err, req, res, next) => { @@ -241,11 +279,11 @@ _private.BasicErrorRenderer = (err, req, res, next) => { // eslint-disable-line errorHandler.resourceNotFound = (req, res, next) => { // TODO, handle unknown resources & methods differently, so that we can also produce // 405 Method Not Allowed - next(new errors.NotFoundError({message: i18n.t('errors.errors.resourceNotFound')})); + next(new errors.NotFoundError({message: tpl(messages.resourceNotFound)})); }; errorHandler.pageNotFound = (req, res, next) => { - next(new errors.NotFoundError({message: i18n.t('errors.errors.pageNotFound')})); + next(new errors.NotFoundError({message: tpl(messages.pageNotFound)})); }; errorHandler.handleJSONResponse = [