fix: load screen on sign, block on missing counterpart #170
@ -11,7 +11,6 @@ import {
|
|||||||
hexToNpub,
|
hexToNpub,
|
||||||
parseNostrEvent,
|
parseNostrEvent,
|
||||||
parseCreateSignatureEventContent,
|
parseCreateSignatureEventContent,
|
||||||
SigitMetaParseError,
|
|
||||||
SigitStatus,
|
SigitStatus,
|
||||||
SignStatus
|
SignStatus
|
||||||
} from '../utils'
|
} from '../utils'
|
||||||
@ -21,6 +20,7 @@ import { Event } from 'nostr-tools'
|
|||||||
import store from '../store/store'
|
import store from '../store/store'
|
||||||
import { AuthState } from '../store/auth/types'
|
import { AuthState } from '../store/auth/types'
|
||||||
import { NostrController } from '../controllers'
|
import { NostrController } from '../controllers'
|
||||||
|
import { MetaParseError } from '../types/errors/MetaParseError'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Flattened interface that combines properties `Meta`, `CreateSignatureEventContent`,
|
* Flattened interface that combines properties `Meta`, `CreateSignatureEventContent`,
|
||||||
@ -247,7 +247,7 @@ export const useSigitMeta = (meta: Meta): FlatMeta => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof SigitMetaParseError) {
|
if (error instanceof MetaParseError) {
|
||||||
toast.error(error.message)
|
toast.error(error.message)
|
||||||
}
|
}
|
||||||
console.error(error)
|
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 { Event, verifyEvent } from 'nostr-tools'
|
||||||
import { toast } from 'react-toastify'
|
import { toast } from 'react-toastify'
|
||||||
import { extractFileExtensions } from './file'
|
import { extractFileExtensions } from './file'
|
||||||
|
import { handleError } from '../types/errors'
|
||||||
|
import { MetaParseError } from '../types/errors/MetaParseError'
|
||||||
|
|
||||||
export enum SignStatus {
|
export enum SignStatus {
|
||||||
Signed = 'Signed',
|
Signed = 'Signed',
|
||||||
@ -17,52 +19,6 @@ export enum SigitStatus {
|
|||||||
Complete = 'Completed'
|
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
|
// Reuse common error messages for meta parsing
|
||||||
export enum SigitMetaParseErrorType {
|
export enum SigitMetaParseErrorType {
|
||||||
'PARSE_ERROR_EVENT' = 'error occurred in parsing the create signature event',
|
'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)
|
const event = await parseJson<Event>(raw)
|
||||||
return event
|
return event
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new SigitMetaParseError(SigitMetaParseErrorType.PARSE_ERROR_EVENT, {
|
throw new MetaParseError(SigitMetaParseErrorType.PARSE_ERROR_EVENT, {
|
||||||
cause: handleError(error),
|
cause: handleError(error),
|
||||||
context: raw
|
context: raw
|
||||||
})
|
})
|
||||||
@ -109,7 +65,7 @@ export const parseCreateSignatureEventContent = async (
|
|||||||
await parseJson<CreateSignatureEventContent>(raw)
|
await parseJson<CreateSignatureEventContent>(raw)
|
||||||
return createSignatureEventContent
|
return createSignatureEventContent
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new SigitMetaParseError(
|
throw new MetaParseError(
|
||||||
SigitMetaParseErrorType.PARSE_ERROR_SIGNATURE_EVENT_CONTENT,
|
SigitMetaParseErrorType.PARSE_ERROR_SIGNATURE_EVENT_CONTENT,
|
||||||
{
|
{
|
||||||
cause: handleError(error),
|
cause: handleError(error),
|
||||||
@ -165,7 +121,7 @@ export const extractSigitCardDisplayInfo = async (meta: Meta) => {
|
|||||||
|
|
||||||
return sigitInfo
|
return sigitInfo
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof SigitMetaParseError) {
|
if (error instanceof MetaParseError) {
|
||||||
toast.error(error.message)
|
toast.error(error.message)
|
||||||
console.error(error.name, error.message, error.cause, error.context)
|
console.error(error.name, error.message, error.cause, error.context)
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user