From c5e6bbec6d960b4ccb30830984df510674ce5f6e Mon Sep 17 00:00:00 2001 From: Mahmoud Emad Date: Tue, 13 Aug 2024 11:20:19 +0300 Subject: [PATCH 1/2] fix: Define the value arg in the TFChainError constructor: - Send the value of the prop that i want to define as an object that contain a 'value' attr - Set the 'enumerable: ture' to display the error args --- packages/tfchain_client/src/errors.ts | 28 +++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/tfchain_client/src/errors.ts b/packages/tfchain_client/src/errors.ts index 997195b610..b77d8db500 100644 --- a/packages/tfchain_client/src/errors.ts +++ b/packages/tfchain_client/src/errors.ts @@ -27,12 +27,28 @@ class TFChainError extends Error { constructor(options: ITFChainError) { super(options.message); this.name = "TFChainError"; - this.keyError = options.keyError || "GenericError"; - options.keyError && Object.defineProperty(this, "keyError", options.keyError); - options.section && Object.defineProperty(this, "section", options.section); - options.args && Object.defineProperty(this, "args", options.args); - options.method && Object.defineProperty(this, "method", options.method); - options.docs && Object.defineProperty(this, "docs", options.docs); + + if (options.keyError) { + Object.defineProperty(this, "keyError", { value: options.keyError, enumerable: true }); + } else { + Object.defineProperty(this, "keyError", { value: "GenericError", enumerable: true }); + } + + if (options.section) { + Object.defineProperty(this, "section", { value: options.section, enumerable: true }); + } + + if (options.args) { + Object.defineProperty(this, "args", { value: options.args, enumerable: true }); + } + + if (options.method) { + Object.defineProperty(this, "method", { value: options.method, enumerable: true }); + } + + if (options.docs) { + Object.defineProperty(this, "docs", { value: options.docs, enumerable: true }); + } } } From ca9ec967e5c0f7f6a1346830d865d3b3e6969752 Mon Sep 17 00:00:00 2001 From: Mahmoud Emad Date: Wed, 14 Aug 2024 09:23:52 +0300 Subject: [PATCH 2/2] fix: dynamically defines the TFChainError key prop: - dynamically defines the TFChainError key property instead of making a condition on each key in the 'TFChainError' contractor. --- packages/tfchain_client/src/errors.ts | 29 +++++++++------------------ 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/packages/tfchain_client/src/errors.ts b/packages/tfchain_client/src/errors.ts index b77d8db500..bc0b659239 100644 --- a/packages/tfchain_client/src/errors.ts +++ b/packages/tfchain_client/src/errors.ts @@ -28,27 +28,18 @@ class TFChainError extends Error { super(options.message); this.name = "TFChainError"; - if (options.keyError) { - Object.defineProperty(this, "keyError", { value: options.keyError, enumerable: true }); - } else { - Object.defineProperty(this, "keyError", { value: "GenericError", enumerable: true }); - } - - if (options.section) { - Object.defineProperty(this, "section", { value: options.section, enumerable: true }); - } - - if (options.args) { - Object.defineProperty(this, "args", { value: options.args, enumerable: true }); + if (!options.keyError) { + options.keyError = "GenericError"; } - if (options.method) { - Object.defineProperty(this, "method", { value: options.method, enumerable: true }); - } - - if (options.docs) { - Object.defineProperty(this, "docs", { value: options.docs, enumerable: true }); - } + Object.keys(options).forEach(key => { + if (options[key as keyof ITFChainError]) { + Object.defineProperty(this, key, { + value: options[key as keyof ITFChainError], + enumerable: true, + }); + } + }); } }