UNPKG

16.7 kBTypeScriptView Raw
1export = Boom;
2/**
3 * boom provides a set of utilities for returning HTTP errors. Each utility returns a Boom error response object (instance of Error) which includes the following properties:
4 * @see {@link https://github.com/hapijs/boom#boom}
5 */
6declare class Boom<Data = any> extends Error {
7 /** Creates a new Boom object using the provided message and then calling boomify() to decorate the error with the Boom properties. */
8 constructor(message?: string | Error, options?: Boom.Options<Data>);
9 /** isBoom - if true, indicates this is a Boom object instance. */
10 isBoom: boolean;
11 /** isServer - convenience bool indicating status code >= 500. */
12 isServer: boolean;
13 /** message - the error message. */
14 message: string;
15 /** output - the formatted response. Can be directly manipulated after object construction to return a custom error response. Allowed root keys: */
16 output: Boom.Output;
17 /** typeof - the constructor used to create the error (e.g. Boom.badRequest). */
18 readonly typeof: () => any;
19 /** reformat() - rebuilds error.output using the other object properties. */
20 reformat(): string;
21 /**
22 * "If message is unset, the 'error' segment of the header will not be present and
23 * isMissing will be true on the error object." mentioned in
24 * @see {@link https://github.com/hapijs/boom#boomunauthorizedmessage-scheme-attributes}
25 */
26 isMissing?: boolean | undefined;
27 /** https://github.com/hapijs/boom#createstatuscode-message-data and https://github.com/hapijs/boom/blob/v4.3.0/lib/index.js#L99 */
28 data: Data;
29}
30declare namespace Boom {
31 interface Options<Data> {
32 /** statusCode - the HTTP status code. Defaults to 500 if no status code is already set. */
33 statusCode?: number | undefined;
34 /** data - additional error information (assigned to error.data). */
35 data?: Data | undefined;
36 /** decorate - an option with extra properties to set on the error object. */
37 decorate?: object | undefined;
38 /** ctor - constructor reference used to crop the exception call stack output. */
39 ctor?: any;
40 /** message - error message string. If the error already has a message, the provided message is added as a prefix. Defaults to no message. */
41 message?: string | undefined;
42 /**
43 * override - if false, the err provided is a Boom object, and a statusCode or message are
44 * provided, the values are ignored. Defaults to true (apply the provided statusCode and
45 * message options to the error regardless of its type, Error or Boom object).
46 */
47 override?: boolean | undefined;
48 }
49
50 interface Output {
51 /** statusCode - the HTTP status code (typically 4xx or 5xx). */
52 statusCode: number;
53 /**
54 * headers - an object containing any HTTP headers where each key is a header name and
55 * value is the header content. (Limited value type to string
56 * https://github.com/hapijs/boom/issues/151 )
57 */
58 headers: { [index: string]: string };
59 /**
60 * payload - the formatted object used as the response payload (stringified).
61 * Can be directly manipulated but any changes will be lost if reformat() is called.
62 * Any content allowed and by default includes the following content:
63 */
64 payload: Payload;
65 }
66
67 interface Payload {
68 /** statusCode - the HTTP status code, derived from error.output.statusCode. */
69 statusCode: number;
70 /** error - the HTTP status message (e.g. 'Bad Request', 'Internal Server Error') derived from statusCode. */
71 error: string;
72 /** message - the error message derived from error.message. */
73 message: string;
74 /**
75 * "Every key/value pair will be included ... in the response payload under the attributes key."
76 * [see docs](https://github.com/hapijs/boom#boomunauthorizedmessage-scheme-attributes)
77 */
78 attributes?: any;
79 // Excluded this to aid typing of the other values. See tests for example casting to a custom interface to manipulate the payload
80 // [anyContent: string]: any;
81 }
82
83 /**
84 * Decorates an error with the boom properties
85 * @param error the error object to wrap. If error is already a boom object, it defaults to overriding the object with the new status code and message.
86 * @param options optional additional options
87 * @see {@link https://github.com/hapijs/boom#boomifyerror-options}
88 */
89 function boomify(
90 error: Error,
91 options?: { statusCode?: number | undefined; message?: string | undefined; override?: boolean | undefined },
92 ): Boom<null>;
93
94 /**
95 * Identifies whether an error is a Boom object. Same as calling instanceof Boom.
96 * @param error the error object to identify.
97 */
98 function isBoom(error: Error): error is Boom;
99
100 // 4xx
101 /**
102 * Returns a 400 Bad Request error
103 * @param message optional message.
104 * @param data optional additional error data.
105 * @see {@link https://github.com/hapijs/boom#boombadrequestmessage-data}
106 */
107 function badRequest<Data = null>(message?: string, data?: Data): Boom<Data>;
108
109 /**
110 * Returns a 401 Unauthorized error
111 * @param message optional message.
112 * @param scheme can be one of the following:
113 * * an authentication scheme name
114 * * an array of string values. These values will be separated by ', ' and set to the 'WWW-Authenticate' header.
115 * @param attributes an object of values to use while setting the 'WWW-Authenticate' header.
116 * This value is only used when scheme is a string, otherwise it is ignored.
117 * Every key/value pair will be included in the 'WWW-Authenticate' in the format of
118 * 'key="value"' as well as in the response payload under the attributes key.
119 * Alternatively value can be a string which is use to set the value of the scheme,
120 * for example setting the token value for negotiate header.
121 * If string is used message parameter must be null.
122 * null and undefined will be replaced with an empty string. If attributes is set,
123 * message will be used as the 'error' segment of the 'WWW-Authenticate' header.
124 * If message is unset, the 'error' segment of the header will not be present and isMissing will be true on the error object.
125 * @see {@link https://github.com/hapijs/boom#boomunauthorizedmessage-scheme-attributes}
126 */
127 function unauthorized(message?: string, scheme?: string, attributes?: { [index: string]: string }): Boom<null>;
128 function unauthorized(message?: string, scheme?: string[]): Boom<null>;
129 function unauthorized(
130 message?: null,
131 scheme?: string,
132 attributes?: { [index: string]: string } | string,
133 ): Boom<null>;
134
135 /**
136 * Returns a 402 Payment Required error
137 * @param message optional message.
138 * @param data optional additional error data.
139 * @see {@link https://github.com/hapijs/boom#boompaymentrequiredmessage-data}
140 */
141 function paymentRequired<Data = null>(message?: string, data?: Data): Boom<Data>;
142
143 /**
144 * Returns a 403 Forbidden error
145 * @param message optional message.
146 * @param data optional additional error data.
147 * @see {@link https://github.com/hapijs/boom#boomforbiddenmessage-data}
148 */
149 function forbidden<Data = null>(message?: string, data?: Data): Boom<Data>;
150
151 /**
152 * Returns a 404 Not Found error
153 * @param message optional message.
154 * @param data optional additional error data.
155 * @see {@link https://github.com/hapijs/boom#boomnotfoundmessage-data}
156 */
157 function notFound<Data = null>(message?: string, data?: Data): Boom<Data>;
158
159 /**
160 * Returns a 405 Method Not Allowed error
161 * @param message optional message.
162 * @param data optional additional error data.
163 * @param allow optional string or array of strings (to be combined and separated by ', ') which is set to the 'Allow' header.
164 * @see {@link https://github.com/hapijs/boom#boommethodnotallowedmessage-data-allow}
165 */
166 function methodNotAllowed<Data = null>(message?: string, data?: Data, allow?: string | string[]): Boom<Data>;
167
168 /**
169 * Returns a 406 Not Acceptable error
170 * @param message optional message.
171 * @param data optional additional error data.
172 * @see {@link https://github.com/hapijs/boom#boomnotacceptablemessage-data}
173 */
174 function notAcceptable<Data = null>(message?: string, data?: Data): Boom<Data>;
175
176 /**
177 * Returns a 407 Proxy Authentication Required error
178 * @param message optional message.
179 * @param data optional additional error data.
180 * @see {@link https://github.com/hapijs/boom#boomproxyauthrequiredmessage-data}
181 */
182 function proxyAuthRequired<Data = null>(message?: string, data?: Data): Boom<Data>;
183
184 /**
185 * Returns a 408 Request Time-out error
186 * @param message optional message.
187 * @param data optional additional error data.
188 * @see {@link https://github.com/hapijs/boom#boomclienttimeoutmessage-data}
189 */
190 function clientTimeout<Data = null>(message?: string, data?: Data): Boom<Data>;
191
192 /**
193 * Returns a 409 Conflict error
194 * @param message optional message.
195 * @param data optional additional error data.
196 * @see {@link https://github.com/hapijs/boom#boomconflictmessage-data}
197 */
198 function conflict<Data = null>(message?: string, data?: Data): Boom<Data>;
199
200 /**
201 * Returns a 410 Gone error
202 * @param message optional message.
203 * @param data optional additional error data.
204 * @see {@link https://github.com/hapijs/boom#boomresourcegonemessage-data}
205 */
206 function resourceGone<Data = null>(message?: string, data?: Data): Boom<Data>;
207
208 /**
209 * Returns a 411 Length Required error
210 * @param message optional message.
211 * @param data optional additional error data.
212 * @see {@link https://github.com/hapijs/boom#boomlengthrequiredmessage-data}
213 */
214 function lengthRequired<Data = null>(message?: string, data?: Data): Boom<Data>;
215
216 /**
217 * Returns a 412 Precondition Failed error
218 * @param message optional message.
219 * @param data optional additional error data.
220 * @see {@link https://github.com/hapijs/boom#boompreconditionfailedmessage-data}
221 */
222 function preconditionFailed<Data = null>(message?: string, data?: Data): Boom<Data>;
223
224 /**
225 * Returns a 413 Request Entity Too Large error
226 * @param message optional message.
227 * @param data optional additional error data.
228 * @see {@link https://github.com/hapijs/boom#boomentitytoolargemessage-data}
229 */
230 function entityTooLarge<Data = null>(message?: string, data?: Data): Boom<Data>;
231
232 /**
233 * Returns a 414 Request-URI Too Large error
234 * @param message optional message.
235 * @param data optional additional error data.
236 * @see {@link https://github.com/hapijs/boom#boomuritoolongmessage-data}
237 */
238 function uriTooLong<Data = null>(message?: string, data?: Data): Boom<Data>;
239
240 /**
241 * Returns a 415 Unsupported Media Type error
242 * @param message optional message.
243 * @param data optional additional error data.
244 * @see {@link https://github.com/hapijs/boom#boomunsupportedmediatypemessage-data}
245 */
246 function unsupportedMediaType<Data = null>(message?: string, data?: Data): Boom<Data>;
247
248 /**
249 * Returns a 416 Requested Range Not Satisfiable error
250 * @param message optional message.
251 * @param data optional additional error data.
252 * @see {@link https://github.com/hapijs/boom#boomrangenotsatisfiablemessage-data}
253 */
254 function rangeNotSatisfiable<Data = null>(message?: string, data?: Data): Boom<Data>;
255
256 /**
257 * Returns a 417 Expectation Failed error
258 * @param message optional message.
259 * @param data optional additional error data.
260 * @see {@link https://github.com/hapijs/boom#boomexpectationfailedmessage-data}
261 */
262 function expectationFailed<Data = null>(message?: string, data?: Data): Boom<Data>;
263
264 /**
265 * Returns a 418 I'm a Teapot error
266 * @param message optional message.
267 * @param data optional additional error data.
268 * @see {@link https://github.com/hapijs/boom#boomteapotmessage-data}
269 */
270 function teapot<Data = null>(message?: string, data?: Data): Boom<Data>;
271
272 /**
273 * Returns a 422 Unprocessable Entity error
274 * @param message optional message.
275 * @param data optional additional error data.
276 * @see {@link https://github.com/hapijs/boom#boombaddatamessage-data}
277 */
278 function badData<Data = null>(message?: string, data?: Data): Boom<Data>;
279
280 /**
281 * Returns a 423 Locked error
282 * @param message optional message.
283 * @param data optional additional error data.
284 * @see {@link https://github.com/hapijs/boom#boomlockedmessage-data}
285 */
286 function locked<Data = null>(message?: string, data?: Data): Boom<Data>;
287
288 /**
289 * Returns a 424 Failed Dependency error
290 * @param message optional message.
291 * @param data optional additional error data.
292 * @see {@link https://github.com/hapijs/boom#boomfaileddependencymessage-data}
293 */
294 function failedDependency<Data = null>(message?: string, data?: Data): Boom<Data>;
295
296 /**
297 * Returns a 428 Precondition Required error
298 * @param message optional message.
299 * @param data optional additional error data.
300 * @see {@link https://github.com/hapijs/boom#boompreconditionrequiredmessage-data}
301 */
302 function preconditionRequired<Data = null>(message?: string, data?: Data): Boom<Data>;
303
304 /**
305 * Returns a 429 Too Many Requests error
306 * @param message optional message.
307 * @param data optional additional error data.
308 * @see {@link https://github.com/hapijs/boom#boomtoomanyrequestsmessage-data}
309 */
310 function tooManyRequests<Data = null>(message?: string, data?: Data): Boom<Data>;
311
312 /**
313 * Returns a 451 Unavailable For Legal Reasons error
314 * @param message optional message.
315 * @param data optional additional error data.
316 * @see {@link https://github.com/hapijs/boom#boomillegalmessage-data}
317 */
318 function illegal<Data = null>(message?: string, data?: Data): Boom<Data>;
319
320 // 5xx
321 /**
322 * Returns a 500 Internal Server Error error
323 * Only 500 errors will hide your message from the end user. Your message is recorded in the server log.
324 * @param message optional message.
325 * @param data optional additional error data.
326 * @see {@link https://github.com/hapijs/boom#boombadimplementationmessage-data---alias-internal}
327 */
328 function badImplementation<Data = null>(message?: string, data?: Data): Boom<Data>;
329
330 /**
331 * Returns a 500 Internal Server Error error
332 * Only 500 errors will hide your message from the end user. Your message is recorded in the server log.
333 * @param message optional message.
334 * @param data optional additional error data.
335 * @see {@link https://github.com/hapijs/boom#boombadimplementationmessage-data---alias-internal}
336 */
337 function internal<Data = null>(message?: string, data?: Data): Boom<Data>;
338
339 /**
340 * Returns a 501 Not Implemented error with your error message to the user
341 * @param message optional message.
342 * @param data optional additional error data.
343 * @see {@link https://github.com/hapijs/boom#boomnotimplementedmessage-data}
344 */
345 function notImplemented<Data = null>(message?: string, data?: Data): Boom<Data>;
346
347 /**
348 * Returns a 502 Bad Gateway error with your error message to the user
349 * @param message optional message.
350 * @param data optional additional error data.
351 * @see {@link https://github.com/hapijs/boom#boombadgatewaymessage-data}
352 */
353 function badGateway<Data = null>(message?: string, data?: Data): Boom<Data>;
354
355 /**
356 * Returns a 503 Service Unavailable error with your error message to the user
357 * @param message optional message.
358 * @param data optional additional error data.
359 * @see {@link https://github.com/hapijs/boom#boomserverunavailablemessage-data}
360 */
361 function serverUnavailable<Data = null>(message?: string, data?: Data): Boom<Data>;
362
363 /**
364 * Returns a 504 Gateway Time-out error with your error message to the user
365 * @param message optional message.
366 * @param data optional additional error data.
367 * @see {@link https://github.com/hapijs/boom#boomgatewaytimeoutmessage-data}
368 */
369 function gatewayTimeout<Data = null>(message?: string, data?: Data): Boom<Data>;
370}