-
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ability): adds possibility to specify custom error messages (#245)
Fixes #241
- Loading branch information
Showing
6 changed files
with
148 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Ability, ForbiddenError } from '../src' | ||
|
||
describe('`ForbiddenError` class', () => { | ||
let ability | ||
let error | ||
|
||
beforeEach(() => { | ||
ability = new Ability([ | ||
{ action: 'read', subject: 'Post' } | ||
]) | ||
error = ForbiddenError.from(ability) | ||
}) | ||
|
||
describe('`throwUnlessCan` method', () => { | ||
it('raises forbidden exception on disallowed action', () => { | ||
expect(() => error.throwUnlessCan('archive', 'Post')).to.throw(ForbiddenError) | ||
}) | ||
|
||
it('does not raise forbidden exception on allowed action', () => { | ||
expect(() => ability.throwUnlessCan('read', 'Post')).not.to.throw(ForbiddenError) | ||
}) | ||
|
||
it('raises error with context information', () => { | ||
let thrownError = new Error('No error raised') | ||
|
||
try { | ||
error.throwUnlessCan('archive', 'Post') | ||
} catch (abilityError) { | ||
thrownError = abilityError | ||
} | ||
|
||
expect(thrownError).to.have.property('action').that.equal('archive') | ||
expect(thrownError).to.have.property('subject').that.equal('Post') | ||
expect(thrownError).to.have.property('subjectName').that.equal('Post') | ||
}) | ||
|
||
it('raises error with message provided in `reason` field of forbidden rule', () => { | ||
const NO_CARD_MESSAGE = 'No credit card provided' | ||
ability.update([{ | ||
action: 'update', | ||
subject: 'Post', | ||
inverted: true, | ||
reason: NO_CARD_MESSAGE | ||
}]) | ||
|
||
expect(() => error.throwUnlessCan('update', 'Post')).to.throw(NO_CARD_MESSAGE) | ||
}) | ||
|
||
it('can raise error with custom message', () => { | ||
const message = 'My custom error message' | ||
expect(() => error.setMessage(message).throwUnlessCan('update', 'Post')).to.throw(message) | ||
}) | ||
}) | ||
|
||
describe('`setDefaultMessage` method', () => { | ||
afterEach(() => { | ||
ForbiddenError.setDefaultMessage(null) | ||
}) | ||
|
||
it('sets default message from function', () => { | ||
ForbiddenError.setDefaultMessage(err => `${err.action}-${err.subjectName}`) | ||
expect(() => error.throwUnlessCan('update', 'Post')).to.throw('update-Post') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,65 @@ | ||
export function ForbiddenError(message, options = {}) { | ||
Error.call(this); | ||
this.constructor = ForbiddenError; | ||
this.subject = options.subject; | ||
this.subjectName = options.subjectName; | ||
this.action = options.action; | ||
this.field = options.field; | ||
this.message = message || `Cannot execute "${this.action}" on "${this.subjectName}"`; | ||
|
||
if (typeof Error.captureStackTrace === 'function') { | ||
this.name = this.constructor.name; | ||
Error.captureStackTrace(this, this.constructor); | ||
} else { | ||
this.stack = new Error(this.message).stack; | ||
const getDefaultMessage = error => `Cannot execute "${error.action}" on "${error.subjectName}"`; | ||
let defaultErrorMessage = getDefaultMessage; | ||
|
||
export class ForbiddenError extends Error { | ||
static setDefaultMessage(messageOrFn) { | ||
if (messageOrFn === null) { | ||
defaultErrorMessage = getDefaultMessage; | ||
} else { | ||
defaultErrorMessage = typeof messageOrFn === 'string' ? () => messageOrFn : messageOrFn; | ||
} | ||
} | ||
|
||
static from(ability) { | ||
const error = new this(''); | ||
Object.defineProperty(error, 'ability', { value: ability }); | ||
return error; | ||
} | ||
|
||
constructor(message, options = {}) { | ||
super(message); | ||
this._setMetadata(options); | ||
this.message = message || defaultErrorMessage(this); | ||
this._customMessage = null; | ||
|
||
if (typeof Error.captureStackTrace === 'function') { | ||
this.name = this.constructor.name; | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
} | ||
|
||
setMessage(message) { | ||
this._customMessage = message; | ||
return this; | ||
} | ||
} | ||
|
||
ForbiddenError.prototype = Object.create(Error.prototype); | ||
throwUnlessCan(action, subject, field) { | ||
if (!this.ability) { | ||
throw new ReferenceError('Cannot throw FordiddenError without respective ability instance'); | ||
} | ||
|
||
const rule = this.ability.relevantRuleFor(action, subject, field); | ||
|
||
if (rule && !rule.inverted) { | ||
return; | ||
} | ||
|
||
this._setMetadata({ | ||
action, | ||
subject, | ||
field, | ||
subjectName: this.ability.subjectName(subject) | ||
}); | ||
|
||
const reason = rule ? rule.reason : ''; | ||
this.message = this._customMessage || reason || defaultErrorMessage(this); | ||
throw this; // eslint-disable-line | ||
} | ||
|
||
_setMetadata(options) { | ||
this.subject = options.subject; | ||
this.subjectName = options.subjectName; | ||
this.action = options.action; | ||
this.field = options.field; | ||
} | ||
} |