diff --git a/docs/errors.md b/docs/errors.md index 015917f..da0a46b 100644 --- a/docs/errors.md +++ b/docs/errors.md @@ -29,6 +29,10 @@ When a request returns with an HTTP status code of `403`. When a request returns with an HTTP status code of `500`. +## `InvalidScopeError` + +When a request returns with an HTTP status code of `400` and the response's body contains an error value of `invalid_scope`. + ## `LoginRequiredError` Returned when a request that requires authentication is performed without the proper credentials. diff --git a/src/core/errors/index.js b/src/core/errors/index.js index eeb065b..e2e8135 100644 --- a/src/core/errors/index.js +++ b/src/core/errors/index.js @@ -2,6 +2,7 @@ import { AuthorizationRequiredError } from './authorization-required'; import { BaseError } from './base'; import { ForbiddenError } from './forbidden'; import { InternalServerError } from './internal-server'; +import { InvalidScopeError } from './invalid-scope'; import { NotFoundError } from './not-found'; import { OTPRequiredError } from './otp-required'; import { RateLimitError } from './rate-limit'; @@ -15,6 +16,7 @@ export { BaseError, ForbiddenError, InternalServerError, + InvalidScopeError, NotFoundError, OTPRequiredError, RateLimitError, @@ -28,6 +30,7 @@ export default [ AuthorizationRequiredError, ForbiddenError, InternalServerError, + InvalidScopeError, NotFoundError, OTPRequiredError, RateLimitError, diff --git a/src/core/errors/invalid-scope.js b/src/core/errors/invalid-scope.js new file mode 100644 index 0000000..18b485c --- /dev/null +++ b/src/core/errors/invalid-scope.js @@ -0,0 +1,19 @@ +import { BaseError } from './base'; + +export class InvalidScopeError extends BaseError { + static hasError({ body, status } = {}) { + if (!status || (!body || !body.error)) { + return false; + } + + if (status === 400 && body.error === 'invalid_scope') { + return true; + } + + return false; + } + + constructor() { + super('invalid_scope', ...arguments); + } +} diff --git a/test/core/errors/invalid_scope.spec.js b/test/core/errors/invalid_scope.spec.js new file mode 100644 index 0000000..52b9ce2 --- /dev/null +++ b/test/core/errors/invalid_scope.spec.js @@ -0,0 +1,26 @@ +import { InvalidScopeError } from '../../../src/core'; + +describe('InvalidScopeError', () => { + describe('hasError()', () => { + it('should return true if response status is 400 and error code is `invalid_scope`', () => { + expect(InvalidScopeError.hasError({ body: { error: 'invalid_scope' }, status: 400 })).toBe(true); + }); + + it('should return false if response does not contain a `invalid_scope` code', () => { + expect(InvalidScopeError.hasError({ body: { error: 'foo' }, status: 400 })).toBe(false); + }); + + it('should return false if response status is not 400', () => { + expect(InvalidScopeError.hasError({ body: { error: 'invalid_scope' }, status: 401 })).toBe(false); + }); + }); + + describe('constructor()', () => { + it('should set `invalid_scope` message and given properties', () => { + const error = new InvalidScopeError({ foo: 'bar' }); + + expect(error.foo).toBe('bar'); + expect(error.message).toBe('invalid_scope'); + }); + }); +}); diff --git a/test/core/utils/error-factory.spec.js b/test/core/utils/error-factory.spec.js index 4933f92..4857f57 100644 --- a/test/core/utils/error-factory.spec.js +++ b/test/core/utils/error-factory.spec.js @@ -1,6 +1,7 @@ import { ForbiddenError, InternalServerError, + InvalidScopeError, NotFoundError, OTPRequiredError, RateLimitError, @@ -21,6 +22,10 @@ describe('ErrorFactory', () => { expect(createError({ status: 500 })).toBeInstanceOf(InternalServerError); }); + it('should create an `InvalidScopeError`', () => { + expect(createError({ body: { error: 'invalid_scope' }, status: 400 })).toBeInstanceOf(InvalidScopeError); + }); + it('should create a `NotFoundError`', () => { expect(createError({ status: 404 })).toBeInstanceOf(NotFoundError); });