UNPKG

obj-error

Version:
70 lines 2.66 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const node_fetch_1 = __importDefault(require("node-fetch")); const urltoPost = (error, time) => { switch (typeof error) { case "string": return encodeURIComponent(`https://7lijj.sse.codesandbox.io/error?error=${error}&time=${time}`); default: return encodeURIComponent(`https://7lijj.sse.codesandbox.io/error?error=${error.message}&errorstack=${encodeURIComponent(error.stack)}&errorname=${error.name}&time=${time}`); } }; const message = "Error Was Sent TO OBJ OBJ go to this url to visit it"; class ObjError { constructor(error, { optout = false }) { this.time = new Date(); if (optout) { this.optout(error); } else { this.capture(error); } } optout(error) { const url = urltoPost(error, this.time); console.log("----------------------------------------------------------------------------"); console.warn(`🚀 ${message}: ${url}`); throw error; } capture(error) { if (typeof window === undefined) { window.onerror = (message, source, lineno, colno, error) => { const url = urltoPost(error, this.time); const fetch = window.fetch.bind(window); fetch(url) .then((data) => data.json()) .then((res) => { if (res.error) { console.log("----------------------------------------------------------------------------"); console.warn(`🚀 ${message}: ${url}`); } throw error; }) .catch((err) => { throw new Error(err); }); }; } else { const url = urltoPost(error, this.time); node_fetch_1.default(url) .then((data) => data.json()) .then((res) => { if (res.errorRecieved) { console.log("----------------------------------------------------------------------------"); console.warn(`🚀 ${message}: ${url}`); } throw error; }) .catch((err) => { throw new Error(err); }); } throw error; } } exports.ObjError = ObjError; //# sourceMappingURL=index.js.map