File tree Expand file tree Collapse file tree 4 files changed +64
-19
lines changed Expand file tree Collapse file tree 4 files changed +64
-19
lines changed Original file line number Diff line number Diff line change @@ -9,14 +9,27 @@ export async function POST(req: NextRequest) {
9
9
const pipe = new Pipe ( pipeSummary ( ) ) ;
10
10
11
11
// 2. Run the Pipe.
12
- const { stream, threadId} = await pipe . run ( runOptions ) ;
13
-
14
- // 3. Return the ReadableStream directly with the threadId in the headers
15
- // to be used on the client side to mainain a single chat thread.
16
- return new Response ( stream , {
17
- status : 200 ,
18
- headers : {
19
- 'lb-thread-id' : threadId ?? '' ,
20
- } ,
21
- } ) ;
12
+ try {
13
+ const { stream, threadId} = await pipe . run ( runOptions ) ;
14
+ // 3. Return the ReadableStream directly with the threadId in the headers
15
+ // to be used on the client side to mainain a single chat thread.
16
+ return new Response ( stream , {
17
+ status : 200 ,
18
+ headers : {
19
+ 'lb-thread-id' : threadId ?? '' ,
20
+ } ,
21
+ } ) ;
22
+ } catch ( error : any ) {
23
+ return new Response (
24
+ JSON . stringify ( {
25
+ error,
26
+ } ) ,
27
+ {
28
+ status : error . status || 500 ,
29
+ headers : {
30
+ 'Content-Type' : 'application/json' ,
31
+ } ,
32
+ } ,
33
+ ) ;
34
+ }
22
35
}
Original file line number Diff line number Diff line change @@ -9,8 +9,24 @@ export async function POST(req: NextRequest) {
9
9
const pipe = new Pipe ( pipeSummary ( ) ) ;
10
10
11
11
// 2. Run the pipe
12
- const result = await pipe . run ( runOptions ) ;
12
+ try {
13
+ const result = await pipe . run ( runOptions ) ;
13
14
14
- // 3. Return the response stringified.
15
- return new Response ( JSON . stringify ( result ) ) ;
15
+ // 3. Return the response stringified.
16
+ return new Response ( JSON . stringify ( result ) ) ;
17
+ } catch ( error : any ) {
18
+ // 4. Return the error response
19
+
20
+ return new Response (
21
+ JSON . stringify ( {
22
+ error,
23
+ } ) ,
24
+ {
25
+ status : error . status || 500 ,
26
+ headers : {
27
+ 'Content-Type' : 'application/json' ,
28
+ } ,
29
+ } ,
30
+ ) ;
31
+ }
16
32
}
Original file line number Diff line number Diff line change @@ -25,15 +25,19 @@ export default function PipeRunExample() {
25
25
} ) ;
26
26
27
27
if ( ! response . ok ) {
28
- throw new Error ( 'Network response was not ok' ) ;
28
+ const res = await response . json ( ) ;
29
+ throw new Error ( res . error . error . message ) ;
29
30
}
30
31
31
32
// Parse the JSON response.
32
33
const data = await response . json ( ) ;
33
34
setCompletion ( data . completion ) ;
34
- } catch ( error ) {
35
- console . error ( 'Error:' , error ) ;
36
- setCompletion ( 'An error occurred while generating the completion.' ) ;
35
+ } catch ( error : any ) {
36
+ if ( error . message ) setCompletion ( error . message ) ;
37
+ else
38
+ setCompletion (
39
+ 'An error occurred while generating the completion.' ,
40
+ ) ;
37
41
} finally {
38
42
setLoading ( false ) ;
39
43
}
Original file line number Diff line number Diff line change @@ -186,7 +186,7 @@ export function usePipe({
186
186
signal,
187
187
} ) ;
188
188
189
- if ( ! response . ok ) throw new Error ( 'Failed to send message' ) ;
189
+ if ( ! response . ok ) await processErrorResponse ( response ) ;
190
190
191
191
const newThreadId = response . headers . get ( 'lb-thread-id' ) ;
192
192
if ( newThreadId ) threadIdRef . current = newThreadId ;
@@ -197,10 +197,12 @@ export function usePipe({
197
197
const result : RunResponse = await response . json ( ) ;
198
198
processNonStreamResponse ( result ) ;
199
199
}
200
- } catch ( err ) {
200
+ } catch ( err : any ) {
201
201
if ( err instanceof Error && err . name !== 'AbortError' ) {
202
202
setError ( err ) ;
203
203
onError ?.( err ) ;
204
+ } else if ( err . name !== 'AbortError' ) {
205
+ throw new Error ( 'Failed to send message' ) ;
204
206
}
205
207
} finally {
206
208
setIsLoading ( false ) ;
@@ -264,6 +266,16 @@ export function usePipe({
264
266
setIsLoading ( false ) ;
265
267
} , [ ] ) ;
266
268
269
+ const processErrorResponse = async ( response : Response ) => {
270
+ const res = await response . json ( ) ;
271
+ if ( res . error . error ) {
272
+ // Throw error object if it exists
273
+ throw new Error ( res . error . error . message ) ;
274
+ } else {
275
+ throw new Error ( 'Failed to send message' ) ;
276
+ }
277
+ } ;
278
+
267
279
return useMemo (
268
280
( ) => ( {
269
281
messages,
You canβt perform that action at this time.
0 commit comments