refactor(error): refactor SigitMetaParseError
This commit is contained in:
parent
2ea9afb3a3
commit
7a685c6cfe
@ -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)
|
||||
|
17
src/types/errors/MetaParseError.ts
Normal file
17
src/types/errors/MetaParseError.ts
Normal file
@ -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
|
||||
}
|
||||
}
|
29
src/types/errors/index.ts
Normal file
29
src/types/errors/index.ts
Normal file
@ -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}`)
|
||||
}
|
@ -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<Event> => {
|
||||
const event = await parseJson<Event>(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<CreateSignatureEventContent>(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 {
|
||||
|
Loading…
Reference in New Issue
Block a user