Skip to content

Commit

Permalink
Ensure PromiseRejectionEvent has reason before logging it (#1150)
Browse files Browse the repository at this point in the history
Fixes #1149

## What is this PR doing?

Checks if `event.reason` or `event.reason.stack` exist in
`logUnhandledRejection`.

## What problem is it solving?

Fixes a JS error when a promise without a reason is thrown.

## How is the problem addressed?

By checking if a reason is provided.

## Testing Instructions

- Checkout this branch
- Start Playground `npm run dev`
- Open Playground and execute this in the browser console
```
Promise.reject();
```
- Confirm that logger doesn't throw an error
  • Loading branch information
bgrgicak authored Mar 29, 2024
1 parent 8ce6087 commit 907aa25
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/php-wasm/logger/src/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ export class Logger extends EventTarget {
* @param PromiseRejectionEvent event
*/
private logUnhandledRejection(event: PromiseRejectionEvent) {
this.log(`${event.reason.stack}`, 'Error');
// No reason was provided, so we can't log anything.
if (!event?.reason) {
return;
}
const message = event?.reason.stack ?? event.reason;
this.log(message, 'Error');
}

/**
Expand Down

0 comments on commit 907aa25

Please sign in to comment.