Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 111x 200x 200x 84x 84x 116x 116x 2x 85x 85x 2x 85x 1x 85x 1x 85x | import { hasProperty } from '@metamask/utils';
/**
* Stringify an evaluation result.
*
* @param value - The result to stringify.
* @param indent - The number of spaces to use for indentation (optional).
* @returns The stringified result.
*/
export const stringify = (value: unknown, indent: number = 2): string => {
try {
if (value instanceof Error) {
const errorObject = stringifyError(value);
return JSON.stringify(errorObject, null, indent);
}
const result = JSON.stringify(value, null, indent);
return result ?? String(value);
} catch {
return String(value);
}
};
/**
* Helper function to process an error.
*
* @param error - The error to process.
* @returns The processed object.
*/
function stringifyError(error: Error): Record<string, unknown> {
const errorObject: Record<string, unknown> = {
name: error.name,
message: error.message,
stack: error.stack,
};
if (error.cause) {
errorObject.cause =
error.cause instanceof Error
? stringifyError(error.cause)
: stringify(error.cause);
}
// By our convention, these properties may be present on errors
// and should be preserved.
if (hasProperty(error, 'code')) {
errorObject.code = error.code;
}
if (hasProperty(error, 'data')) {
errorObject.data = stringify(error.data);
}
return errorObject;
}
|