generated from ehmicky/template-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.d.ts
98 lines (90 loc) · 2.17 KB
/
main.d.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
/**
* The options and the return value have the same shape
* ([RFC 7807](https://www.rfc-editor.org/rfc/rfc7807)). Options can be passed
* either as an argument to
* [`errorHttpResponse()`](#errorhttpresponseerror-options) or be set to
* `error.http`.
*
* Options are validated: an exception is thrown if their syntax is invalid.
*/
export interface Options {
/**
* URI identifying and documenting the error class.
* Ideally, each error class should set one.
*
* @default undefined
*/
readonly type?: string
/**
* HTTP status code.
*
* @default undefined
*/
readonly status?: number
/**
* Error class name.
*
* @default error.name
*/
readonly title?: string
/**
* Error description.
*
* @default error.message
*/
readonly detail?: string
/**
* URI identifying the value which errored.
*
* @default undefined
*/
readonly instance?: string
/**
* Error stack trace. Can be set to an empty string.
*
* @default error.stack
*/
readonly stack?: string
/**
* Additional information. This is always
* [safe to serialize as JSON](https://github.com/ehmicky/safe-json-value).
* Can be set to an empty object.
*
* @default any additional `error` properties
*/
readonly extra?: object
}
/**
* `errorHttpResponse()`'s return value
*/
export interface HttpResponse extends Options {
readonly title: string
readonly detail: string
readonly stack: string
}
/**
* Converts `error` to a plain object
* ([RFC 7807](https://www.rfc-editor.org/rfc/rfc7807), "problem details") to
* use in an HTTP response.
*
* `error` should be an `Error` instance, but invalid errors are automatically
* [normalized](https://github.com/ehmicky/normalize-exception).
*
* @example
* ```js
* const error = new AuthError('Could not authenticate.')
* error.userId = 62
* const object = errorHttpResponse(error)
* // {
* // title: 'AuthError',
* // detail: 'Could not authenticate.',
* // stack: `AuthError: Could not authenticate.
* // at ...`,
* // extra: { userId: 62 }
* // }
* ```
*/
export default function errorHttpResponse(
error: unknown,
options?: Options,
): HttpResponse