Skip to content

Commit d6a8c14

Browse files
committedOct 10, 2023
Initial shortMessage support for errors (#4241).
1 parent 2616f4c commit d6a8c14

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed
 

‎src.ts/utils/errors.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import type { FetchRequest, FetchResponse } from "./fetch.js";
2222
* An error may contain additional properties, but those must not
2323
* conflict with any impliciat properties.
2424
*/
25-
export type ErrorInfo<T> = Omit<T, "code" | "name" | "message">;
25+
export type ErrorInfo<T> = Omit<T, "code" | "name" | "message" | "shortMessage"> & { shortMessage?: string };
2626

2727

2828
function stringify(value: any): any {
@@ -159,6 +159,12 @@ export interface EthersError<T extends ErrorCode = ErrorCode> extends Error {
159159
*/
160160
code: ErrorCode;
161161

162+
/**
163+
* A short message describing the error, with minimal additional
164+
* details.
165+
*/
166+
shortMessage: string;
167+
162168
/**
163169
* Additional info regarding the error that may be useful.
164170
*
@@ -648,13 +654,16 @@ export function isCallException(error: any): error is CallExceptionError {
648654
* ethers version, %%code%% and all aditional properties, serialized.
649655
*/
650656
export function makeError<K extends ErrorCode, T extends CodedEthersError<K>>(message: string, code: K, info?: ErrorInfo<T>): T {
657+
let shortMessage = message;
658+
651659
{
652660
const details: Array<string> = [];
653661
if (info) {
654662
if ("message" in info || "code" in info || "name" in info) {
655663
throw new Error(`value will overwrite populated values: ${ stringify(info) }`);
656664
}
657665
for (const key in info) {
666+
if (key === "shortMessage") { continue; }
658667
const value = <any>(info[<keyof ErrorInfo<T>>key]);
659668
// try {
660669
details.push(key + "=" + stringify(value));
@@ -689,6 +698,10 @@ export function makeError<K extends ErrorCode, T extends CodedEthersError<K>>(me
689698

690699
if (info) { Object.assign(error, info); }
691700

701+
if ((<any>error).shortMessage == null) {
702+
defineProperties<EthersError>(<EthersError>error, { shortMessage });
703+
}
704+
692705
return <T>error;
693706
}
694707

0 commit comments

Comments
 (0)
Please sign in to comment.