From eca3ae1af3113a68257a2b219a3a30ef951c385f Mon Sep 17 00:00:00 2001 From: Fabien 'egg' O'Carroll Date: Mon, 19 Jul 2021 11:46:38 +0100 Subject: [PATCH] Replaced usage of Error with @tryghost/errors (#13161) refs https://github.com/TryGhost/Ghost/commit/2f1123d6ca7894fc21499739f46f0ae5c9eda73a Usage of the raw Error class has been deprecated in favour of our own errors, which are more descriptive and have built in HTTP status codes. This also updates the same errors to use @tryghost/tpl for the error messages, which is the new pattern we are following in order for us to deprecate the i18n module. --- ghost/members-importer/lib/importer.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/ghost/members-importer/lib/importer.js b/ghost/members-importer/lib/importer.js index d460f95ed9..83180ad963 100644 --- a/ghost/members-importer/lib/importer.js +++ b/ghost/members-importer/lib/importer.js @@ -2,6 +2,8 @@ const moment = require('moment-timezone'); const path = require('path'); const fs = require('fs-extra'); const membersCSV = require('@tryghost/members-csv'); +const errors = require('@tryghost/errors'); +const tpl = require('@tryghost/tpl'); const GhostMailer = require('../../mail').GhostMailer; const urlUtils = require('../../../../shared/url-utils'); const db = require('../../../data/db'); @@ -9,6 +11,11 @@ const emailTemplate = require('./email-template'); const jobsService = require('../../jobs'); const labsService = require('../../../../shared/labs'); +const messages = { + filenameCollision: 'Filename already exists, please try again.', + jobAlreadyComplete: 'Job is already complete.' +}; + const ghostMailer = new GhostMailer(); module.exports = class MembersCSVImporter { /** @@ -71,7 +78,7 @@ module.exports = class MembersCSVImporter { const pathExists = await fs.pathExists(outputFilePath); if (pathExists) { - throw new Error('Maybe we need better name generation'); + throw new errors.DataImportError(tpl(messages.filenameCollision)); } const rows = await membersCSV.parse(inputFilePath, headerMapping, defaultLabels); @@ -102,7 +109,7 @@ module.exports = class MembersCSVImporter { const job = await this.getJob(id); if (job.status === 'complete') { - throw new Error('Job is already complete'); + throw new errors.BadRequestError(tpl(messages.jobAlreadyComplete)); } const rows = membersCSV.parse(job.filename); @@ -178,8 +185,8 @@ module.exports = class MembersCSVImporter { }; } catch (error) { // The model layer can sometimes throw arrays of errors - const errors = [].concat(error); - const errorMessage = errors.map(({message}) => message).join(', '); + const errorList = [].concat(error); + const errorMessage = errorList.map(({message}) => message).join(', '); await trx.rollback(); return { ...resultAccumulator,