Skip to content

Commit d3f236d

Browse files
committed
Add support to invalid scope error
1 parent b7f3726 commit d3f236d

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

docs/errors.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ When a request returns with an HTTP status code of `403`.
2929

3030
When a request returns with an HTTP status code of `500`.
3131

32+
## `InvalidScopeError`
33+
34+
When a request returns with an HTTP status code of `400` and the response's body contains an error value of `invalid_scope`.
35+
3236
## `LoginRequiredError`
3337

3438
Returned when a request that requires authentication is performed without the proper credentials.

src/core/errors/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { AuthorizationRequiredError } from './authorization-required';
22
import { BaseError } from './base';
33
import { ForbiddenError } from './forbidden';
44
import { InternalServerError } from './internal-server';
5+
import { InvalidScopeError } from './invalid-scope';
56
import { NotFoundError } from './not-found';
67
import { OTPRequiredError } from './otp-required';
78
import { RateLimitError } from './rate-limit';
@@ -15,6 +16,7 @@ export {
1516
BaseError,
1617
ForbiddenError,
1718
InternalServerError,
19+
InvalidScopeError,
1820
NotFoundError,
1921
OTPRequiredError,
2022
RateLimitError,
@@ -28,6 +30,7 @@ export default [
2830
AuthorizationRequiredError,
2931
ForbiddenError,
3032
InternalServerError,
33+
InvalidScopeError,
3134
NotFoundError,
3235
OTPRequiredError,
3336
RateLimitError,

src/core/errors/invalid-scope.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { BaseError } from './base';
2+
3+
export class InvalidScopeError extends BaseError {
4+
static hasError({ body, status } = {}) {
5+
if (!status || (!body || !body.error)) {
6+
return false;
7+
}
8+
9+
if (status === 400 && body.error === 'invalid_scope') {
10+
return true;
11+
}
12+
13+
return false;
14+
}
15+
16+
constructor() {
17+
super('invalid_scope', ...arguments);
18+
}
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { InvalidScopeError } from '../../../src/core';
2+
3+
describe('InvalidScopeError', () => {
4+
describe('hasError()', () => {
5+
it('should return true if response status is 400 and error code is `invalid_scope`', () => {
6+
expect(InvalidScopeError.hasError({ body: { error: 'invalid_scope' }, status: 400 })).toBe(true);
7+
});
8+
9+
it('should return false if response does not contain a `invalid_scope` code', () => {
10+
expect(InvalidScopeError.hasError({ body: { error: 'foo' }, status: 400 })).toBe(false);
11+
});
12+
13+
it('should return false if response status is not 400', () => {
14+
expect(InvalidScopeError.hasError({ body: { error: 'invalid_scope' }, status: 401 })).toBe(false);
15+
});
16+
});
17+
18+
describe('constructor()', () => {
19+
it('should set `invalid_scope` message and given properties', () => {
20+
const error = new InvalidScopeError({ foo: 'bar' });
21+
22+
expect(error.foo).toBe('bar');
23+
expect(error.message).toBe('invalid_scope');
24+
});
25+
});
26+
});

test/core/utils/error-factory.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
ForbiddenError,
33
InternalServerError,
4+
InvalidScopeError,
45
NotFoundError,
56
OTPRequiredError,
67
RateLimitError,
@@ -21,6 +22,10 @@ describe('ErrorFactory', () => {
2122
expect(createError({ status: 500 })).toBeInstanceOf(InternalServerError);
2223
});
2324

25+
it('should create an `InvalidScopeError`', () => {
26+
expect(createError({ body: { error: 'invalid_scope' }, status: 400 })).toBeInstanceOf(InvalidScopeError);
27+
});
28+
2429
it('should create a `NotFoundError`', () => {
2530
expect(createError({ status: 404 })).toBeInstanceOf(NotFoundError);
2631
});

0 commit comments

Comments
 (0)