Skip to content

Wrong instance of error in ES5 #3

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

Open
PunGy opened this issue Jan 6, 2021 · 4 comments
Open

Wrong instance of error in ES5 #3

PunGy opened this issue Jan 6, 2021 · 4 comments

Comments

@PunGy
Copy link

PunGy commented Jan 6, 2021

Custom fetcher errors from error.ts are inherit as ES6 class from the basic Error class.
It's okey in ES6, but when it transpiles to es5 - basic function inheritance throws error before we correctly define out class. And because of that, for example const error = new NetworkError ('Some troubles') is not the instance of NetworkError, it's always instance of Error.

One of possible decisions - create custom general Error class which isn't automaticaly throw:

class Error_ {
  name: string;

  message: string;

  readonly stack?: string;

  constructor(message: string) {
    this.name = 'Error';
    this.message = message;
    this.stack = (new Error()).stack;
  }
}
export class NetworkError extends Error_

image
image

@PunGy PunGy changed the title Wrong instance of error Wrong instance of error in ES5 Jan 6, 2021
@kalda341
Copy link

I'm also encountering this, and it's a dealbreaker. Another option is to leave it as ES6?

@anilanar
Copy link

Wouldn't it be better to use a tagged union type for errors instead of using prototype inheritence?

@PunGy
Copy link
Author

PunGy commented Jul 13, 2021

Actually, I think a better way of resolving that issue: using the babel transpiler(maybe with a bundler like webpack), because it an issue of TypeScript compiler microsoft/TypeScript#10166
But seems like this repo is no longer maintained :(
@anilanar Don't really think it would be better in such context. Also, creating new error types by inheritance is a very popular and well-used way in JavaScript

@anilanar
Copy link

anilanar commented Jul 13, 2021

@PunGy That's true in Javascript (and possibly also Typescript) but perhaps not in "fp-ts" ecosystem simply because pattern matching on the error type can never be exhaustive because the type system doesn't know all possible subtypes of Error when pattern matching with instanceof. Just something to keep in mind.


Ah nevermind, you are actually using a union type for the error.

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

No branches or pull requests

3 participants