* Construct a JSON-serializable object given an error and a JSON-serializable `fallbackError` * * @param error - The error in question. * @param fallbackError - A JSON-serializable fallback error. * @param shouldPreserveMessage - Whether to preserve the error's message if the fallback * error i
( error: unknown, fallbackError: SerializedJsonRpcError, shouldPreserveMessage: boolean, )
| 136 | * @returns A JSON-serializable error object. |
| 137 | */ |
| 138 | function buildError( |
| 139 | error: unknown, |
| 140 | fallbackError: SerializedJsonRpcError, |
| 141 | shouldPreserveMessage: boolean, |
| 142 | ): SerializedJsonRpcError { |
| 143 | // If an error specifies a `serialize` function, we call it and return the result. |
| 144 | if ( |
| 145 | error && |
| 146 | typeof error === 'object' && |
| 147 | 'serialize' in error && |
| 148 | typeof error.serialize === 'function' |
| 149 | ) { |
| 150 | return error.serialize(); |
| 151 | } |
| 152 | |
| 153 | if (isJsonRpcError(error)) { |
| 154 | return error; |
| 155 | } |
| 156 | |
| 157 | const originalMessage = getOriginalMessage(error); |
| 158 | |
| 159 | // If the error does not match the JsonRpcError type, use the fallback error, but try to include the original error as `cause`. |
| 160 | const cause = serializeCause(error); |
| 161 | const fallbackWithCause = { |
| 162 | ...fallbackError, |
| 163 | ...(shouldPreserveMessage && |
| 164 | originalMessage && { message: originalMessage }), |
| 165 | data: { cause }, |
| 166 | }; |
| 167 | |
| 168 | return fallbackWithCause; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Attempts to extract the original `message` property from an error value of uncertain shape. |
no test coverage detected
searching dependent graphs…