obj-error
Version:
Object Object Error
77 lines (71 loc) • 2.15 kB
text/typescript
import Nodefetch from "node-fetch";
const urltoPost = (error: Error | string, time: Date) => {
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}&errorname=${error.name}&time=${time}`
);
}
};
const message = "Error Was Sent TO OBJ OBJ go to this url to visit it";
export class ObjError {
public time = new Date();
constructor(error: Error, { optout = false }: { optout?: boolean }) {
if (optout) {
this.optout(error);
} else {
this.capture(error);
}
}
optout(error: Error) {
const url = urltoPost(error, this.time);
console.log(
"----------------------------------------------------------------------------"
);
console.warn(`🚀 ${message}: ${url}`);
throw error;
}
capture(error: 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);
Nodefetch(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;
}
}