From a266c64130e20d7f0f6c4ca04784c44bac86d2a6 Mon Sep 17 00:00:00 2001 From: Naz Date: Wed, 14 Jul 2021 17:14:56 +0400 Subject: [PATCH] Removed use of deprecated new Error() syntax refs https://github.com/TryGhost/Ghost/commit/2f1123d6ca7894fc21499739f46f0ae5c9eda73a refs https://github.com/TryGhost/Ghost/commit/6f1a3e17749e878a379830f12672b266c15bc835 - As per refed commits, we are removing deprecated use of `new Error()` in the codebase --- core/server/data/migrations/utils.js | 50 +++++++++++---- test/unit/data/migrations/utils.test.js | 82 +++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 12 deletions(-) diff --git a/core/server/data/migrations/utils.js b/core/server/data/migrations/utils.js index cbbbc58393..b7f6e6f788 100644 --- a/core/server/data/migrations/utils.js +++ b/core/server/data/migrations/utils.js @@ -1,10 +1,16 @@ const ObjectId = require('bson-objectid').default; const logging = require('@tryghost/logging'); +const errors = require('@tryghost/errors'); +const tpl = require('@tryghost/tpl'); const commands = require('../schema').commands; const Promise = require('bluebird'); const MIGRATION_USER = 1; +const messages = { + permissionRoleActionError: 'Cannot {action} permission({permission}) with role({role}) - {resource} does not exist' +}; + /** * Creates a migrations which will add a new table from schema.js to the database * @param {string} name - table name @@ -138,9 +144,14 @@ function addPermissionToRole(config) { }).first(); if (!permission) { - throw new Error( - `Cannot add permission(${config.permission}) to role(${config.role}) - permission does not exist` - ); + throw new errors.GhostError({ + message: tpl(messages.permissionRoleActionError, { + action: 'add', + permission: config.permission, + role: config.role, + resource: 'permission' + }) + }); } const role = await connection('roles').where({ @@ -148,9 +159,14 @@ function addPermissionToRole(config) { }).first(); if (!role) { - throw new Error( - `Cannot add permission(${config.permission}) to role(${config.role}) - role does not exist` - ); + throw new errors.GhostError({ + message: tpl(messages.permissionRoleActionError, { + action: 'add', + permission: config.permission, + role: config.role, + resource: 'role' + }) + }); } const existingRelation = await connection('permissions_roles').where({ @@ -176,9 +192,14 @@ function addPermissionToRole(config) { }).first(); if (!permission) { - throw new Error( - `Cannot remove permission(${config.permission}) from role(${config.role}) - permission does not exist` - ); + throw new errors.GhostError({ + message: tpl(messages.permissionRoleActionError, { + action: 'remove', + permission: config.permission, + role: config.role, + resource: 'permission' + }) + }); } const role = await connection('roles').where({ @@ -186,9 +207,14 @@ function addPermissionToRole(config) { }).first(); if (!role) { - throw new Error( - `Cannot remove permission(${config.permission}) from role(${config.role}) - role does not exist` - ); + throw new errors.GhostError({ + message: tpl(messages.permissionRoleActionError, { + action: 'remove', + permission: config.permission, + role: config.role, + resource: 'role' + }) + }); } const existingRelation = await connection('permissions_roles').where({ diff --git a/test/unit/data/migrations/utils.test.js b/test/unit/data/migrations/utils.test.js index 01e1103b7e..b8a0f12fcb 100644 --- a/test/unit/data/migrations/utils.test.js +++ b/test/unit/data/migrations/utils.test.js @@ -1,5 +1,6 @@ const should = require('should'); const sinon = require('sinon'); +const errors = require('@tryghost/errors'); const utils = require('../../../../core/server/data/migrations/utils'); @@ -359,6 +360,87 @@ describe('migrations/utils/permissions', function () { should.ok(!attachedPermissionAfterDown, 'The permission was removed from the role.'); }); + + describe('Throws errors', function () { + it('Throws when permission cannot be found in up migration', async function () { + const knex = await setupDb(); + + const migration = utils.addPermissionToRole({ + permission: 'Unimaginable', + role: 'Not there' + }); + + let runDownMigration; + try { + runDownMigration = await runUpMigration(knex, migration); + should.fail('addPermissionToRole up migration did not throw'); + } catch (err) { + should.equal(err instanceof errors.GhostError, true); + err.message.should.equal('Cannot add permission(Unimaginable) with role(Not there) - permission does not exist'); + } + }); + + it('Throws when permission cannot be found in down migration', async function () { + const knex = await setupDb(); + + const migration = utils.addPermissionToRole({ + permission: 'Permission Name', + role: 'Role Name' + }); + + const runDownMigration = await runUpMigration(knex, migration); + await knex('permissions') + .where('name', '=', 'Permission Name') + .del(); + + try { + await runDownMigration(knex, migration); + should.fail('addPermissionToRole down migration did not throw'); + } catch (err) { + should.equal(err instanceof errors.GhostError, true); + err.message.should.equal('Cannot remove permission(Permission Name) with role(Role Name) - permission does not exist'); + } + }); + + it('Throws when role cannot be found', async function () { + const knex = await setupDb(); + + const migration = utils.addPermissionToRole({ + permission: 'Permission Name', + role: 'Not there' + }); + + try { + await runUpMigration(knex, migration); + should.fail('addPermissionToRole did not throw'); + } catch (err) { + should.equal(err instanceof errors.GhostError, true); + err.message.should.equal('Cannot add permission(Permission Name) with role(Not there) - role does not exist'); + } + }); + + it('Throws when role cannot be found in down migration', async function () { + const knex = await setupDb(); + + const migration = utils.addPermissionToRole({ + permission: 'Permission Name', + role: 'Role Name' + }); + + const runDownMigration = await runUpMigration(knex, migration); + await knex('roles') + .where('name', '=', 'Role Name') + .del(); + + try { + await runDownMigration(knex, migration); + should.fail('addPermissionToRole down migration did not throw'); + } catch (err) { + should.equal(err instanceof errors.GhostError, true); + err.message.should.equal('Cannot remove permission(Permission Name) with role(Role Name) - role does not exist'); + } + }); + }); }); describe('addPermissionWithRoles', function () {