Skip to content

Commit

Permalink
Implement console.error interception and logging for Tact compiler er…
Browse files Browse the repository at this point in the history
…rors
  • Loading branch information
rahulyadav-57 committed Apr 19, 2024
1 parent 8379ddc commit c5f8f77
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
16 changes: 16 additions & 0 deletions public/assets/js/log.js
Original file line number Diff line number Diff line change
@@ -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);
}
11 changes: 11 additions & 0 deletions src/components/workspace/WorkSpace/WorkSpace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand Down
2 changes: 2 additions & 0 deletions src/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Head, Html, Main, NextScript } from 'next/document';
import Script from 'next/script';

export default function Document() {
return (
Expand All @@ -14,6 +15,7 @@ export default function Document() {
<body>
<Main />
<NextScript />
<Script src="/assets/js/log.js" strategy="beforeInteractive" />
</body>
</Html>
);
Expand Down

1 comment on commit c5f8f77

@rahulyadav-57
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolves #19

Please sign in to comment.