@@ -1189,12 +1189,12 @@ describe("Replicate client", () => {
1189
1189
// Continue with tests for other methods
1190
1190
1191
1191
describe ( "Stream" , ( ) => {
1192
- function createStream ( body : string | NodeJS . ReadableStream ) {
1192
+ function createStream ( body : string | NodeJS . ReadableStream , status = 200 ) {
1193
1193
const streamEndpoint = "https://stream.replicate.com" ;
1194
1194
nock ( streamEndpoint )
1195
1195
. get ( "/fake_stream" )
1196
1196
. matchHeader ( "Accept" , "text/event-stream" )
1197
- . reply ( 200 , body ) ;
1197
+ . reply ( status , body ) ;
1198
1198
1199
1199
return new Stream ( { url : `${ streamEndpoint } /fake_stream` , fetch } ) ;
1200
1200
}
@@ -1468,5 +1468,41 @@ describe("Replicate client", () => {
1468
1468
} ) ;
1469
1469
expect ( await iterator . next ( ) ) . toEqual ( { done : true } ) ;
1470
1470
} ) ;
1471
+
1472
+ test ( "an error event in the stream raises an exception" , async ( ) => {
1473
+ const stream = createStream (
1474
+ `
1475
+ event: output
1476
+ id: EVENT_1
1477
+ data: hello world
1478
+
1479
+ event: error
1480
+ id: EVENT_2
1481
+ data: An unexpected error occurred
1482
+
1483
+ `
1484
+ . trim ( )
1485
+ . replace ( / ^ [ ] + / gm, "" )
1486
+ ) ;
1487
+
1488
+ const iterator = stream [ Symbol . asyncIterator ] ( ) ;
1489
+ expect ( await iterator . next ( ) ) . toEqual ( {
1490
+ done : false ,
1491
+ value : { event : "output" , id : "EVENT_1" , data : "hello world" } ,
1492
+ } ) ;
1493
+ await expect ( iterator . next ( ) ) . rejects . toThrowError (
1494
+ "An unexpected error occurred"
1495
+ ) ;
1496
+ expect ( await iterator . next ( ) ) . toEqual ( { done : true } ) ;
1497
+ } ) ;
1498
+
1499
+ test ( "an error when fetching the stream raises an exception" , async ( ) => {
1500
+ const stream = createStream ( "{}" , 500 ) ;
1501
+ const iterator = stream [ Symbol . asyncIterator ] ( ) ;
1502
+ await expect ( iterator . next ( ) ) . rejects . toThrowError (
1503
+ "Request to https://stream.replicate.com/fake_stream failed with status 500 Internal Server Error: {}."
1504
+ ) ;
1505
+ expect ( await iterator . next ( ) ) . toEqual ( { done : true } ) ;
1506
+ } ) ;
1471
1507
} ) ;
1472
1508
} ) ;
0 commit comments