mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-28 14:03:48 +03:00
Removed use of deprecated new Error() syntax
refs2f1123d6ca
refs6f1a3e1774
- As per refed commits, we are removing deprecated use of `new Error()` in the codebase
This commit is contained in:
parent
798bbd5320
commit
a266c64130
@ -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({
|
||||
|
@ -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 () {
|
||||
|
Loading…
Reference in New Issue
Block a user