Skip to content

Commit

Permalink
Initial shortMessage support for errors (#4241).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Oct 10, 2023
1 parent 2616f4c commit d6a8c14
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src.ts/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type { FetchRequest, FetchResponse } from "./fetch.js";
* An error may contain additional properties, but those must not
* conflict with any impliciat properties.
*/
export type ErrorInfo<T> = Omit<T, "code" | "name" | "message">;
export type ErrorInfo<T> = Omit<T, "code" | "name" | "message" | "shortMessage"> & { shortMessage?: string };


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

/**
* A short message describing the error, with minimal additional
* details.
*/
shortMessage: string;

/**
* Additional info regarding the error that may be useful.
*
Expand Down Expand Up @@ -648,13 +654,16 @@ export function isCallException(error: any): error is CallExceptionError {
* ethers version, %%code%% and all aditional properties, serialized.
*/
export function makeError<K extends ErrorCode, T extends CodedEthersError<K>>(message: string, code: K, info?: ErrorInfo<T>): T {
let shortMessage = message;

{
const details: Array<string> = [];
if (info) {
if ("message" in info || "code" in info || "name" in info) {
throw new Error(`value will overwrite populated values: ${ stringify(info) }`);
}
for (const key in info) {
if (key === "shortMessage") { continue; }
const value = <any>(info[<keyof ErrorInfo<T>>key]);
// try {
details.push(key + "=" + stringify(value));
Expand Down Expand Up @@ -689,6 +698,10 @@ export function makeError<K extends ErrorCode, T extends CodedEthersError<K>>(me

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

if ((<any>error).shortMessage == null) {
defineProperties<EthersError>(<EthersError>error, { shortMessage });
}

return <T>error;
}

Expand Down

0 comments on commit d6a8c14

Please sign in to comment.