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

Suggestion: Checked & Unchecked Exceptions #23060

Closed
bennycode opened this issue Apr 2, 2018 · 2 comments
Closed

Suggestion: Checked & Unchecked Exceptions #23060

bennycode opened this issue Apr 2, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@bennycode
Copy link
Contributor

Scenario

Node.js introduced a UnhandledPromiseRejectionWarning to make developers aware of handling promise rejections. In the future, promise rejections that are not handled will terminate the Node.js process which will stop applications from continuing to function.

Solution

When using code, it's hard to say if that particular code throws an exception or not. That's why I suggest to implement a concept similar to checked exceptions in Java. Checked exceptions need to get handled by the programmer with a try/catch block or a throws clause. It would be great if TypeScript supports such a concept even for Promise-based code so that you can force developers to catch Promise rejections.

Proposed Change

In the short term, it might be helpful if tsc detects whether a method is marked with @throws, so that the developer can be informed about error cases to handle when using such methods. This way, the change would not interfere with ES6 or ES7 syntax because it's limited to parsing comments in JavaScript.

Code Sample

provider.ts

...

/**
 * Decode a serialized public key.
 * @param {CBOR.Decoder} decoder - Payload
 * @returns {PublicKey} Deserialized public key.
 * @throws {ConversionError}
 */
static decode(decoder: CBOR.Decoder): PublicKey {
  const nprops = decoder.object();
  for (let index = 0; index < nprops; index++) {
    switch (decoder.u8()) {
      case 0:
        this.pub_edward = new Uint8Array(decoder.bytes());
        break;
      default:
        decoder.skip();
    }
  }

  const pub_curve = ed2curve.convertPublicKey(this.pub_edward);
  if (pub_curve) {
    this.pub_curve = pub_curve;
    return this;
  }

  throw new ConversionError('Could not convert private key with ed2curve.');
}

consumer.ts

...
decode(decoder); // tsc will warn the developer when transpiling, because `decode` declares
                 // to throw a `ConversionError`, which is unhandled in `consumer.ts`
...
@mhegazy
Copy link
Contributor

mhegazy commented Apr 2, 2018

looks like a duplicate of #13219

@mhegazy mhegazy added the Duplicate An existing issue was already created label Apr 2, 2018
@bennycode
Copy link
Contributor Author

@mhegazy: You're right. Thanks for the hint.

I will close my issue because #13219 is still open (keeping fingers crossed 🤞).

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants