@@ -62,6 +62,45 @@ const shouldSkipEntry = (output: any): boolean => {
6262 return false
6363}
6464
65+ interface NotifyBlockErrorParams {
66+ error : unknown
67+ blockName : string
68+ workflowId ?: string
69+ logContext : Record < string , unknown >
70+ }
71+
72+ /**
73+ * Sends an error notification for a block failure if error notifications are enabled.
74+ */
75+ const notifyBlockError = ( { error, blockName, workflowId, logContext } : NotifyBlockErrorParams ) => {
76+ const settings = getQueryClient ( ) . getQueryData < GeneralSettings > ( generalSettingsKeys . settings ( ) )
77+ const isErrorNotificationsEnabled = settings ?. errorNotificationsEnabled ?? true
78+
79+ if ( ! isErrorNotificationsEnabled ) return
80+
81+ try {
82+ const errorMessage = String ( error )
83+ const displayName = blockName || 'Unknown Block'
84+ const displayMessage = `${ displayName } : ${ errorMessage } `
85+ const copilotMessage = `${ errorMessage } \n\nError in ${ displayName } .\n\nPlease fix this.`
86+
87+ useNotificationStore . getState ( ) . addNotification ( {
88+ level : 'error' ,
89+ message : displayMessage ,
90+ workflowId,
91+ action : {
92+ type : 'copilot' ,
93+ message : copilotMessage ,
94+ } ,
95+ } )
96+ } catch ( notificationError ) {
97+ logger . error ( 'Failed to create block error notification' , {
98+ ...logContext ,
99+ error : notificationError ,
100+ } )
101+ }
102+ }
103+
65104export const useTerminalConsoleStore = create < ConsoleStore > ( ) (
66105 devtools (
67106 persist (
@@ -154,35 +193,12 @@ export const useTerminalConsoleStore = create<ConsoleStore>()(
154193 const newEntry = get ( ) . entries [ 0 ]
155194
156195 if ( newEntry ?. error ) {
157- const settings = getQueryClient ( ) . getQueryData < GeneralSettings > (
158- generalSettingsKeys . settings ( )
159- )
160- const isErrorNotificationsEnabled = settings ?. errorNotificationsEnabled ?? true
161-
162- if ( isErrorNotificationsEnabled ) {
163- try {
164- const errorMessage = String ( newEntry . error )
165- const blockName = newEntry . blockName || 'Unknown Block'
166- const displayMessage = `${ blockName } : ${ errorMessage } `
167-
168- const copilotMessage = `${ errorMessage } \n\nError in ${ blockName } .\n\nPlease fix this.`
169-
170- useNotificationStore . getState ( ) . addNotification ( {
171- level : 'error' ,
172- message : displayMessage ,
173- workflowId : entry . workflowId ,
174- action : {
175- type : 'copilot' ,
176- message : copilotMessage ,
177- } ,
178- } )
179- } catch ( notificationError ) {
180- logger . error ( 'Failed to create block error notification' , {
181- entryId : newEntry . id ,
182- error : notificationError ,
183- } )
184- }
185- }
196+ notifyBlockError ( {
197+ error : newEntry . error ,
198+ blockName : newEntry . blockName || 'Unknown Block' ,
199+ workflowId : entry . workflowId ,
200+ logContext : { entryId : newEntry . id } ,
201+ } )
186202 }
187203
188204 return newEntry
@@ -378,37 +394,15 @@ export const useTerminalConsoleStore = create<ConsoleStore>()(
378394 } )
379395
380396 if ( typeof update === 'object' && update . error ) {
381- const settings = getQueryClient ( ) . getQueryData < GeneralSettings > (
382- generalSettingsKeys . settings ( )
397+ const matchingEntry = get ( ) . entries . find (
398+ ( e ) => e . blockId === blockId && e . executionId === executionId
383399 )
384- const isErrorNotificationsEnabled = settings ?. errorNotificationsEnabled ?? true
385-
386- if ( isErrorNotificationsEnabled ) {
387- try {
388- const matchingEntry = get ( ) . entries . find (
389- ( e ) => e . blockId === blockId && e . executionId === executionId
390- )
391- const errorMessage = String ( update . error )
392- const blockName = matchingEntry ?. blockName || 'Unknown Block'
393- const displayMessage = `${ blockName } : ${ errorMessage } `
394- const copilotMessage = `${ errorMessage } \n\nError in ${ blockName } .\n\nPlease fix this.`
395-
396- useNotificationStore . getState ( ) . addNotification ( {
397- level : 'error' ,
398- message : displayMessage ,
399- workflowId : matchingEntry ?. workflowId ,
400- action : {
401- type : 'copilot' ,
402- message : copilotMessage ,
403- } ,
404- } )
405- } catch ( notificationError ) {
406- logger . error ( 'Failed to create block error notification' , {
407- blockId,
408- error : notificationError ,
409- } )
410- }
411- }
400+ notifyBlockError ( {
401+ error : update . error ,
402+ blockName : matchingEntry ?. blockName || 'Unknown Block' ,
403+ workflowId : matchingEntry ?. workflowId ,
404+ logContext : { blockId } ,
405+ } )
412406 }
413407 } ,
414408
0 commit comments