Skip to content
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

enable useUnknownInCatchVariables typescript compiler #3361

Open
wants to merge 38 commits into
base: main
Choose a base branch
from

Conversation

rwat17
Copy link
Contributor

@rwat17 rwat17 commented Jun 26, 2024

  • enable useUnknownInCatchVariables flag in tsconfig

Closes: #2463

@@ -105,7 +105,7 @@ export class Containers {
return response;
} catch (ex) {
log.error("Failed to get diagnostics of Docker daemon", ex);
throw new RuntimeError("Failed to get diagnostics of Docker daemon", ex);
throw new RuntimeError(`Failed to get diagnostics of Docker daemon${ex}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rwat17 This completely disappears all the useful information that we could gain from the exception object. Please make sure to preserve the inner exceptions.

I won't post the same comment everywhere in the diff but please fix it everywhere.

message: "Internal Server Error",
error: ex?.stack || ex?.message,
});
const errorMsg = `Internal server Error`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rwat17 The underlying method call could've throw an HTTP 400 exception. We don't know here if it's internal server error or not (the original code is wrong on this too). I've created a PR to showcase how to use the new error handling utilities in this type of scenario:

https://github.com/hyperledger/cacti/pull/2869/files

Specifically if you look at this file in that PR you'll see that it doesn't say either side to be at fault (internal server error vs. user error) and instead just let's the underlying utility function decide on it based on the suggested generic logic:

packages/cactus-plugin-ledger-connector-besu/src/main/typescript/web-services/deploy-contract-solidity-bytecode-endpoint.ts

I won't post this review comment everywhere, but please address it across the PR.

Comment on lines +97 to +103
if (
typeof ex === "object" &&
ex !== null &&
"message" in ex &&
ex["message"] &&
typeof ex.message === "string"
) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rwat17 You can achieve the same type narrowing with

if (ex instanceof Error) {
    // now in here the compiler let's you have a
    // truthy ex.message string property to work with.
}

If for whatever reason the thrown exception wasn't an instance of Error then that's definitely an internal server error so it's OK for us to then fall through to the generic logic.

I won't post this comment at all the occurrences of this same code pattern but please make sure to address it across the board.

@@ -28,6 +28,12 @@ import { SetKeychainEntryEndpointV1 } from "./web-services/set-keychain-entry-en
import { HasKeychainEntryEndpointV1 } from "./web-services/has-keychain-entry-endpoint-v1";
import { DeleteKeychainEntryEndpointV1 } from "./web-services/delete-keychain-entry-endpoint-v1";

interface ErrorResponse extends Error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rwat17 Could the AxiosError type be used here instead of creating our own? The upside of the AxiosResponse type is that it ships with a custom type-guard function as well which you can use to narrow the type down with an if condition (isAxiosError())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AxiosError has .status not .statusCode property. This is associated with http error response. But i cant find proper interface to handle this case.

res.status(HttpStatus.INTERNAL_SERVER_ERROR);
res.statusMessage = ex.message;
res.json({ error: ex.stack });
res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rwat17 Please use the handleRestEndpointException utility instead of hardcoding the status code.

I won't post this everywhere but please address the same change across the PR

@RafaelAPB
Copy link
Contributor

Thanks for your contribution. LGTM, please make sure to incorporate Peter's feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

buid(tsc): re-enable useUnknownInCatchVariables typescript compiler
3 participants