-
-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1030@major: Adds logic for catching both synchronous and asynchronou…
…s errors in scripts, event listeners and timers. Adds a new package called "@happy-dom/uncaught-exception-observer". Adds a new virtual console that by default will output all log entries to a virtual console printer instead of using the global NodeJS console.
- Loading branch information
1 parent
7d4750e
commit 4570bac
Showing
24 changed files
with
1,243 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...om/src/console/IVirtualConsoleLogEntry.ts → .../console/types/IVirtualConsoleLogEntry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
58 changes: 58 additions & 0 deletions
58
packages/happy-dom/src/console/types/IVirtualConsolePrinter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import IVirtualConsoleLogEntry from './IVirtualConsoleLogEntry.js'; | ||
import VirtualConsoleLogLevelEnum from '../enums/VirtualConsoleLogLevelEnum.js'; | ||
import Event from '../../event/Event.js'; | ||
|
||
/** | ||
* Virtual console printer. | ||
*/ | ||
export default interface IVirtualConsolePrinter { | ||
/** | ||
* Writes to the output. | ||
* | ||
* @param logEntry Log entry. | ||
*/ | ||
print(logEntry: IVirtualConsoleLogEntry): void; | ||
|
||
/** | ||
* Clears the output. | ||
*/ | ||
clear(): void; | ||
|
||
/** | ||
* Adds an event listener. | ||
* | ||
* @param eventType Event type ("print" or "clear"). | ||
* @param listener Listener. | ||
*/ | ||
addEventListener(eventType: 'print' | 'clear', listener: (event: Event) => void): void; | ||
|
||
/** | ||
* Removes an event listener. | ||
* | ||
* @param eventType Event type ("print" or "clear"). | ||
* @param listener Listener. | ||
*/ | ||
removeEventListener(eventType: 'print' | 'clear', listener: (event: Event) => void): void; | ||
|
||
/** | ||
* Dispatches an event. | ||
* | ||
* @param event Event. | ||
*/ | ||
dispatchEvent(event: Event): void; | ||
|
||
/** | ||
* Reads the buffer. | ||
* | ||
* @returns Console log entries. | ||
*/ | ||
read(): IVirtualConsoleLogEntry[]; | ||
|
||
/** | ||
* Returns the buffer as a string. | ||
* | ||
* @param [logLevel] Log level. | ||
* @returns Buffer as a string of concatenated log entries. | ||
*/ | ||
readAsString(logLevel: VirtualConsoleLogLevelEnum): string; | ||
} |
100 changes: 100 additions & 0 deletions
100
packages/happy-dom/src/console/utilities/VirtualConsoleLogEntryStringifier.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import IVirtualConsoleLogEntry from '../types/IVirtualConsoleLogEntry.js'; | ||
import VirtualConsoleLogTypeEnum from '../enums/VirtualConsoleLogTypeEnum.js'; | ||
|
||
/** | ||
* Virtual console utility. | ||
*/ | ||
export default class VirtualConsoleLogEntryStringifier { | ||
/** | ||
* Stringifies a log entry. | ||
* | ||
* @param logEntry Log entry. | ||
* @returns Stringified message. | ||
*/ | ||
public static toString(logEntry: IVirtualConsoleLogEntry): string { | ||
if (this.isLogEntryCollapsed(logEntry)) { | ||
return ''; | ||
} | ||
|
||
const tabbing = this.getLogEntryGroupTabbing(logEntry); | ||
let output = tabbing; | ||
for (const part of logEntry.message) { | ||
output += output !== '' && output !== tabbing ? ' ' : ''; | ||
if ( | ||
typeof part === 'object' && | ||
(part === null || part.constructor.name === 'Object' || Array.isArray(part)) | ||
) { | ||
try { | ||
output += JSON.stringify(part); | ||
} catch (error) { | ||
output += new Error('Failed to JSON stringify object in log entry.').stack.replace( | ||
/\n at/gm, | ||
'\n ' + tabbing + 'at' | ||
); | ||
} | ||
} else if (typeof part === 'object' && part['message'] && part['stack']) { | ||
output += part['stack'].replace(/\n at/gm, '\n ' + tabbing + 'at'); | ||
} else { | ||
output += this.getLogEntryIcon(logEntry) + String(part); | ||
} | ||
} | ||
return output + '\n'; | ||
} | ||
|
||
/** | ||
* Gets the log entry icon. | ||
* | ||
* @param logEntry Log entry. | ||
* @returns Icon. | ||
*/ | ||
private static getLogEntryIcon(logEntry: IVirtualConsoleLogEntry): string { | ||
switch (logEntry.type) { | ||
case VirtualConsoleLogTypeEnum.group: | ||
return '▼ '; | ||
case VirtualConsoleLogTypeEnum.groupCollapsed: | ||
return '▶ '; | ||
} | ||
return ''; | ||
} | ||
|
||
/** | ||
* Gets the log entry group tabbing. | ||
* | ||
* @param logEntry Log entry. | ||
* @returns Tabbing. | ||
*/ | ||
private static getLogEntryGroupTabbing(logEntry: IVirtualConsoleLogEntry): string { | ||
let tabs = ''; | ||
let group = | ||
logEntry.type === VirtualConsoleLogTypeEnum.group || | ||
logEntry.type === VirtualConsoleLogTypeEnum.groupCollapsed | ||
? logEntry.group?.parent | ||
: logEntry.group; | ||
while (group) { | ||
tabs += ' '; | ||
group = group.parent; | ||
} | ||
return tabs; | ||
} | ||
|
||
/** | ||
* Checks if the log entry content is collapsed. | ||
* | ||
* @param logEntry Log entry. | ||
* @returns True if collapsed. | ||
*/ | ||
private static isLogEntryCollapsed(logEntry: IVirtualConsoleLogEntry): boolean { | ||
let group = | ||
logEntry.type === VirtualConsoleLogTypeEnum.group || | ||
logEntry.type === VirtualConsoleLogTypeEnum.groupCollapsed | ||
? logEntry.group?.parent | ||
: logEntry.group; | ||
while (group) { | ||
if (group.collapsed) { | ||
return true; | ||
} | ||
group = group.parent; | ||
} | ||
return false; | ||
} | ||
} |
Oops, something went wrong.