Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions src/core/errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -15,6 +16,7 @@ export {
BaseError,
ForbiddenError,
InternalServerError,
InvalidScopeError,
NotFoundError,
OTPRequiredError,
RateLimitError,
Expand All @@ -28,6 +30,7 @@ export default [
AuthorizationRequiredError,
ForbiddenError,
InternalServerError,
InvalidScopeError,
NotFoundError,
OTPRequiredError,
RateLimitError,
Expand Down
19 changes: 19 additions & 0 deletions src/core/errors/invalid-scope.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
26 changes: 26 additions & 0 deletions test/core/errors/invalid_scope.spec.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
5 changes: 5 additions & 0 deletions test/core/utils/error-factory.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ForbiddenError,
InternalServerError,
InvalidScopeError,
NotFoundError,
OTPRequiredError,
RateLimitError,
Expand All @@ -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);
});
Expand Down