From e20fb9df1d811efa08a8a68110b1a6819a384af4 Mon Sep 17 00:00:00 2001 From: "Mr.Dr.Professor Patrick" Date: Wed, 27 Jul 2022 14:59:17 +0300 Subject: [PATCH] fix: error handling fixes (#31) * error handling fixes * marginy review fixes --- src/components/ErrorBoundary/ErrorBoundary.tsx | 2 +- src/libs/chartkit-error/chartkit-error.ts | 4 ++-- src/plugins/indicator/renderer/IndicatorWidget.tsx | 5 +++-- src/types/index.ts | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/components/ErrorBoundary/ErrorBoundary.tsx b/src/components/ErrorBoundary/ErrorBoundary.tsx index 2b91bd54..bb2c5822 100644 --- a/src/components/ErrorBoundary/ErrorBoundary.tsx +++ b/src/components/ErrorBoundary/ErrorBoundary.tsx @@ -20,7 +20,7 @@ export class ErrorBoundary extends React.Component { error: undefined, }; - componentDidUpdate() { + componentDidCatch() { const {error} = this.state; if (error) { diff --git a/src/libs/chartkit-error/chartkit-error.ts b/src/libs/chartkit-error/chartkit-error.ts index 0970d40e..5081e883 100644 --- a/src/libs/chartkit-error/chartkit-error.ts +++ b/src/libs/chartkit-error/chartkit-error.ts @@ -12,7 +12,7 @@ export const CHARTKIT_ERROR_CODE = { export class ChartKitError extends Error { readonly code: number | string; - readonly isChartKitError = true; + readonly isCustomError = true; constructor({ originalError, @@ -31,5 +31,5 @@ export class ChartKitError extends Error { } export const isChartKitError = (error: unknown): error is ChartKitError => { - return error instanceof Error && 'isChartKitError' in error; + return error instanceof Error && 'isCustomError' in error; }; diff --git a/src/plugins/indicator/renderer/IndicatorWidget.tsx b/src/plugins/indicator/renderer/IndicatorWidget.tsx index 800b6f1a..ce16c0c9 100644 --- a/src/plugins/indicator/renderer/IndicatorWidget.tsx +++ b/src/plugins/indicator/renderer/IndicatorWidget.tsx @@ -1,5 +1,6 @@ import React from 'react'; import block from 'bem-cn-lite'; +import {isEmpty} from 'lodash'; import {i18n} from '../../../i18n'; import {CHARTKIT_ERROR_CODE, ChartKitError} from '../../../libs'; import {CHARTKIT_SCROLLABLE_NODE_CLASSNAME} from '../../../constants'; @@ -18,14 +19,14 @@ const IndicatorWidget = React.forwardRef { onLoad?.(); }, []); - if (!data) { + if (isEmpty(data)) { throw new ChartKitError({ code: CHARTKIT_ERROR_CODE.NO_DATA, message: i18n('error', 'label_no-data'), diff --git a/src/types/index.ts b/src/types/index.ts index 2e854e7d..701c5a6e 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -19,7 +19,7 @@ export type ChartKitOnLoadData = { export type ChartKitOnError = (data: {error: any}) => void; -export type ChartKitFormatNumber = (value: number, options?: unknown) => string; +export type ChartKitFormatNumber = (value: number, options?: T) => string; export type ChartKitProps = { type: T;