@@ -8,12 +8,15 @@ import type { ServerSentEventMessage } from 'fetch-event-stream';
88import type { FetchResponse } from 'openapi-fetch' ;
99import type { MediaType } from 'openapi-typescript-helpers' ;
1010
11+ import type { QueryMetadataError } from '#contexts/QueryProvider/types.ts' ;
12+ import type { Toast } from '#contexts/Toast/toast-context.ts' ;
1113import type { Agent } from '#modules/agents/api/types.ts' ;
1214import { NEXTAUTH_URL , TRUST_PROXY_HEADERS } from '#utils/constants.ts' ;
1315import { isNotNull } from '#utils/helpers.ts' ;
16+ import { createSection , joinSections } from '#utils/markdown.ts' ;
1417
15- import { ApiError , ApiValidationError , HttpError , UnauthenticatedError } from './errors' ;
16- import type { ApiErrorCode , ApiErrorResponse , ApiValidationErrorResponse } from './types' ;
18+ import { A2AExtensionError , ApiError , ApiValidationError , HttpError , UnauthenticatedError } from './errors' ;
19+ import type { A2AErrorMetadata , ApiErrorCode , ApiErrorResponse , ApiValidationErrorResponse } from './types' ;
1720
1821export function ensureData < T extends Record < string | number , unknown > , O , M extends MediaType > (
1922 fetchResponse : FetchResponse < T , O , M > ,
@@ -111,3 +114,45 @@ export async function getProxyHeaders(headers: Headers, url?: URL) {
111114
112115 return { forwardedHost, forwardedProto, forwarded } ;
113116}
117+
118+ export function buildErrorToast ( { metadata = { } , error } : { metadata ?: QueryMetadataError ; error : unknown } ) : Toast {
119+ const { includeErrorMessage } = metadata ;
120+
121+ const defaults : Partial < Toast > = {
122+ kind : 'error' ,
123+ renderMarkdown : true ,
124+ } ;
125+
126+ if ( error instanceof A2AExtensionError ) {
127+ const message = includeErrorMessage ? createA2AErrorMessage ( error ) : undefined ;
128+
129+ return {
130+ ...defaults ,
131+ title : error . title ,
132+ message,
133+ } ;
134+ }
135+
136+ const { title = 'An error occurred' } = metadata ;
137+ const message = joinSections ( [ metadata . message , includeErrorMessage ? getErrorMessage ( error ) : undefined ] ) ;
138+
139+ return {
140+ ...defaults ,
141+ title,
142+ message,
143+ } ;
144+ }
145+
146+ function createA2AErrorMessage ( error : A2AErrorMetadata ) {
147+ const { context, stacktrace } = error ;
148+
149+ const errorMessage = getErrorMessage ( error ) ;
150+ const contextMessage = context
151+ ? createSection ( { heading : 'Context' , content : JSON . stringify ( context , null , 2 ) } )
152+ : undefined ;
153+ const stacktraceMessage = stacktrace ? createSection ( { heading : 'Stacktrace' , content : stacktrace } ) : undefined ;
154+
155+ const message = joinSections ( [ errorMessage , contextMessage , stacktraceMessage ] ) ;
156+
157+ return message ;
158+ }
0 commit comments