diff --git a/src/hooks/useSigitMeta.tsx b/src/hooks/useSigitMeta.tsx index fea5154..088940e 100644 --- a/src/hooks/useSigitMeta.tsx +++ b/src/hooks/useSigitMeta.tsx @@ -11,7 +11,6 @@ import { hexToNpub, parseNostrEvent, parseCreateSignatureEventContent, - SigitMetaParseError, SigitStatus, SignStatus } from '../utils' @@ -21,6 +20,7 @@ import { Event } from 'nostr-tools' import store from '../store/store' import { AuthState } from '../store/auth/types' import { NostrController } from '../controllers' +import { MetaParseError } from '../types/errors/MetaParseError' /** * Flattened interface that combines properties `Meta`, `CreateSignatureEventContent`, @@ -247,7 +247,7 @@ export const useSigitMeta = (meta: Meta): FlatMeta => { ) } } catch (error) { - if (error instanceof SigitMetaParseError) { + if (error instanceof MetaParseError) { toast.error(error.message) } console.error(error) diff --git a/src/types/errors/MetaParseError.ts b/src/types/errors/MetaParseError.ts new file mode 100644 index 0000000..85cddec --- /dev/null +++ b/src/types/errors/MetaParseError.ts @@ -0,0 +1,17 @@ +import { Jsonable } from '.' + +export class MetaParseError extends Error { + public readonly context?: Jsonable + + constructor( + message: string, + options: { cause?: Error; context?: Jsonable } = {} + ) { + const { cause, context } = options + + super(message, { cause }) + this.name = this.constructor.name + + this.context = context + } +} diff --git a/src/types/errors/index.ts b/src/types/errors/index.ts new file mode 100644 index 0000000..6ef8990 --- /dev/null +++ b/src/types/errors/index.ts @@ -0,0 +1,29 @@ +export type Jsonable = + | string + | number + | boolean + | null + | undefined + | readonly Jsonable[] + | { readonly [key: string]: Jsonable } + | { toJSON(): Jsonable } + +/** + * Handle errors + * Wraps the errors without message property and stringify to a message so we can use it later + * @param error + * @returns + */ +export function handleError(error: unknown): Error { + if (error instanceof Error) return error + + // No message error, wrap it and stringify + let stringified = 'Unable to stringify the thrown value' + try { + stringified = JSON.stringify(error) + } catch (error) { + console.error(stringified, error) + } + + return new Error(`Wrapped Error: ${stringified}`) +} diff --git a/src/utils/meta.ts b/src/utils/meta.ts index fd3481c..62eb740 100644 --- a/src/utils/meta.ts +++ b/src/utils/meta.ts @@ -3,6 +3,8 @@ import { fromUnixTimestamp, parseJson } from '.' import { Event, verifyEvent } from 'nostr-tools' import { toast } from 'react-toastify' import { extractFileExtensions } from './file' +import { handleError } from '../types/errors' +import { MetaParseError } from '../types/errors/MetaParseError' export enum SignStatus { Signed = 'Signed', @@ -17,52 +19,6 @@ export enum SigitStatus { Complete = 'Completed' } -type Jsonable = - | string - | number - | boolean - | null - | undefined - | readonly Jsonable[] - | { readonly [key: string]: Jsonable } - | { toJSON(): Jsonable } - -export class SigitMetaParseError extends Error { - public readonly context?: Jsonable - - constructor( - message: string, - options: { cause?: Error; context?: Jsonable } = {} - ) { - const { cause, context } = options - - super(message, { cause }) - this.name = this.constructor.name - - this.context = context - } -} - -/** - * Handle meta errors - * Wraps the errors without message property and stringify to a message so we can use it later - * @param error - * @returns - */ -function handleError(error: unknown): Error { - if (error instanceof Error) return error - - // No message error, wrap it and stringify - let stringified = 'Unable to stringify the thrown value' - try { - stringified = JSON.stringify(error) - } catch (error) { - console.error(stringified, error) - } - - return new Error(`[SiGit Error]: ${stringified}`) -} - // Reuse common error messages for meta parsing export enum SigitMetaParseErrorType { 'PARSE_ERROR_EVENT' = 'error occurred in parsing the create signature event', @@ -89,7 +45,7 @@ export const parseNostrEvent = async (raw: string): Promise => { const event = await parseJson(raw) return event } catch (error) { - throw new SigitMetaParseError(SigitMetaParseErrorType.PARSE_ERROR_EVENT, { + throw new MetaParseError(SigitMetaParseErrorType.PARSE_ERROR_EVENT, { cause: handleError(error), context: raw }) @@ -109,7 +65,7 @@ export const parseCreateSignatureEventContent = async ( await parseJson(raw) return createSignatureEventContent } catch (error) { - throw new SigitMetaParseError( + throw new MetaParseError( SigitMetaParseErrorType.PARSE_ERROR_SIGNATURE_EVENT_CONTENT, { cause: handleError(error), @@ -165,7 +121,7 @@ export const extractSigitCardDisplayInfo = async (meta: Meta) => { return sigitInfo } catch (error) { - if (error instanceof SigitMetaParseError) { + if (error instanceof MetaParseError) { toast.error(error.message) console.error(error.name, error.message, error.cause, error.context) } else {