Initial shortMessage support for errors (#4241).
This commit is contained in:
parent
2616f4c30c
commit
d6a8c14d90
@ -22,7 +22,7 @@ import type { FetchRequest, FetchResponse } from "./fetch.js";
|
|||||||
* An error may contain additional properties, but those must not
|
* An error may contain additional properties, but those must not
|
||||||
* conflict with any impliciat properties.
|
* 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 {
|
function stringify(value: any): any {
|
||||||
@ -159,6 +159,12 @@ export interface EthersError<T extends ErrorCode = ErrorCode> extends Error {
|
|||||||
*/
|
*/
|
||||||
code: ErrorCode;
|
code: ErrorCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A short message describing the error, with minimal additional
|
||||||
|
* details.
|
||||||
|
*/
|
||||||
|
shortMessage: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Additional info regarding the error that may be useful.
|
* Additional info regarding the error that may be useful.
|
||||||
*
|
*
|
||||||
@ -648,6 +654,8 @@ export function isCallException(error: any): error is CallExceptionError {
|
|||||||
* ethers version, %%code%% and all aditional properties, serialized.
|
* 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 {
|
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> = [];
|
const details: Array<string> = [];
|
||||||
if (info) {
|
if (info) {
|
||||||
@ -655,6 +663,7 @@ export function makeError<K extends ErrorCode, T extends CodedEthersError<K>>(me
|
|||||||
throw new Error(`value will overwrite populated values: ${ stringify(info) }`);
|
throw new Error(`value will overwrite populated values: ${ stringify(info) }`);
|
||||||
}
|
}
|
||||||
for (const key in info) {
|
for (const key in info) {
|
||||||
|
if (key === "shortMessage") { continue; }
|
||||||
const value = <any>(info[<keyof ErrorInfo<T>>key]);
|
const value = <any>(info[<keyof ErrorInfo<T>>key]);
|
||||||
// try {
|
// try {
|
||||||
details.push(key + "=" + stringify(value));
|
details.push(key + "=" + stringify(value));
|
||||||
@ -689,6 +698,10 @@ export function makeError<K extends ErrorCode, T extends CodedEthersError<K>>(me
|
|||||||
|
|
||||||
if (info) { Object.assign(error, info); }
|
if (info) { Object.assign(error, info); }
|
||||||
|
|
||||||
|
if ((<any>error).shortMessage == null) {
|
||||||
|
defineProperties<EthersError>(<EthersError>error, { shortMessage });
|
||||||
|
}
|
||||||
|
|
||||||
return <T>error;
|
return <T>error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user