@@ -2,13 +2,19 @@ import {parseMultipart} from '@mjackson/multipart-parser';
2
2
import qs from 'qs' ;
3
3
4
4
import {
5
+ isErrorChunk ,
5
6
isKeepAliveChunk ,
6
7
isQueryResponseChunk ,
7
8
isSessionChunk ,
8
9
isStreamDataChunk ,
9
10
} from '../../store/reducers/query/utils' ;
10
11
import type { Actions , StreamQueryParams } from '../../types/api/query' ;
11
- import type { QueryResponseChunk , SessionChunk , StreamDataChunk } from '../../types/store/streaming' ;
12
+ import type {
13
+ QueryResponseChunk ,
14
+ SessionChunk ,
15
+ StreamDataChunk ,
16
+ StreamingChunk ,
17
+ } from '../../types/store/streaming' ;
12
18
import {
13
19
BINARY_DATA_IN_PLAIN_TEXT_DISPLAY ,
14
20
DEV_ENABLE_TRACING_FOR_ALL_REQUESTS ,
@@ -94,24 +100,34 @@ export class StreamingAPI extends BaseYdbAPI {
94
100
const traceId = response . headers . get ( 'traceresponse' ) ?. split ( '-' ) [ 1 ] ;
95
101
96
102
await parseMultipart ( response . body , { boundary : BOUNDARY } , async ( part ) => {
103
+ const text = await part . text ( ) ;
104
+
105
+ let chunk : unknown ;
97
106
try {
98
- const chunk = JSON . parse ( await part . text ( ) ) ;
99
-
100
- if ( isSessionChunk ( chunk ) ) {
101
- const sessionChunk = chunk ;
102
- sessionChunk . meta . trace_id = traceId ;
103
- options . onSessionChunk ( chunk ) ;
104
- } else if ( isStreamDataChunk ( chunk ) ) {
105
- options . onStreamDataChunk ( chunk ) ;
106
- } else if ( isQueryResponseChunk ( chunk ) ) {
107
- options . onQueryResponseChunk ( chunk ) ;
108
- } else if ( isKeepAliveChunk ( chunk ) ) {
109
- // Logging for debug purposes
110
- console . info ( 'Received keep alive chunk' ) ;
111
- }
107
+ chunk = JSON . parse ( text ) ;
112
108
} catch ( e ) {
113
109
throw new Error ( `Error parsing chunk: ${ e } ` ) ;
114
110
}
111
+
112
+ if ( isErrorChunk ( chunk ) ) {
113
+ await response . body ?. cancel ( ) . catch ( ( ) => { } ) ;
114
+ throw chunk ;
115
+ }
116
+
117
+ const streamingChunk = chunk as StreamingChunk ;
118
+
119
+ if ( isSessionChunk ( streamingChunk ) ) {
120
+ const sessionChunk = streamingChunk ;
121
+ sessionChunk . meta . trace_id = traceId ;
122
+ options . onSessionChunk ( streamingChunk ) ;
123
+ } else if ( isStreamDataChunk ( streamingChunk ) ) {
124
+ options . onStreamDataChunk ( streamingChunk ) ;
125
+ } else if ( isQueryResponseChunk ( streamingChunk ) ) {
126
+ options . onQueryResponseChunk ( streamingChunk ) ;
127
+ } else if ( isKeepAliveChunk ( streamingChunk ) ) {
128
+ // Logging for debug purposes
129
+ console . info ( 'Received keep alive chunk' ) ;
130
+ }
115
131
} ) ;
116
132
}
117
133
}
0 commit comments