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) {
99 const pipe = new Pipe ( pipeSummary ( ) ) ;
1010
1111 // 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+ }
2235}
Original file line number Diff line number Diff line change @@ -9,8 +9,24 @@ export async function POST(req: NextRequest) {
99 const pipe = new Pipe ( pipeSummary ( ) ) ;
1010
1111 // 2. Run the pipe
12- const result = await pipe . run ( runOptions ) ;
12+ try {
13+ const result = await pipe . run ( runOptions ) ;
1314
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+ }
1632}
Original file line number Diff line number Diff line change @@ -25,15 +25,19 @@ export default function PipeRunExample() {
2525 } ) ;
2626
2727 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 ) ;
2930 }
3031
3132 // Parse the JSON response.
3233 const data = await response . json ( ) ;
3334 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+ ) ;
3741 } finally {
3842 setLoading ( false ) ;
3943 }
Original file line number Diff line number Diff line change @@ -186,7 +186,7 @@ export function usePipe({
186186 signal,
187187 } ) ;
188188
189- if ( ! response . ok ) throw new Error ( 'Failed to send message' ) ;
189+ if ( ! response . ok ) await processErrorResponse ( response ) ;
190190
191191 const newThreadId = response . headers . get ( 'lb-thread-id' ) ;
192192 if ( newThreadId ) threadIdRef . current = newThreadId ;
@@ -197,10 +197,12 @@ export function usePipe({
197197 const result : RunResponse = await response . json ( ) ;
198198 processNonStreamResponse ( result ) ;
199199 }
200- } catch ( err ) {
200+ } catch ( err : any ) {
201201 if ( err instanceof Error && err . name !== 'AbortError' ) {
202202 setError ( err ) ;
203203 onError ?.( err ) ;
204+ } else if ( err . name !== 'AbortError' ) {
205+ throw new Error ( 'Failed to send message' ) ;
204206 }
205207 } finally {
206208 setIsLoading ( false ) ;
@@ -264,6 +266,16 @@ export function usePipe({
264266 setIsLoading ( false ) ;
265267 } , [ ] ) ;
266268
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+
267279 return useMemo (
268280 ( ) => ( {
269281 messages,
You canβt perform that action at this time.
0 commit comments