11import { createParser , type EventSourceMessage , type EventSourceParser } from 'eventsource-parser'
22
3- import { ErrorEvent , syntaxError } from './errors.js'
3+ import { ErrorEvent , flattenError , syntaxError } from './errors.js'
44import type {
55 AddEventListenerOptions ,
66 EventListenerOptions ,
@@ -438,7 +438,7 @@ export class EventSource extends EventTarget {
438438 return
439439 }
440440
441- this . #scheduleReconnect( )
441+ this . #scheduleReconnect( flattenError ( err ) )
442442 }
443443
444444 /**
@@ -514,6 +514,7 @@ export class EventSource extends EventTarget {
514514 * Handles the process referred to in the EventSource specification as "failing a connection".
515515 *
516516 * @param error - The error causing the connection to fail
517+ * @param code - The HTTP status code, if available
517518 * @internal
518519 */
519520 #failConnection( error ?: string , code ?: number ) {
@@ -525,14 +526,11 @@ export class EventSource extends EventTarget {
525526
526527 // [spec] …and fires an event named `error` at the `EventSource` object.
527528 // [spec] Once the user agent has failed the connection, it does not attempt to reconnect.
528- const errorEvent = new ErrorEvent ( 'error' )
529-
530529 // [spec] > Implementations are especially encouraged to report detailed information
531530 // [spec] > to their development consoles whenever an error event is fired, since little
532531 // [spec] > to no information can be made available in the events themselves.
533532 // Printing to console is not very programatically helpful, though, so we emit a custom event.
534- errorEvent . code = code
535- errorEvent . message = error
533+ const errorEvent = new ErrorEvent ( 'error' , code , error )
536534
537535 this . #onError?.( errorEvent )
538536 this . dispatchEvent ( errorEvent )
@@ -541,9 +539,11 @@ export class EventSource extends EventTarget {
541539 /**
542540 * Schedules a reconnection attempt against the EventSource endpoint.
543541 *
542+ * @param error - The error causing the connection to fail
543+ * @param code - The HTTP status code, if available
544544 * @internal
545545 */
546- #scheduleReconnect( ) {
546+ #scheduleReconnect( error ?: string , code ?: number ) {
547547 // [spec] If the readyState attribute is set to CLOSED, abort the task.
548548 if ( this . #readyState === this . CLOSED ) {
549549 return
@@ -553,7 +553,7 @@ export class EventSource extends EventTarget {
553553 this . #readyState = this . CONNECTING
554554
555555 // [spec] Fire an event named `error` at the EventSource object.
556- const errorEvent = new ErrorEvent ( 'error' )
556+ const errorEvent = new ErrorEvent ( 'error' , code , error )
557557 this . #onError?.( errorEvent )
558558 this . dispatchEvent ( errorEvent )
559559
0 commit comments