From ff842d2a59ef898e734e1424ee6f26b52ba0af9b Mon Sep 17 00:00:00 2001 From: Peter Somogyvari Date: Wed, 16 Oct 2024 18:06:23 -0700 Subject: [PATCH] build: migrate to Typescript target of ES2022 and use Error.cause Project-wide upgrade to Typescript target of ES2022 so that we can use the new Error APIs. Wherever possible we should now use the new `cause` property of the built-in `Error` type in combination with the `asError(unknown)` utility function: ```typescript import { asError } from "@hyperledger/cactus-common"; try { await performSomeImportantOperation(); } catch (ex: unknown) { const cause = asError(ex); throw new Error("Something went wrong while doing something.", { cause }); } ``` More information about the EcmaScript proposal that made this possible: https://github.com/tc39/proposal-error-cause Fixes #3592 Signed-off-by: Peter Somogyvari --- .../tsconfig.json | 2 +- examples/test-run-transaction/tsconfig.json | 6 +-- .../create-runtime-error-with-cause.ts | 39 +++++++++++++++++++ tsconfig.base.json | 7 +--- tsconfig.json | 7 +--- 5 files changed, 46 insertions(+), 15 deletions(-) diff --git a/examples/cactus-example-supply-chain-frontend/tsconfig.json b/examples/cactus-example-supply-chain-frontend/tsconfig.json index cb97846075..5781182096 100644 --- a/examples/cactus-example-supply-chain-frontend/tsconfig.json +++ b/examples/cactus-example-supply-chain-frontend/tsconfig.json @@ -14,7 +14,7 @@ "importHelpers": true, "target": "ES2022", "lib": [ - "es2018", + "ES2022", "dom" ], "resolveJsonModule": true, diff --git a/examples/test-run-transaction/tsconfig.json b/examples/test-run-transaction/tsconfig.json index 98a8691880..d56cfc0018 100644 --- a/examples/test-run-transaction/tsconfig.json +++ b/examples/test-run-transaction/tsconfig.json @@ -2,12 +2,10 @@ "compilerOptions": { /* Basic Options */ "incremental": true, /* Enable incremental compilation */ - "target": "ES2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ + "target": "ES2022", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ "module": "CommonJS", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ "lib": [ - "es2015", - "es2016", - "es2017", + "ES2022", "dom" ], /* Specify library files to be included in the compilation. */ // "allowJs": true, /* Allow javascript files to be compiled. */ diff --git a/packages/cactus-common/src/main/typescript/exception/create-runtime-error-with-cause.ts b/packages/cactus-common/src/main/typescript/exception/create-runtime-error-with-cause.ts index bef8326dff..f6bf6029d1 100644 --- a/packages/cactus-common/src/main/typescript/exception/create-runtime-error-with-cause.ts +++ b/packages/cactus-common/src/main/typescript/exception/create-runtime-error-with-cause.ts @@ -2,6 +2,25 @@ import { RuntimeError } from "run-time-error-cjs"; import { coerceUnknownToError } from "./coerce-unknown-to-error"; /** + * ## DEPRECATED + * + * Instead of relying on this function, in the future, use the new `cause` + * property of the built-in `Error` type in combination + * with the `asError(unknown)` utility function: + * ```typescript + * import { asError } from "@hyperledger/cactus-common"; + * + * try { + * await performSomeImportantOperation(); + * } catch (ex: unknown) { + * const cause = asError(ex); + * throw new Error("Something went wrong while doing something.", { cause }); + * } + * ``` + * More information about the EcmaScript proposal that made this possible: + * https://github.com/tc39/proposal-error-cause + * + * ## The Old Documentation Prior to the Deprecation: * ### STANDARD EXCEPTION HANDLING - EXAMPLE WITH RE-THROW: * * Use the this utility function and pass in any throwable of whatever type and format @@ -77,6 +96,7 @@ import { coerceUnknownToError } from "./coerce-unknown-to-error"; * return result; // 42 * } * ``` + * @deprecated * * @param message The contextual information that will be passed into the * constructor of the returned {@link RuntimeError} instance. @@ -93,9 +113,28 @@ export function createRuntimeErrorWithCause( } /** + * ## DEPRECATED + * + * Instead of relying on this function, in the future, use the new `cause` + * property of the built-in `Error` type in combination + * with the `asError(unknown)` utility function: + * ```typescript + * import { asError } from "@hyperledger/cactus-common"; + * + * try { + * await performSomeImportantOperation(); + * } catch (ex: unknown) { + * const cause = asError(ex); + * throw new Error("Something went wrong while doing something.", { cause }); + * } + * ``` + * More information about the EcmaScript proposal that made this possible: + * https://github.com/tc39/proposal-error-cause + * * An alias to the `createRuntimeErrorWithCause` function for those prefering * a shorter utility for their personal style. * + * @deprecated * @see {@link createRuntimeErrorWithCause} * @returns `RuntimeError` */ diff --git a/tsconfig.base.json b/tsconfig.base.json index 1290f6141f..3867cf1474 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -9,13 +9,10 @@ }, /* Basic Options */ "incremental": true, /* Enable incremental compilation */ - "target": "ES2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ + "target": "ES2022", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ "module": "CommonJS", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ "lib": [ - "es2015", - "es2016", - "es2017", - "es2019", + "ES2022", "dom" ], /* Specify library files to be included in the compilation. */ // "allowJs": true, /* Allow javascript files to be compiled. */ diff --git a/tsconfig.json b/tsconfig.json index 600c138e5e..790f7474e6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -172,13 +172,10 @@ ], "compilerOptions": { "incremental": true, - "target": "ES2017", + "target": "ES2022", "module": "Node16", "lib": [ - "es2015", - "es2016", - "es2017", - "es2019", + "ES2022", "dom" ] /* Specify library files to be included in the compilation. */, // "allowJs": true, /* Allow javascript files to be compiled. */