-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {CHARTKIT_ERROR_CODE, ChartKitError, isChartKitError} from '../chartkit-error'; | ||
import type {ChartKitErrorArgs} from '../chartkit-error'; | ||
|
||
describe('libs/chartkit-error', () => { | ||
test.each<[unknown, boolean] /* [error, expected] */>([ | ||
[new ChartKitError(), true], | ||
[new Error(), false], | ||
[null, false], | ||
[undefined, false], | ||
])('isChartKitError (args: %j)', (error, expected) => { | ||
const result = isChartKitError(error); | ||
expect(result).toEqual(expected); | ||
}); | ||
|
||
test.each<[ChartKitErrorArgs | undefined, ChartKitErrorArgs['code']] /* [args, expected] */>([ | ||
[undefined, CHARTKIT_ERROR_CODE.UNKNOWN], | ||
[{code: CHARTKIT_ERROR_CODE.NO_DATA}, CHARTKIT_ERROR_CODE.NO_DATA], | ||
])('check ChartKitError code (args: %j)', (args, expected) => { | ||
let result: ChartKitErrorArgs['code'] = ''; | ||
|
||
try { | ||
throw new ChartKitError(args); | ||
} catch (error) { | ||
if (isChartKitError(error)) { | ||
result = error.code; | ||
} | ||
} | ||
|
||
expect(result).toEqual(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export type ChartKitErrorArgs = { | ||
code?: number | string; | ||
originalError?: Error; | ||
message?: string; | ||
}; | ||
|
||
export const CHARTKIT_ERROR_CODE = { | ||
UNKNOWN: 'ERR.CK.UNKNOWN_ERROR', | ||
UNKNOWN_PLUGIN: 'ERR.CK.UNKNOWN_PLUGIN', | ||
NO_DATA: 'ERR.CK.NO_DATA', | ||
}; | ||
|
||
export class ChartKitError extends Error { | ||
readonly code: number | string; | ||
readonly isChartKitError = true; | ||
|
||
constructor({ | ||
originalError, | ||
message, | ||
code = CHARTKIT_ERROR_CODE.UNKNOWN, | ||
}: ChartKitErrorArgs = {}) { | ||
super(message); | ||
|
||
this.code = code; | ||
|
||
if (originalError) { | ||
this.name = originalError.name; | ||
this.stack = originalError.stack; | ||
} | ||
} | ||
} | ||
|
||
export const isChartKitError = (error: unknown): error is ChartKitError => { | ||
return error instanceof Error && 'isChartKitError' in error; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export {CHARTKIT_ERROR_CODE, ChartKitError, isChartKitError} from './chartkit-error/chartkit-error'; | ||
export type {ChartKitErrorArgs} from './chartkit-error/chartkit-error'; | ||
export {settings} from './settings/settings'; |