/* Open in Val Town: https://www.val.town/v/std/catch */
/**
 * This script helps you debug vals - it catches JavaScript errors
 * and sends them up to the application so Townie can help fix them.
 */
function errorHandler(e: any) {
  window.top?.postMessage({
    type: "error",
    message: e.message || e?.target?.src,
  }, "*");
}

window.addEventListener("error", errorHandler);
window.addEventListener("document", errorHandler, true);

window.addEventListener("unhandledrejection", e => {
  window.top?.postMessage({
    type: "error",
    message: String(e.reason),
  }, "*");
});