-
Notifications
You must be signed in to change notification settings - Fork 29.6k
feat: forward browser errors/logs to terminal #80909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c342b9b
feat: forward logs/errors from browser to stdout/stderr
RobPruzan c78b682
old comment
RobPruzan 3057f6b
fix tests
RobPruzan 9a02c38
optional original stack frame again
RobPruzan 963596d
feat: forward logs/errors from browser to stdout/stderr
RobPruzan 2b32e37
fix types again
RobPruzan 0af61d1
guard every entrypoint with flag
RobPruzan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) Ruben Bridgewater | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 @@ | ||
{"name":"safe-stable-stringify","main":"index.js","author":"Ruben Bridgewater","license":"MIT"} |
114 changes: 114 additions & 0 deletions
114
packages/next/src/next-devtools/shared/forward-logs-shared.ts
This file contains hidden or 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,114 @@ | ||
export type LogMethod = | ||
| 'log' | ||
| 'info' | ||
| 'debug' | ||
| 'table' | ||
| 'error' | ||
| 'assert' | ||
| 'dir' | ||
| 'dirxml' | ||
| 'group' | ||
| 'groupCollapsed' | ||
| 'groupEnd' | ||
| 'trace' | ||
| 'warn' | ||
|
||
export type ConsoleEntry<T> = { | ||
kind: 'console' | ||
method: LogMethod | ||
consoleMethodStack: string | null | ||
args: Array< | ||
| { | ||
kind: 'arg' | ||
data: T | ||
} | ||
| { | ||
kind: 'formatted-error-arg' | ||
prefix: string | ||
stack: string | ||
} | ||
> | ||
} | ||
|
||
export type ConsoleErrorEntry<T> = { | ||
kind: 'any-logged-error' | ||
method: 'error' | ||
consoleErrorStack: string | ||
args: Array< | ||
| { | ||
kind: 'arg' | ||
data: T | ||
isRejectionMessage?: boolean | ||
} | ||
| { | ||
kind: 'formatted-error-arg' | ||
prefix: string | ||
stack: string | null | ||
} | ||
> | ||
} | ||
|
||
export type FormattedErrorEntry = { | ||
kind: 'formatted-error' | ||
prefix: string | ||
stack: string | ||
method: 'error' | ||
} | ||
|
||
export type ClientLogEntry = | ||
| ConsoleEntry<unknown> | ||
| ConsoleErrorEntry<unknown> | ||
| FormattedErrorEntry | ||
export type ServerLogEntry = | ||
| ConsoleEntry<string> | ||
| ConsoleErrorEntry<string> | ||
| FormattedErrorEntry | ||
|
||
export const UNDEFINED_MARKER = '__next_tagged_undefined' | ||
|
||
// Based on https://github.com/facebook/react/blob/28dc0776be2e1370fe217549d32aee2519f0cf05/packages/react-server/src/ReactFlightServer.js#L248 | ||
export function patchConsoleMethod<T extends keyof Console>( | ||
methodName: T, | ||
wrapper: ( | ||
methodName: T, | ||
...args: Console[T] extends (...args: infer P) => any ? P : never[] | ||
) => void | ||
): () => void { | ||
const descriptor = Object.getOwnPropertyDescriptor(console, methodName) | ||
if ( | ||
descriptor && | ||
(descriptor.configurable || descriptor.writable) && | ||
typeof descriptor.value === 'function' | ||
) { | ||
const originalMethod = descriptor.value as Console[T] extends ( | ||
...args: any[] | ||
) => any | ||
? Console[T] | ||
: never | ||
const originalName = Object.getOwnPropertyDescriptor(originalMethod, 'name') | ||
const wrapperMethod = function ( | ||
this: typeof console, | ||
...args: Console[T] extends (...args: infer P) => any ? P : never[] | ||
) { | ||
wrapper(methodName, ...args) | ||
|
||
originalMethod.apply(this, args) | ||
} | ||
if (originalName) { | ||
Object.defineProperty(wrapperMethod, 'name', originalName) | ||
} | ||
Object.defineProperty(console, methodName, { | ||
value: wrapperMethod, | ||
}) | ||
|
||
return () => { | ||
Object.defineProperty(console, methodName, { | ||
value: originalMethod, | ||
writable: descriptor.writable, | ||
configurable: descriptor.configurable, | ||
}) | ||
} | ||
} | ||
|
||
return () => {} | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/next/src/next-devtools/userspace/app/app-dev-overlay-setup.ts
This file contains hidden or 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 |
---|---|---|
@@ -1,5 +1,13 @@ | ||
import { patchConsoleError } from './errors/intercept-console-error' | ||
import { handleGlobalErrors } from './errors/use-error-handler' | ||
import { | ||
initializeDebugLogForwarding, | ||
isTerminalLoggingEnabled, | ||
} from './forward-logs' | ||
|
||
handleGlobalErrors() | ||
patchConsoleError() | ||
|
||
if (isTerminalLoggingEnabled) { | ||
initializeDebugLogForwarding('app') | ||
} |
This file contains hidden or 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 hidden or 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
26 changes: 26 additions & 0 deletions
26
packages/next/src/next-devtools/userspace/app/errors/use-forward-console-log.ts
This file contains hidden or 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,26 @@ | ||
import { useEffect } from 'react' | ||
import { isTerminalLoggingEnabled, logQueue } from '../forward-logs' | ||
import type { useWebsocket } from '../../../../client/dev/hot-reloader/app/use-websocket' | ||
|
||
export const useForwardConsoleLog = ( | ||
socketRef: ReturnType<typeof useWebsocket> | ||
) => { | ||
useEffect(() => { | ||
if (!isTerminalLoggingEnabled) { | ||
return | ||
} | ||
const socket = socketRef.current | ||
if (!socket) { | ||
return | ||
} | ||
|
||
const onOpen = () => { | ||
logQueue.onSocketReady(socket) | ||
} | ||
socket.addEventListener('open', onOpen) | ||
|
||
return () => { | ||
socket.removeEventListener('open', onOpen) | ||
} | ||
}, [socketRef]) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took this from #80793, we should sync when merged