1 | 'use strict'
|
2 |
|
3 |
|
4 |
|
5 | const throwUserError = function (messageOrError, error) {
|
6 | const errorA = getError(messageOrError, error)
|
7 | errorA[CUSTOM_ERROR_KEY] = { type: USER_ERROR_TYPE }
|
8 | throw errorA
|
9 | }
|
10 |
|
11 |
|
12 | const getError = function (messageOrError, error) {
|
13 | if (messageOrError instanceof Error) {
|
14 | return messageOrError
|
15 | }
|
16 |
|
17 | if (error === undefined) {
|
18 | return new Error(messageOrError)
|
19 | }
|
20 |
|
21 | error.message = `${messageOrError}\n${error.message}`
|
22 | return error
|
23 | }
|
24 |
|
25 | const isUserError = function (error) {
|
26 | return (
|
27 | canHaveErrorInfo(error) && error[CUSTOM_ERROR_KEY] !== undefined && error[CUSTOM_ERROR_KEY].type === USER_ERROR_TYPE
|
28 | )
|
29 | }
|
30 |
|
31 |
|
32 |
|
33 | const canHaveErrorInfo = function (error) {
|
34 | return error != null
|
35 | }
|
36 |
|
37 | const CUSTOM_ERROR_KEY = 'customErrorInfo'
|
38 | const USER_ERROR_TYPE = 'resolveConfig'
|
39 |
|
40 | module.exports = { throwUserError, isUserError }
|