-
Notifications
You must be signed in to change notification settings - Fork 295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(cactus-common): add createRuntimeErrorWithCause() & newRex() #1707
feat(cactus-common): add createRuntimeErrorWithCause() & newRex() #1707
Conversation
0cb53d0
to
af33bee
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@izuru0: Thank you very much for reviewing this PR. |
As an example on how to integrate / use the new generic functions the cactus-cmd-api-server was taken as a show case |
af33bee
to
8dd17e9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@m-courtin Can you make it support these (and similar) cases of incorrect error throwing as well? Right now these would all result in the loss of the information that the thrower was trying to convey (which would be their fault for not throwing an exception, but since the language allows for it we have to assume it will happen...)
try {
throw "Something very important about what went wrong";
} catch (ex) {
const theStack = LogHelper.getExceptionStack(ex);
const theMessage = LogHelper.getExceptionMessage(ex);
console.log(theStack);
console.log(theMessage);
}
try {
throw { error: "Something very important about what went wrong" };
} catch (ex) {
const theStack = LogHelper.getExceptionStack(ex);
const theMessage = LogHelper.getExceptionMessage(ex);
console.log(theStack);
console.log(theMessage);
}
The other thing I'd ask is to check for the input being a specific instance of RuntimeError
so that this also does not lose information:
const exA = new RuntimeError("A");
const exB = new RuntimeError("B", exA);
const exC = new RuntimeError("C", exB);
// The nested exceptions here are swallowed, the only stack that is shown
// is the last one (for "C")
const x = LogHelper.getExceptionStack(exC);
console.log(x);
JSON stringifying the RuntimeError
above will preserve the full chain[1] of nested exceptions, but with the current code it only preserves the last one.
[1]:
{
"name": "RuntimeError",
"stack": "RuntimeError: C\n at /..../cactus-1/packages/cactus-common/src/test/typescript/unit/logging/log-helper.test.ts:9:15\n at _dispatchDescribe (/..../cactus-1/node_modules/jest-circus/build/index.js:97:26)\n at describe (/..../cactus-1/node_modules/jest-circus/build/index.js:60:5)\n at Object.<anonymous> (/..../cactus-1/packages/cactus-common/src/test/typescript/unit/logging/log-helper.test.ts:6:1)\n at Runtime._execModule (/..../cactus-1/node_modules/jest-runtime/build/index.js:1646:24)\n at Runtime._loadModule (/..../cactus-1/node_modules/jest-runtime/build/index.js:1185:12)\n at Runtime.requireModule (/..../cactus-1/node_modules/jest-runtime/build/index.js:1009:12)\n at jestAdapter (/..../cactus-1/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:13)\n at runTestInternal (/..../cactus-1/node_modules/jest-runner/build/runTest.js:389:16)\n at runTest (/..../cactus-1/node_modules/jest-runner/build/runTest.js:475:34)",
"message": "C",
"cause": {
"name": "RuntimeError",
"stack": "RuntimeError: B\n at /..../cactus-1/packages/cactus-common/src/test/typescript/unit/logging/log-helper.test.ts:8:15\n at _dispatchDescribe (/..../cactus-1/node_modules/jest-circus/build/index.js:97:26)\n at describe (/..../cactus-1/node_modules/jest-circus/build/index.js:60:5)\n at Object.<anonymous> (/..../cactus-1/packages/cactus-common/src/test/typescript/unit/logging/log-helper.test.ts:6:1)\n at Runtime._execModule (/..../cactus-1/node_modules/jest-runtime/build/index.js:1646:24)\n at Runtime._loadModule (/..../cactus-1/node_modules/jest-runtime/build/index.js:1185:12)\n at Runtime.requireModule (/..../cactus-1/node_modules/jest-runtime/build/index.js:1009:12)\n at jestAdapter (/..../cactus-1/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:13)\n at runTestInternal (/..../cactus-1/node_modules/jest-runner/build/runTest.js:389:16)\n at runTest (/..../cactus-1/node_modules/jest-runner/build/runTest.js:475:34)",
"message": "B",
"cause": {
"name": "RuntimeError",
"stack": "RuntimeError: A\n at /..../cactus-1/packages/cactus-common/src/test/typescript/unit/logging/log-helper.test.ts:7:15\n at _dispatchDescribe (/..../cactus-1/node_modules/jest-circus/build/index.js:97:26)\n at describe (/..../cactus-1/node_modules/jest-circus/build/index.js:60:5)\n at Object.<anonymous> (/..../cactus-1/packages/cactus-common/src/test/typescript/unit/logging/log-helper.test.ts:6:1)\n at Runtime._execModule (/..../cactus-1/node_modules/jest-runtime/build/index.js:1646:24)\n at Runtime._loadModule (/..../cactus-1/node_modules/jest-runtime/build/index.js:1185:12)\n at Runtime.requireModule (/..../cactus-1/node_modules/jest-runtime/build/index.js:1009:12)\n at jestAdapter (/..../cactus-1/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:13)\n at runTestInternal (/..../cactus-1/node_modules/jest-runner/build/runTest.js:389:16)\n at runTest (/..../cactus-1/node_modules/jest-runner/build/runTest.js:475:34)",
"message": "A",
"cause": null
}
}
}
@petermetz: Yes, good point to support similar "false" exceptions as well and try to get their message / load. I will extend the generic handling accordingly |
9664449
to
0707950
Compare
@m-courtin Also how about something like this for the stack extraction? I came to realize that the export const K_FALLBACK_STACK = "NO_STACK_INFORMATION_INCLUDED_IN_EXCEPTION";
export class LogHelper {
public static getExceptionStack(exception: unknown): string {
if (exception instanceof RuntimeError) {
return stringify(exception);
} else if (exception instanceof Error) {
return stringify(exception.stack);
} else if (exception) {
return stringify(exception);
} else {
return K_FALLBACK_STACK;
}
} |
packages/cactus-common/src/main/typescript/logging/log-helper.ts
Outdated
Show resolved
Hide resolved
packages/cactus-common/src/main/typescript/logging/log-helper.ts
Outdated
Show resolved
Hide resolved
Depends on hyperledger-cacti#1707 Fixes hyperledger-cacti#1741 Signed-off-by: Youngone Lee <youngone.lee@accenture.com>
Depends on hyperledger-cacti#1707 Fixes hyperledger-cacti#1741 Signed-off-by: Youngone Lee <youngone.lee@accenture.com>
@petermetz: Regarding the stack handling I would prefer to use the |
dbaf4e5
to
f514b32
Compare
Depends on hyperledger-cacti#1707 Fixes hyperledger-cacti#1741 Signed-off-by: Youngone Lee <youngone.lee@accenture.com>
Depends on hyperledger-cacti#1707 Fixes hyperledger-cacti#1735 Signed-off-by: Youngone Lee <youngone.lee@accenture.com>
3bf237d
to
da0409d
Compare
packages/cactus-common/src/main/typescript/exception/exception-helper.ts
Outdated
Show resolved
Hide resolved
aa701f9
to
56b5dcc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@m-courtin Sorry I meant the entire newRuntimeError function's code, not the code of the getExceptionMessageInfo function which is being invoked on the line I'm commenting on. Apologies for the confusion!
732d8f5
to
8b2bec7
Compare
8b2bec7
to
0a4fbd6
Compare
packages/cactus-common/src/main/typescript/exception/exception-helper.ts
Outdated
Show resolved
Hide resolved
packages/cactus-common/src/main/typescript/exception/exception-helper.ts
Outdated
Show resolved
Hide resolved
795879f
to
c5d9f14
Compare
2fb2068
to
1e9d045
Compare
1e9d045
to
c2682ba
Compare
ad346d0
to
9c329a1
Compare
Utility functions to conveniently re-throw excpetions typed as unknown by their catch block (which is the default since Typescript v4.4). Example usage can and much more documentation can be seen here: `packages/cactus-common/src/main/typescript/exception/create-runtime-error-with-cause.ts` and here `packages/cactus-common/src/test/typescript/unit/exception/create-runtime-error-with-cause.test.ts` Co-authored-by: Peter Somogyvari <peter.somogyvari@accenture.com> Closes: hyperledger-cacti#1702 [skip ci] Signed-off-by: Michael Courtin <michael.courtin@accenture.com> Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
9c329a1
to
b3a508c
Compare
Utility functions to conveniently re-throw excpetions typed as unknown
by their catch block (which is the default since Typescript v4.4).
Example usage can and much more documentation can be seen here:
packages/cactus-common/src/main/typescript/exception/create-runtime-error-with-cause.ts
and here
packages/cactus-common/src/test/typescript/unit/exception/create-runtime-error-with-cause.test.ts
Co-authored-by: Peter Somogyvari peter.somogyvari@accenture.com
Closes: #1702
[skip ci]
Signed-off-by: Michael Courtin michael.courtin@accenture.com
Signed-off-by: Peter Somogyvari peter.somogyvari@accenture.com