11/* eslint-disable @typescript-eslint/ban-ts-comment */
22import { Cell } from '@fileverse-dev/fortune-core' ;
33import { WorkbookInstance } from '@fileverse-dev/fortune-react' ;
4- import { OnboardingHandlerType , DataBlockApiKeyHandlerType } from '../types' ;
4+ import {
5+ OnboardingHandlerType ,
6+ DataBlockApiKeyHandlerType ,
7+ ErrorMessageHandlerReturnType ,
8+ } from '../types' ;
59import { formulaResponseUiSync } from './formula-ui-sync' ;
610import {
711 executeStringFunction ,
@@ -236,7 +240,7 @@ const processFlvurlPromise = async (
236240} ;
237241
238242const handleDatablockErroMessage = (
239- data : string ,
243+ data : ErrorMessageHandlerReturnType ,
240244 dataBlockApiKeyHandler : DataBlockApiKeyHandlerType ,
241245 params : Pick <
242246 AfterUpdateCellParams ,
@@ -254,11 +258,18 @@ const handleDatablockErroMessage = (
254258 } ) ;
255259} ;
256260
261+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
262+ const isDatablockError = ( value : any ) => {
263+ const isObject =
264+ value !== null && typeof value === 'object' && ! Array . isArray ( value ) ;
265+ return isObject && containsErrorFlag ( value . type ) ;
266+ } ;
267+
257268/**
258269 * Processes promise resolution for regular formulas
259270 */
260271const processRegularPromise = async (
261- promise : Promise < Record < string , string > [ ] | string > ,
272+ promise : Promise < unknown [ ] | ErrorMessageHandlerReturnType | string > ,
262273 params : Pick <
263274 AfterUpdateCellParams ,
264275 | 'row'
@@ -272,18 +283,21 @@ const processRegularPromise = async (
272283) : Promise < void > => {
273284 try {
274285 const data = await promise ;
275- if (
276- typeof data === 'string' &&
277- containsErrorFlag ( data ) &&
278- params . dataBlockApiKeyHandler
279- ) {
280- handleStringResponse ( data as string , params ) ;
281- handleDatablockErroMessage ( data , params . dataBlockApiKeyHandler , params ) ;
286+ const formulaName = params . newValue ?. f
287+ ?. match ( / ^ = ( [ A - Z a - z 0 - 9 _ ] + ) \s * \( / ) ?. [ 1 ]
288+ ?. toUpperCase ( ) ;
289+ if ( isDatablockError ( data ) ) {
290+ if ( ! params . dataBlockApiKeyHandler ) {
291+ throw new Error ( 'dataBlockApiKeyHandler missing' ) ;
292+ }
293+ const _data = data as ErrorMessageHandlerReturnType ;
294+ handleDatablockErroMessage ( _data , params . dataBlockApiKeyHandler , params ) ;
282295 return ;
283296 }
284297
285298 if ( Array . isArray ( data ) ) {
286- if ( ! data . length ) {
299+ const firstItem = ( data as any [ ] ) ?. [ 0 ] ;
300+ if ( ! data . length || Object . keys ( firstItem ) . length === 0 ) {
287301 params . sheetEditorRef . current ?. setCellValue ( params . row , params . column , {
288302 ...params . newValue ,
289303 m : 'No Data' ,
@@ -295,16 +309,33 @@ const processRegularPromise = async (
295309 } else {
296310 handleStringResponse ( data as string , params ) ;
297311 }
312+ params . onDataBlockApiResponse ?.( formulaName as string ) ;
298313 const workbookContext = params . sheetEditorRef . current ?. getWorkbookContext ( ) ;
299- const formulaName = params . newValue ?. f
300- ?. match ( / ^ = ( [ A - Z a - z 0 - 9 _ ] + ) \s * \( / ) ?. [ 1 ]
301- ?. toUpperCase ( ) ;
302314 const apiKeyName =
303315 workbookContext ?. formulaCache . functionlistMap [ formulaName || '' ] ?. API_KEY ;
304316 params . storeApiKey ?.( apiKeyName ) ;
305- } catch ( error ) {
306- console . error ( 'Error processing regular promise:' , error ) ;
307- handleStringResponse ( 'Error processing data' , params ) ;
317+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
318+ } catch ( error : any ) {
319+ if (
320+ error === 'dataBlockApiKeyHandler missing' ||
321+ error ?. message === 'dataBlockApiKeyHandler missing'
322+ ) {
323+ throw new Error ( 'dataBlockApiKeyHandler missing' ) ;
324+ } else {
325+ const formulaName = params . newValue ?. f
326+ ?. match ( / ^ = ( [ A - Z a - z 0 - 9 _ ] + ) \s * \( / ) ?. [ 1 ]
327+ ?. toUpperCase ( ) ;
328+ handleDatablockErroMessage (
329+ {
330+ type : 'DSHEET_ERROR' ,
331+ message : 'Unexpected Error' ,
332+ functionName : formulaName ,
333+ reason : error ?. message ,
334+ } ,
335+ params . dataBlockApiKeyHandler ! ,
336+ params ,
337+ ) ;
338+ }
308339 }
309340} ;
310341
@@ -433,8 +464,6 @@ export const afterUpdateCell = async (
433464
434465 // register dataBlockCalcFunction cell
435466 updateDataCalcFunc ( { params : updatedParams , currentSheetId } ) ;
436-
437- params . onDataBlockApiResponse ?.( formulaName as string ) ;
438467 }
439468
440469 const dataBlockCalcFunction = params ?. dataBlockCalcFunction ;
@@ -521,10 +550,16 @@ const updateDataCalcFunc = ({
521550 } ;
522551 } ) ;
523552 } catch ( error : any ) {
524- console . log ( { error } ) ;
553+ const formulaName = params . newValue . f
554+ ?. match ( / ^ = ( [ A - Z a - z 0 - 9 _ ] + ) \s * \( / ) ?. [ 1 ]
555+ ?. toUpperCase ( ) ;
525556 // send error message to dsheet.new to commit to sentry
526557 params ?. dataBlockApiKeyHandler ?.( {
527- data : `ERROR from updateDataCalcFunc ${ error ?. message } ` ,
558+ data : {
559+ message : `ERROR from updateDataCalcFunc ${ error ?. message } ` ,
560+ type : 'Unexpected error' ,
561+ functionName : formulaName ,
562+ } ,
528563 sheetEditorRef : params . sheetEditorRef ,
529564 executeStringFunction,
530565 row : params . row ,
0 commit comments