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

Catching exceptions in evalTS #94

Open
igor-elovikov opened this issue Aug 4, 2023 · 1 comment
Open

Catching exceptions in evalTS #94

igor-elovikov opened this issue Aug 4, 2023 · 1 comment

Comments

@igor-elovikov
Copy link

Does evalTS catch exceptions if I'm throwing them as throw new Error("message") from jsx?

Currently it just returns Error object so I've added this to evalTS:

if (res === "undefined") return resolve();
const parsed = JSON.parse(res);
if (parsed.name === "ReferenceError") {
  console.error("REFERENCE ERROR");
  reject(parsed);
} else if (parsed.name === "Error") {
  console.error(parsed.message);
  reject(parsed);
} else {
  resolve(parsed);
}

(again, I'm not very familiar with javascript so I "fixed" this by intuition, may be I'm missing something obvious, but currently throwing from jsx is basically returning error objects with type erasure as they are converted from json)

@andressuarezmonk
Copy link

I'm on the same boat than you, just made a slight improvement so exceptions are treated as actual exceptions:

if (parsed.name === "Error") {
  reject(new Error(parsed.message));
}

If you need it to handle custom errors it gets ugly, and you need to make sure you override the name property from the subclasses you create (as well as manually passing any additional properties you might want to pass), but is the best I could get:

if (parsed.name === "Error") {
  reject(new Error(parsed.message));
} else if (parsed.name === "CustomError") {
  reject(new CustomError(parsed.message, parsed.statusCode));
}

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

2 participants