mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
Replaced usage of Error with @tryghost/errors (#13161)
refs 2f1123d6ca
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.
This commit is contained in:
parent
23748c92fb
commit
eca3ae1af3
1 changed files with 11 additions and 4 deletions
|
@ -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,
|
||||
|
|
Loading…
Add table
Reference in a new issue