MCPcopy Create free account
hub / github.com/MetaMask/rpc-errors / buildError

Function buildError

src/utils.ts:138–169  ·  view source on GitHub ↗

* 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,
)

Source from the content-addressed store, hash-verified

136 * @returns A JSON-serializable error object.
137 */
138function 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.

Callers 1

serializeErrorFunction · 0.85

Calls 3

getOriginalMessageFunction · 0.85
serializeCauseFunction · 0.85
serializeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…