mirror of
https://github.com/QwenLM/qwen-code.git
synced 2026-08-08 00:05:37 +00:00
fix(cli): surface unhandled rejections and render errors instead of swallowing them (#7406)
* fix(cli): surface unhandled rejections and render errors instead of swallowing them Unhandled promise rejections were emitted to AppEvent.LogError which has no production listener, making crashes completely invisible in debug logs and stderr. Additionally, the main App tree had no top-level ErrorBoundary, so React render errors were caught only by Ink's internal boundary which silently exits via exitPromise.catch(noop). - Write unhandled rejection details to the debug logger so they appear in ~/.qwen/debug/<session>.txt - Wrap the interactive UI tree in an ErrorBoundary that logs fatal render errors and shows a fallback message instead of silent exit * fix(cli): address review feedback — normalize non-Error throws and add exit path - Normalize non-Error thrown values (strings, null, etc.) to Error instances in ErrorBoundary before logging/rendering, preventing secondary TypeErrors from sanitizeTerminalText(undefined) - Schedule a graceful exit (5s delay) from the fatal render error fallback so the session does not hang under the Kitty keyboard protocol where Ctrl+C is a keypress, not SIGINT * test(cli): add test for non-Error thrown value normalization in ErrorBoundary --------- Co-authored-by: 秦奇 <gary.gq@alibaba-inc.com>
This commit is contained in:
parent
e58f995fae
commit
93af84f248
4 changed files with 55 additions and 9 deletions
|
|
@ -181,6 +181,7 @@ Stack trace:
|
|||
${reason.stack}`
|
||||
: ''
|
||||
}`;
|
||||
debugLogger.error(errorMessage);
|
||||
appEvents.emit(AppEvent.LogError, errorMessage);
|
||||
if (!unhandledRejectionOccurred) {
|
||||
unhandledRejectionOccurred = true;
|
||||
|
|
|
|||
|
|
@ -15,6 +15,11 @@ const Thrower = ({ message }: { message: string }) => {
|
|||
throw new Error(message);
|
||||
};
|
||||
|
||||
// A child that throws a non-Error value (string).
|
||||
const StringThrower = ({ message }: { message: string }) => {
|
||||
throw message;
|
||||
};
|
||||
|
||||
describe('ErrorBoundary', () => {
|
||||
// React logs caught render errors to console.error; silence it so the test
|
||||
// output stays clean (the boundary catching the error is the point).
|
||||
|
|
@ -103,4 +108,20 @@ describe('ErrorBoundary', () => {
|
|||
rerender(tree);
|
||||
expect(lastFrame()).toContain('recovered');
|
||||
});
|
||||
|
||||
it('normalizes a non-Error thrown value to an Error instance', () => {
|
||||
const onError = vi.fn();
|
||||
const { lastFrame } = render(
|
||||
<ErrorBoundary onError={onError}>
|
||||
<StringThrower message="string error" />
|
||||
</ErrorBoundary>,
|
||||
);
|
||||
// The fallback renders the stringified value.
|
||||
expect(lastFrame()).toContain('string error');
|
||||
// onError receives a proper Error instance, not the raw string.
|
||||
expect(onError).toHaveBeenCalledTimes(1);
|
||||
const [error] = onError.mock.calls[0];
|
||||
expect(error).toBeInstanceOf(Error);
|
||||
expect((error as Error).message).toBe('string error');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -9,6 +9,13 @@ import { Box, Text } from 'ink';
|
|||
import { theme } from '../../semantic-colors.js';
|
||||
import { sanitizeTerminalText } from '../../utils/textUtils.js';
|
||||
|
||||
function normalizeError(error: unknown): Error {
|
||||
if (error instanceof Error) {
|
||||
return error;
|
||||
}
|
||||
return new Error(String(error));
|
||||
}
|
||||
|
||||
interface ErrorBoundaryProps {
|
||||
children: ReactNode;
|
||||
/**
|
||||
|
|
@ -38,12 +45,12 @@ export class ErrorBoundary extends Component<
|
|||
> {
|
||||
override state: ErrorBoundaryState = { error: null };
|
||||
|
||||
static getDerivedStateFromError(error: Error): ErrorBoundaryState {
|
||||
return { error };
|
||||
static getDerivedStateFromError(error: unknown): ErrorBoundaryState {
|
||||
return { error: normalizeError(error) };
|
||||
}
|
||||
|
||||
override componentDidCatch(error: Error, info: ErrorInfo): void {
|
||||
this.props.onError?.(error, info);
|
||||
override componentDidCatch(error: unknown, info: ErrorInfo): void {
|
||||
this.props.onError?.(normalizeError(error), info);
|
||||
}
|
||||
|
||||
private readonly reset = () => {
|
||||
|
|
|
|||
|
|
@ -33,7 +33,8 @@ import {
|
|||
} from './utils/kittyProtocolDetector.js';
|
||||
import { installTerminalRedrawOptimizer } from './utils/terminalRedrawOptimizer.js';
|
||||
import { installSynchronizedOutput } from './utils/synchronizedOutput.js';
|
||||
import { registerCleanup } from '../utils/cleanup.js';
|
||||
import { ErrorBoundary } from './components/shared/ErrorBoundary.js';
|
||||
import { registerCleanup, runExitCleanup } from '../utils/cleanup.js';
|
||||
import { stopAndGetCapturedInput } from '../utils/earlyInputCapture.js';
|
||||
import { profileCheckpoint } from '../utils/startupProfiler.js';
|
||||
import { writeStderrLine } from '../utils/stdioHelpers.js';
|
||||
|
|
@ -195,13 +196,29 @@ export async function startInteractiveUI(
|
|||
// coordinates even though these listeners are owned and cleaned up.
|
||||
process.stdout.setMaxListeners(0);
|
||||
}
|
||||
const appTree = (
|
||||
<ErrorBoundary
|
||||
onError={(error, info) => {
|
||||
debugLogger.error(
|
||||
`[FATAL_RENDER_ERROR] ${error.message}\n${info.componentStack ?? ''}\n${error.stack ?? ''}`,
|
||||
);
|
||||
// The fallback replaces AppWrapper, unmounting KeypressProvider and
|
||||
// Ctrl+C handling. Schedule a graceful exit so the session does not
|
||||
// hang (e.g. under the Kitty keyboard protocol where Ctrl+C is a
|
||||
// keypress, not SIGINT).
|
||||
setTimeout(() => {
|
||||
void runExitCleanup().then(() => process.exit(1));
|
||||
}, 5000);
|
||||
}}
|
||||
>
|
||||
<AppWrapper />
|
||||
</ErrorBoundary>
|
||||
);
|
||||
const instance = render(
|
||||
process.env['DEBUG'] ? (
|
||||
<React.StrictMode>
|
||||
<AppWrapper />
|
||||
</React.StrictMode>
|
||||
<React.StrictMode>{appTree}</React.StrictMode>
|
||||
) : (
|
||||
<AppWrapper />
|
||||
appTree
|
||||
),
|
||||
{
|
||||
exitOnCtrlC: false,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue