question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when calling instanceof on the instance of the class extending the Error class

See original GitHub issue
Parcel Version: 1.12.4

Code sample

class CustomError extends Error {
  name = "CustomError";
}

try {
  const error = new CustomError("Error happened");
  console.log("TRY: error", error.constructor.name, JSON.stringify(error));
  console.log("TRY: instanceof CustomError", error instanceof CustomError);
  console.log("TRY: instanceof Error", error instanceof Error);
  throw error;
} catch (error) {
  console.log("CATCH: error", error.constructor.name, JSON.stringify(error));
  console.log("CATCH: instanceof CustomError", error instanceof CustomError);
  console.log("CATCH: instanceof Error", error instanceof Error);
}

Respositories

Output

TRY: error Error {"name":"CustomError"} 
TRY: instanceof CustomError  false
TRY: instanceof Error  true
CATCH: error Error {"name":"CustomError"} 
CATCH: instanceof CustomError  false
CATCH: instanceof Error  true

Expected output

TRY: error CustomError {"name":"CustomError"} 
TRY: instanceof CustomError  true
TRY: instanceof Error  true
CATCH: error CustomError {"name":"CustomError"} 
CATCH: instanceof CustomError  true
CATCH: instanceof Error  true

Description

I’ve switched my project from using Webpack to Parcel and suddenly some of the code stopped working. Parcel has problem with properly detecting class of an instance of the object that is extending built in class, like for example the Error class. It works correctly with custom classes but fails with built in ones. This bug is not present in Webpack. After comparing transpiled code I can see that Webpack is using @babel/runtime/helpers/possibleConstructorReturn helper function to initialize instance of the extended class. Is it something that can be easily fixed?

It also doesn’t work when I provide custom Babel config with the typescript transformer.

EDIT:

I’ve just found this issue: https://github.com/parcel-bundler/parcel/issues/2023

Which looks like it would fix my problem. But I see that it’s only fixed in Parcel 2. Are you planning to fix that in Parcel 1? Or is there an easy hack to allow that?

Also a solution using the parcel-plugin-babel-typescript plugin doesn’t work

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mischniccommented, Nov 19, 2019

I thought that I understand the target property in TS config. tcs compiles to version that is using the class keyword but apparently Parcel is still doing extra work after tsc and transpiles all the missing features (using Babel?) to satisfy browserslist. Is it correct?

Yes. I guess the pipelining aspect could be documented better. The root issue here is that typescript’s approach to transpiling classes doesn’t really work with builtins. I’ll make sure this is done automatically in Parcel 2.

0reactions
lukejagodzinskicommented, Nov 19, 2019

@mischnic Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why doesn't instanceof work on instances of Error ...
class MyError extends Error {} var e1 = new MyError(); var e2 = new Error(); ... specifically called this out as an edge-case...
Read more >
Custom errors, extending Error
We'll call it ValidationError and create a class for it. An error of that kind should also carry the information about the offending...
Read more >
Creating your own errors
class CustomError extends Error { constructor (message /* accept any custom arguments here */) { super(message) } } CustomError.prototype.name = 'CustomError';.
Read more >
JavaScript Custom Errors, Extending Error
In this chapter, we are going to dive into more details about errors in JavaScript. Learn what custom errors and extending error are...
Read more >
constructor - JavaScript - MDN Web Docs - Mozilla
class ValidationError extends Error { constructor(message) { super(message); // call parent class constructor this.name = "ValidationError"; ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found