From c5f8f7707e3ad8365ad81e118e8d4bfa2a948b16 Mon Sep 17 00:00:00 2001 From: Rahul Yadav Date: Fri, 19 Apr 2024 19:44:27 +0530 Subject: [PATCH] Implement console.error interception and logging for Tact compiler errors --- public/assets/js/log.js | 16 ++++++++++++++++ src/components/workspace/WorkSpace/WorkSpace.tsx | 11 +++++++++++ src/pages/_document.tsx | 2 ++ 3 files changed, 29 insertions(+) create mode 100644 public/assets/js/log.js diff --git a/public/assets/js/log.js b/public/assets/js/log.js new file mode 100644 index 0000000..04ff93b --- /dev/null +++ b/public/assets/js/log.js @@ -0,0 +1,16 @@ +// Backup the original console.error function +const originalConsoleError = console.error; + +// Override console.error +console.error = function (...args) { + logErrorToService(args); + // Call the original console.error function with all the arguments + originalConsoleError.apply(console, args); +}; + +function logErrorToService(args) { + const logEvent = new CustomEvent("consoleError", { + detail: { data: args }, + }); + document.dispatchEvent(logEvent); +} diff --git a/src/components/workspace/WorkSpace/WorkSpace.tsx b/src/components/workspace/WorkSpace/WorkSpace.tsx index fcfbf4e..a065728 100644 --- a/src/components/workspace/WorkSpace/WorkSpace.tsx +++ b/src/components/workspace/WorkSpace/WorkSpace.tsx @@ -93,10 +93,21 @@ const WorkSpace: FC = () => { type: 'TON-func', }); + document.addEventListener('consoleError', (e: any) => { + if (e?.detail?.data?.length === 0) return; + const _log = e.detail.data.join(', '); + // Some of the error aren't getting thrown by Tact compiler instead then are logged. So we need to check if the log contains '>' or 'compilation error'. This string is only present in the logs thrown by Tact compiler. + // console.error is not getting intercepted by the workspace because they stores reference to the original console.error method. So I have created global script(public/assets/js/log.js) which is getting loaded before any other script and it listens to the console.error and dispatches an event with the error message. + if (!(_log.includes('>') || _log.includes('compilation error'))) return; + + createLog(_log, 'error', true, true); + }); + return () => { console.log = originalConsoleLog; try { document.removeEventListener('keydown', () => {}); + document.removeEventListener('consoleError', () => {}); clearLog(); } catch (error) {} diff --git a/src/pages/_document.tsx b/src/pages/_document.tsx index a7daabf..ef3b629 100644 --- a/src/pages/_document.tsx +++ b/src/pages/_document.tsx @@ -1,4 +1,5 @@ import { Head, Html, Main, NextScript } from 'next/document'; +import Script from 'next/script'; export default function Document() { return ( @@ -14,6 +15,7 @@ export default function Document() {
+