Skip to content

Commit f453a5e

Browse files
add support for proper error messages (#129)
* add support for proper error messages * exported ErrorMessageHandlerReturnType
1 parent fc72715 commit f453a5e

6 files changed

Lines changed: 126 additions & 63 deletions

File tree

index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export { default as DsheetEditor } from './package/dsheet-editor';
22
export { formulaResponseUiSync } from './package/utils/formula-ui-sync';
33
export { executeStringFunction } from './package/utils/executeStringFunction';
44
export { FLVURL } from '@fileverse-dev/formulajs';
5+
export type { ErrorMessageHandlerReturnType } from './package/types';

package-lock.json

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@fileverse-dev/dsheet",
33
"private": false,
44
"description": "DSheet",
5-
"version": "1.0.42",
5+
"version": "1.0.43",
66
"main": "dist/index.es.js",
77
"module": "dist/index.es.js",
88
"exports": {
@@ -34,10 +34,10 @@
3434
},
3535
"dependencies": {
3636
"@fileverse-dev/dsheets-templates": "^0.0.17",
37-
"@fileverse-dev/formula-parser": "^0.2.19",
38-
"@fileverse-dev/formulajs": "^4.4.11-mod-64",
39-
"@fileverse-dev/fortune-core": "^1.0.23",
40-
"@fileverse-dev/fortune-react": "^1.0.23",
37+
"@fileverse-dev/formula-parser": "^0.2.21",
38+
"@fileverse-dev/formulajs": "^4.4.11-mod-67",
39+
"@fileverse-dev/fortune-core": "^1.0.25",
40+
"@fileverse-dev/fortune-react": "^1.0.25",
4141
"@fileverse/ui": "^4.1.7-patch-18",
4242
"classnames": "^2.5.1",
4343
"exceljs": "^4.4.0",

package/components/editor-workbook.tsx

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const EditorWorkbook: React.FC<EditorWorkbookProps> = ({
6161
exportDropdownOpen = false,
6262
commentData,
6363
getCommentCellUI,
64-
setExportDropdownOpen = () => { },
64+
setExportDropdownOpen = () => {},
6565
dsheetId,
6666
storeApiKey,
6767
onDataBlockApiResponse,
@@ -146,30 +146,29 @@ export const EditorWorkbook: React.FC<EditorWorkbookProps> = ({
146146
customToolbarItems={
147147
!isReadOnly
148148
? getCustomToolbarItems({
149-
setExportDropdownOpen,
150-
handleCSVUpload,
151-
handleXLSXUpload,
152-
handleExportToXLSX,
153-
handleExportToCSV,
154-
handleExportToJSON,
155-
sheetEditorRef,
156-
ydocRef,
157-
dsheetId,
158-
currentDataRef,
159-
setForceSheetRender,
160-
toggleTemplateSidebar,
161-
setShowFetchURLModal,
162-
})
149+
setExportDropdownOpen,
150+
handleCSVUpload,
151+
handleXLSXUpload,
152+
handleExportToXLSX,
153+
handleExportToCSV,
154+
handleExportToJSON,
155+
sheetEditorRef,
156+
ydocRef,
157+
dsheetId,
158+
currentDataRef,
159+
setForceSheetRender,
160+
toggleTemplateSidebar,
161+
setShowFetchURLModal,
162+
})
163163
: []
164164
}
165165
hooks={{
166166
afterUpdateCell: (
167167
row: number,
168168
column: number,
169-
oldValue: Cell,
169+
_oldValue: Cell,
170170
newValue: Cell,
171171
): void => {
172-
console.log('afterUpdateCell Old value', oldValue);
173172
const refObj = { current: sheetEditorRef.current };
174173
afterUpdateCell({
175174
row,

package/types.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { RefObject } from 'react';
33
import { WorkbookInstance } from '@fileverse-dev/fortune-react';
44
import * as Y from 'yjs';
55
import { Cell } from '@fileverse-dev/fortune-core';
6+
// @ts-ignore
7+
import { ERROR_MESSAGES_FLAG } from '@fileverse-dev/formulajs/crypto-constants';
68

79
export interface SheetUpdateData {
810
data: Sheet[];
@@ -23,7 +25,7 @@ export type OnboardingHandlerType = (params: {
2325

2426
// Define the data block API key handler type
2527
export type DataBlockApiKeyHandlerType = (params: {
26-
data: string;
28+
data: ErrorMessageHandlerReturnType;
2729
sheetEditorRef: React.RefObject<WorkbookInstance | null>;
2830
executeStringFunction: (functionCallString: string) => Promise<unknown>;
2931
row: number;
@@ -69,3 +71,29 @@ export interface DsheetProps {
6971
onDuneChartEmbed?: () => void;
7072
onSheetCountChange?: (sheetCount: number) => void;
7173
}
74+
type BaseError = {
75+
message: string;
76+
functionName?: string;
77+
type: string;
78+
};
79+
80+
type CustomError = BaseError & {
81+
type: typeof ERROR_MESSAGES_FLAG.CUSTOM;
82+
reason: string;
83+
};
84+
85+
export type NetworkError = BaseError & {
86+
type:
87+
| typeof ERROR_MESSAGES_FLAG.NETWORK_ERROR
88+
| typeof ERROR_MESSAGES_FLAG.RATE_LIMIT;
89+
};
90+
91+
export type DefaultError = BaseError & {
92+
reason: string;
93+
};
94+
95+
export type ErrorMessageHandlerReturnType =
96+
| BaseError
97+
| CustomError
98+
| NetworkError
99+
| DefaultError;

package/utils/after-update-cell.tsx

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
/* eslint-disable @typescript-eslint/ban-ts-comment */
22
import { Cell } from '@fileverse-dev/fortune-core';
33
import { WorkbookInstance } from '@fileverse-dev/fortune-react';
4-
import { OnboardingHandlerType, DataBlockApiKeyHandlerType } from '../types';
4+
import {
5+
OnboardingHandlerType,
6+
DataBlockApiKeyHandlerType,
7+
ErrorMessageHandlerReturnType,
8+
} from '../types';
59
import { formulaResponseUiSync } from './formula-ui-sync';
610
import {
711
executeStringFunction,
@@ -236,7 +240,7 @@ const processFlvurlPromise = async (
236240
};
237241

238242
const 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
*/
260271
const 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-Za-z0-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-Za-z0-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-Za-z0-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-Za-z0-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

Comments
 (0)