|
1 | | -import safeJsonValue from 'safe-json-value' |
| 1 | +import errorHttpResponse from 'error-http-response' |
2 | 2 |
|
3 | | -import { getOptions } from './options.js' |
4 | | - |
5 | | -// Turn `error` into a RFC 7807 problem details object. |
6 | | -// Object keys order is significant. |
7 | | -const httpResponse = function ({ |
8 | | - // eslint-disable-next-line no-unused-vars |
9 | | - error: { name, message, stack, cause, errors, ...errorProps }, |
10 | | - options, |
11 | | -}) { |
12 | | - return safeJsonValue({ |
13 | | - ...getOptionalProp(options, 'type'), |
14 | | - ...getOptionalProp(options, 'status'), |
15 | | - ...getDefaultedProp(options, 'title', String(name)), |
16 | | - ...getDefaultedProp(options, 'detail', String(message)), |
17 | | - ...getOptionalProp(options, 'instance'), |
18 | | - ...getDefaultedProp(options, 'stack', String(stack)), |
19 | | - ...getExtra(options, errorProps), |
20 | | - }).value |
21 | | -} |
22 | | - |
23 | | -const getOptionalProp = function (options, optName) { |
24 | | - return options[optName] === undefined ? {} : { [optName]: options[optName] } |
| 3 | +// Validate options |
| 4 | +const getOptions = function (options) { |
| 5 | + errorHttpResponse('', options) |
| 6 | + return options |
25 | 7 | } |
26 | 8 |
|
27 | | -const getDefaultedProp = function (options, optName, defaultValue) { |
28 | | - return { [optName]: options[optName] ?? defaultValue } |
29 | | -} |
30 | | - |
31 | | -const getExtra = function ({ extra }, errorProps) { |
32 | | - if (extra !== undefined) { |
33 | | - return { extra } |
34 | | - } |
35 | | - |
36 | | - return Object.keys(errorProps).length === 0 ? {} : { extra: errorProps } |
| 9 | +// Turn `error` into a RFC 7807 problem details object |
| 10 | +const httpResponse = function ({ error, options }) { |
| 11 | + return errorHttpResponse(error, options) |
37 | 12 | } |
38 | 13 |
|
39 | 14 | export default { |
|
0 commit comments