0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-17 23:44:39 -05:00

Fixed IncorrectUsageError initialization

no issue

- The error takes in an options object which should contain "message" property instead of a string
This commit is contained in:
Naz 2021-05-06 15:42:02 +04:00
parent 4f41c2a206
commit 1483a5c758
2 changed files with 11 additions and 6 deletions

View file

@ -44,11 +44,11 @@ class MaxLimit extends Limit {
super({name, error: config.error || '', helpLink, db, errors}); super({name, error: config.error || '', helpLink, db, errors});
if (config.max === undefined) { if (config.max === undefined) {
throw new errors.IncorrectUsageError('Attempted to setup a max limit without a limit'); throw new errors.IncorrectUsageError({message: 'Attempted to setup a max limit without a limit'});
} }
if (!config.currentCountQuery) { if (!config.currentCountQuery) {
throw new errors.IncorrectUsageError('Attempted to setup a max limit without a current count query'); throw new errors.IncorrectUsageError({message: 'Attempted to setup a max limit without a current count query'});
} }
this.currentCountQueryFn = config.currentCountQuery; this.currentCountQueryFn = config.currentCountQuery;
@ -210,7 +210,7 @@ class AllowlistLimit extends Limit {
super({name, error: config.error || '', helpLink, errors}); super({name, error: config.error || '', helpLink, errors});
if (!config.allowlist || !config.allowlist.length) { if (!config.allowlist || !config.allowlist.length) {
throw new this.errors.IncorrectUsageError('Attempted to setup an allowlist limit without an allowlist'); throw new this.errors.IncorrectUsageError({message: 'Attempted to setup an allowlist limit without an allowlist'});
} }
this.allowlist = config.allowlist; this.allowlist = config.allowlist;
@ -231,7 +231,7 @@ class AllowlistLimit extends Limit {
async errorIfWouldGoOverLimit(metadata) { async errorIfWouldGoOverLimit(metadata) {
if (!metadata.value) { if (!metadata.value) {
throw new this.errors.IncorrectUsageError('Attempted to check an allowlist limit without a value'); throw new this.errors.IncorrectUsageError({message: 'Attempted to check an allowlist limit without a value'});
} }
if (!this.allowlist.includes(metadata.value)) { if (!this.allowlist.includes(metadata.value)) {
throw this.generateError(); throw this.generateError();
@ -240,7 +240,7 @@ class AllowlistLimit extends Limit {
async errorIfIsOverLimit(metadata) { async errorIfIsOverLimit(metadata) {
if (!metadata.value) { if (!metadata.value) {
throw new this.errors.IncorrectUsageError('Attempted to check an allowlist limit without a value'); throw new this.errors.IncorrectUsageError({message: 'Attempted to check an allowlist limit without a value'});
} }
if (!this.allowlist.includes(metadata.value)) { if (!this.allowlist.includes(metadata.value)) {
throw this.generateError(); throw this.generateError();

View file

@ -55,11 +55,14 @@ describe('Limit Service', function () {
should.exist(err); should.exist(err);
should.exist(err.errorType); should.exist(err.errorType);
should.equal(err.errorType, 'IncorrectUsageError'); should.equal(err.errorType, 'IncorrectUsageError');
err.message.should.match(/max limit without a limit/);
} }
}); });
it('throws if initialized without a current count query', function () { it('throws if initialized without a current count query', function () {
const config = {}; const config = {
max: 100
};
try { try {
const limit = new MaxLimit({name: 'no accountability!', config, errors}); const limit = new MaxLimit({name: 'no accountability!', config, errors});
@ -68,6 +71,7 @@ describe('Limit Service', function () {
should.exist(err); should.exist(err);
should.exist(err.errorType); should.exist(err.errorType);
should.equal(err.errorType, 'IncorrectUsageError'); should.equal(err.errorType, 'IncorrectUsageError');
err.message.should.match(/max limit without a current count query/);
} }
}); });
}); });
@ -299,6 +303,7 @@ describe('Limit Service', function () {
throw new Error('Should have failed earlier...'); throw new Error('Should have failed earlier...');
} catch (error) { } catch (error) {
error.errorType.should.equal('IncorrectUsageError'); error.errorType.should.equal('IncorrectUsageError');
error.message.should.match(/allowlist limit without an allowlist/);
} }
}); });