debugLogger.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. if (typeof window !== "undefined") {
  2. if(! window.error_handler_installed) {
  3. window.error_handler_logs = [];
  4. const handler = ((old) => ({
  5. get: (_, name) => {
  6. function passf() {
  7. old[name].apply(null, arguments);
  8. }
  9. function logf() {
  10. const logEntry = {
  11. type: name,
  12. ts: +new Date(),
  13. arguments,
  14. };
  15. window.error_handler_logs.push(logEntry);
  16. const logsUrl = new URL(`${window.location.protocol}//${window.location.hostname}:${window.location.port}/api/dataHandler`);
  17. logsUrl.searchParams.append("type", "logs");
  18. const apiEnabled = localStorage.getItem("chatvrm_external_api_enabled");
  19. if (window.location.hostname === "localhost" && apiEnabled === "true") {
  20. fetch(logsUrl, {
  21. method: "POST",
  22. headers: { "Content-Type": "application/json" },
  23. body: JSON.stringify(logEntry),
  24. });
  25. }
  26. passf.apply(null, arguments);
  27. }
  28. switch (name) {
  29. case 'log':
  30. case 'debug':
  31. case 'info':
  32. case 'warn':
  33. case 'error':
  34. return logf;
  35. default:
  36. return passf;
  37. }
  38. }
  39. }))(window.console);
  40. window.console = new Proxy({}, handler);
  41. window.addEventListener("error", (e) => {
  42. console.error(`Error occurred: ${e.error.message} ${e.error.stack}`);
  43. return false;
  44. });
  45. window.addEventListener("unhandledrejection", (e) => {
  46. console.error(`Unhandled rejection: ${e.message}`);
  47. return false;
  48. });
  49. window.error_handler_installed = true;
  50. }
  51. }