bolt-cep icon indicating copy to clipboard operation
bolt-cep copied to clipboard

Catching exceptions in evalTS

Open igor-elovikov opened this issue 1 year ago • 1 comments

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)

igor-elovikov avatar Aug 04 '23 10:08 igor-elovikov

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));
}

andressuarezmonk avatar Aug 31 '23 19:08 andressuarezmonk