Skip to content

[no-throw-literal] Allow DOMException to be thrown in browser env #3712

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

Closed
3 tasks done
PaperStrike opened this issue Aug 6, 2021 · 3 comments
Closed
3 tasks done
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@PaperStrike
Copy link

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

{
  "env": {
    "browser": true
  },
  "rules": {
    "@typescript-eslint/no-throw-literal": ["error"]
  }
}
throw new DOMException('Aborted', 'AbortError');

Expected Result

No error.

Actual Result

Error, expected an error object to be thrown.

Additional Info

This seems more like a design problem. ESLint passes as their limitations of static analysis. ESLint isn't about to change the designed behaviour now. eslint/eslint#14880

Any web platform API using promises to represent operations that can be aborted must adhere to the following:

  • ...
  • Convey that the operation got aborted by rejecting the promise with an "AbortError" DOMException.
  • ...

—— Using AbortController and AbortSignal objects in APIs | DOM Standard

It is not always a good practice to only throw Error in browser environment, as it is encouraged to write a function like:

const abortableFunc = async ({ signal }) => {
  if (signal.aborted) {
    throw new DOMException('Aborted', 'AbortError');
  }
  // ...
}

Versions

package version
@typescript-eslint/eslint-plugin 4.29.0
@typescript-eslint/parser 4.29.0
TypeScript 4.3.5
ESLint 7.32.0
node 14.15.0
@PaperStrike PaperStrike added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Aug 6, 2021
@bradzacher
Copy link
Member

It's an oversight of sorts because DOMException is not declared as an extension of the Error base class.
https://github.com/microsoft/TypeScript/blob/c0796c1dfb62198976474c09350e98a1227d17fb/lib/lib.dom.d.ts#L3708-L3737

I think it's just a very, very, very rare usecase to use DOMException yourself, so it's not a case really anyone runs into.

@bradzacher bradzacher added enhancement: plugin rule option New rule option for an existing eslint-plugin rule and removed triage Waiting for team members to take a look labels Aug 6, 2021
@PaperStrike
Copy link
Author

PaperStrike commented Aug 6, 2021

Yes, it's rare to use currently. Well, DOM Standard gives an example that uses DOMException, in https://dom.spec.whatwg.org/#aborting-ongoing-activities .
The standard used the reject executer, and I prefer to use throw in async functions, which has the same functionality.

It may be widely used by browser library developers in the future. 👀

@PaperStrike
Copy link
Author

PaperStrike commented Aug 11, 2021

Ta-da! DOMException now extends Error in microsoft/TypeScript-DOM-lib-generator#1100.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

2 participants