UNPKG

3.7 kBPlain TextView Raw
1/**
2 * Thrown when network requests to the Auth server fail.
3 */
4export class GenericError extends Error {
5 constructor(public error: string, public error_description: string) {
6 super(error_description);
7 Object.setPrototypeOf(this, GenericError.prototype);
8 }
9
10 static fromPayload({
11 error,
12 error_description
13 }: {
14 error: string;
15 error_description: string;
16 }) {
17 return new GenericError(error, error_description);
18 }
19}
20
21/**
22 * Thrown when handling the redirect callback fails, will be one of Auth0's
23 * Authentication API's Standard Error Responses: https://auth0.com/docs/api/authentication?javascript#standard-error-responses
24 */
25export class AuthenticationError extends GenericError {
26 constructor(
27 error: string,
28 error_description: string,
29 public state: string,
30 public appState: any = null
31 ) {
32 super(error, error_description);
33 //https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
34 Object.setPrototypeOf(this, AuthenticationError.prototype);
35 }
36}
37
38/**
39 * Thrown when silent auth times out (usually due to a configuration issue) or
40 * when network requests to the Auth server timeout.
41 */
42export class TimeoutError extends GenericError {
43 constructor() {
44 super('timeout', 'Timeout');
45 //https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
46 Object.setPrototypeOf(this, TimeoutError.prototype);
47 }
48}
49
50/**
51 * Error thrown when the login popup times out (if the user does not complete auth)
52 */
53export class PopupTimeoutError extends TimeoutError {
54 constructor(public popup: Window) {
55 super();
56 //https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
57 Object.setPrototypeOf(this, PopupTimeoutError.prototype);
58 }
59}
60
61export class PopupCancelledError extends GenericError {
62 constructor(public popup: Window) {
63 super('cancelled', 'Popup closed');
64 //https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
65 Object.setPrototypeOf(this, PopupCancelledError.prototype);
66 }
67}
68
69/**
70 * Error thrown when the token exchange results in a `mfa_required` error
71 */
72export class MfaRequiredError extends GenericError {
73 constructor(
74 error: string,
75 error_description: string,
76 public mfa_token: string
77 ) {
78 super(error, error_description);
79 //https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
80 Object.setPrototypeOf(this, MfaRequiredError.prototype);
81 }
82}
83
84/**
85 * Error thrown when there is no refresh token to use
86 */
87export class MissingRefreshTokenError extends GenericError {
88 constructor(public audience: string, public scope: string) {
89 super(
90 'missing_refresh_token',
91 `Missing Refresh Token (audience: '${valueOrEmptyString(audience, [
92 'default'
93 ])}', scope: '${valueOrEmptyString(scope)}')`
94 );
95 Object.setPrototypeOf(this, MissingRefreshTokenError.prototype);
96 }
97}
98
99/**
100 * Returns an empty string when value is falsy, or when it's value is included in the exclude argument.
101 * @param value The value to check
102 * @param exclude An array of values that should result in an empty string.
103 * @returns The value, or an empty string when falsy or included in the exclude argument.
104 */
105function valueOrEmptyString(value: string, exclude: string[] = []) {
106 return value && !exclude.includes(value) ? value : '';
107}