Skip to content

Commit

Permalink
Prevent PHP errors from triggering modal
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrgicak committed Mar 22, 2024
1 parent ee0bd6f commit 978dec8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/php-wasm/logger/src/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export class Logger extends EventTarget {
new CustomEvent(this.fatalErrorEvent, {
detail: {
logs: this.getLogs(),
source: event.source,
},
})
);
Expand Down
4 changes: 4 additions & 0 deletions packages/php-wasm/universal/src/lib/base-php.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ export abstract class BasePHP implements IsomorphicLocalPHP {
);
// @ts-ignore
error.output = output;
// @ts-ignore
error.source = 'request';
console.error(error);
throw error;
}
Expand All @@ -296,6 +298,8 @@ export abstract class BasePHP implements IsomorphicLocalPHP {
this.dispatchEvent({
type: 'request.error',
error: e as Error,
// Distinguish between PHP request and PHP-wasm errors
source: (e as any).source ?? 'php-wasm',
});
throw e;
} finally {
Expand Down
1 change: 1 addition & 0 deletions packages/php-wasm/universal/src/lib/universal-php.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface PHPRequestEndEvent {
export interface PHPRequestErrorEvent {
type: 'request.error';
error: Error;
source?: 'request' | 'php-wasm';
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ export function ErrorReportModal() {

useEffect(() => {
addFatalErrorListener(logger, (e) => {
setShowModal(true);
setText('');
setLogs((e as CustomEvent).detail.logs.join(''));
setUrl(window.location.href);
const error = e as CustomEvent;
if (error.detail?.source === 'php-wasm') {
setShowModal(true);
setText('');
setLogs(error.detail.logs.join(''));
setUrl(window.location.href);
}
});
}, []);

Expand Down

0 comments on commit 978dec8

Please sign in to comment.