-
Notifications
You must be signed in to change notification settings - Fork 396
/
Copy patherrors.ts
249 lines (228 loc) · 8.32 KB
/
errors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**
* @ignore
*/
export function appendCause(errorMessage: string, cause?: Error): string {
if (!cause) return errorMessage;
const separator = errorMessage.endsWith('.') ? '' : '.';
return `${errorMessage}${separator} CAUSE: ${cause.message}`;
}
type AuthErrorOptions = {
code: string;
message: string;
name: string;
cause?: Error;
status?: number;
};
/**
* The base class for all SDK errors.
*
* Because part of the error message can come from the OpenID Connect `error` query parameter we
* do some basic escaping which makes sure the default error handler is safe from XSS.
*
* **IMPORTANT** If you write your own error handler, you should **not** render the error
* without using a templating engine that will properly escape it for other HTML contexts first.
*
* Note that the error message of the {@link AuthError.cause | underlying error} is **not** escaped
* in any way, so do **not** render it without escaping it first!
*
* @category Server
*/
export abstract class AuthError extends Error {
/**
* A machine-readable error code that remains stable within a major version of the SDK. You
* should rely on this error code to handle errors. In contrast, the error message is not part of
* the API and can change anytime. Do **not** parse or otherwise rely on the error message to
* handle errors.
*/
public readonly code: string;
/**
* The error class name.
*/
public readonly name: string;
/**
* The underlying error, if any.
*
* **IMPORTANT** When this error is from the Identity Provider ({@link IdentityProviderError}) it can contain user
* input and is only escaped using basic escaping for putting untrusted data directly into the HTML body.
*
* You should **not** render this error without using a templating engine that will properly escape it for other
* HTML contexts first.
*/
public readonly cause?: Error;
/**
* The HTTP status code, if any.
*/
public readonly status?: number;
constructor(options: AuthErrorOptions) {
/* c8 ignore next */
super(appendCause(options.message, options.cause));
this.code = options.code;
this.name = options.name;
this.cause = options.cause;
this.status = options.status;
}
}
/**
* Error codes for {@link AccessTokenError}.
*
* @category Server
*/
export enum AccessTokenErrorCode {
MISSING_SESSION = 'ERR_MISSING_SESSION',
MISSING_ACCESS_TOKEN = 'ERR_MISSING_ACCESS_TOKEN',
MISSING_REFRESH_TOKEN = 'ERR_MISSING_REFRESH_TOKEN',
EXPIRED_ACCESS_TOKEN = 'ERR_EXPIRED_ACCESS_TOKEN',
INSUFFICIENT_SCOPE = 'ERR_INSUFFICIENT_SCOPE',
FAILED_REFRESH_GRANT = 'ERR_FAILED_REFRESH_GRANT'
}
/**
* The error thrown by {@link GetAccessToken}.
*
* @see the {@link AuthError.code | code property} contains a machine-readable error code that
* remains stable within a major version of the SDK. You should rely on this error code to handle
* errors. In contrast, the error message is not part of the API and can change anytime. Do **not**
* parse or otherwise rely on the error message to handle errors.
*
* @see {@link AccessTokenErrorCode} for the list of all possible error codes.
* @category Server
*/
export class AccessTokenError extends AuthError {
constructor(code: AccessTokenErrorCode, message: string, cause?: Error) {
/* c8 ignore next */
super({ code: code, message: message, name: 'AccessTokenError', cause });
// Capturing stack trace, excluding constructor call from it.
Error.captureStackTrace(this, this.constructor);
Object.setPrototypeOf(this, AccessTokenError.prototype);
}
}
/**
* @ignore
*/
interface HttpError extends Error {
status: number;
statusCode: number;
}
/**
* @ignore
*/
export type HandlerErrorCause = Error | AuthError | HttpError;
type HandlerErrorOptions = {
code: string;
message: string;
name: string;
cause: HandlerErrorCause;
};
/**
* The base class for errors thrown by API route handlers. It extends {@link AuthError}.
*
* Because part of the error message can come from the OpenID Connect `error` query parameter we
* do some basic escaping which makes sure the default error handler is safe from XSS.
*
* **IMPORTANT** If you write your own error handler, you should **not** render the error message
* without using a templating engine that will properly escape it for other HTML contexts first.
*
* @see the {@link AuthError.cause | cause property} contains the underlying error.
* **IMPORTANT** When this error is from the Identity Provider ({@link IdentityProviderError}) it can contain user
* input and is only escaped using basic escaping for putting untrusted data directly into the HTML body.
* You should **not** render this error without using a templating engine that will properly escape it for other
* HTML contexts first.
*
* @see the {@link AuthError.status | status property} contains the HTTP status code of the error,
* if any.
*
* @category Server
*/
export class HandlerError extends AuthError {
constructor(options: HandlerErrorOptions) {
let status: number | undefined;
if ('status' in options.cause) status = options.cause.status;
/* c8 ignore next */
super({ ...options, status });
}
}
/**
* The error thrown by the callback API route handler. It extends {@link HandlerError}.
*
* Because part of the error message can come from the OpenID Connect `error` query parameter we
* do some basic escaping which makes sure the default error handler is safe from XSS.
*
* **IMPORTANT** If you write your own error handler, you should **not** render the error message
* without using a templating engine that will properly escape it for other HTML contexts first.
*
* @see the {@link AuthError.cause | cause property} contains the underlying error.
* **IMPORTANT** When this error is from the Identity Provider ({@link IdentityProviderError}) it can contain user
* input and is only escaped using basic escaping for putting untrusted data directly into the HTML body.
* You should **not** render this error without using a templating engine that will properly escape it for other
* HTML contexts first.
*
* @see the {@link AuthError.status | status property} contains the HTTP status code of the error,
* if any.
*
* @category Server
*/
export class CallbackHandlerError extends HandlerError {
public static readonly code: string = 'ERR_CALLBACK_HANDLER_FAILURE';
constructor(cause: HandlerErrorCause) {
super({
code: CallbackHandlerError.code,
message: 'Callback handler failed.',
name: 'CallbackHandlerError',
cause
}); /* c8 ignore next */
Object.setPrototypeOf(this, CallbackHandlerError.prototype);
}
}
/**
* The error thrown by the login API route handler. It extends {@link HandlerError}.
*
* @see the {@link AuthError.cause | cause property} contains the underlying error.
* @category Server
*/
export class LoginHandlerError extends HandlerError {
public static readonly code: string = 'ERR_LOGIN_HANDLER_FAILURE';
constructor(cause: HandlerErrorCause) {
super({
code: LoginHandlerError.code,
message: 'Login handler failed.',
name: 'LoginHandlerError',
cause
}); /* c8 ignore next */
Object.setPrototypeOf(this, LoginHandlerError.prototype);
}
}
/**
* The error thrown by the logout API route handler. It extends {@link HandlerError}.
*
* @see the {@link AuthError.cause | cause property} contains the underlying error.
* @category Server
*/
export class LogoutHandlerError extends HandlerError {
public static readonly code: string = 'ERR_LOGOUT_HANDLER_FAILURE';
constructor(cause: HandlerErrorCause) {
super({
code: LogoutHandlerError.code,
message: 'Logout handler failed.',
name: 'LogoutHandlerError',
cause
}); /* c8 ignore next */
Object.setPrototypeOf(this, LogoutHandlerError.prototype);
}
}
/**
* The error thrown by the profile API route handler. It extends {@link HandlerError}.
*
* @see the {@link AuthError.cause | cause property} contains the underlying error.
* @category Server
*/
export class ProfileHandlerError extends HandlerError {
public static readonly code: string = 'ERR_PROFILE_HANDLER_FAILURE';
constructor(cause: HandlerErrorCause) {
super({
code: ProfileHandlerError.code,
message: 'Profile handler failed.',
name: 'ProfileHandlerError',
cause
}); /* c8 ignore next */
Object.setPrototypeOf(this, ProfileHandlerError.prototype);
}
}