chore: improved error handling #4
@ -1,5 +1,5 @@
|
||||
import { NDKEvent } from '@nostr-dev-kit/ndk'
|
||||
import { log, ndk } from '../../main.js'
|
||||
import { NDKEvent, log } from '@nostr-dev-kit/ndk'
|
||||
import { ndk } from '../../main.js'
|
||||
|
||||
export async function inProgress(event: NDKEvent): Promise<NDKEvent> {
|
||||
log('marking job as in progress')
|
||||
@ -11,7 +11,13 @@ export async function inProgress(event: NDKEvent): Promise<NDKEvent> {
|
||||
})
|
||||
|
||||
reactEvent.tag(event, 'job')
|
||||
await reactEvent.sign()
|
||||
await reactEvent.publish()
|
||||
|
||||
await reactEvent.sign().catch((err) => {
|
||||
throw `Event signing error: ${err}`
|
||||
})
|
||||
await reactEvent.publish().catch((err) => {
|
||||
throw `Event publishing error: ${err}`
|
||||
})
|
||||
|
||||
return reactEvent
|
||||
}
|
||||
|
37
src/main.ts
37
src/main.ts
@ -28,7 +28,9 @@ export const ndk = new NDK({
|
||||
],
|
||||
signer: getSigner()
|
||||
})
|
||||
await ndk.connect(2000)
|
||||
|
||||
await ndk.connect(2000).catch((err) => log('NDK connection error: ', err))
|
||||
|
||||
log('connected')
|
||||
|
||||
const subs = ndk.subscribe(
|
||||
@ -196,25 +198,44 @@ async function processJobEvent(event: NDKEvent, type: JobType): Promise<void> {
|
||||
return promise
|
||||
}
|
||||
|
||||
const logJobError = (job: JobType, err: any) =>
|
||||
log(`Error thrown by ${job}: `, err)
|
||||
|
||||
const startProcessing = async () => {
|
||||
log('startProcessing')
|
||||
await inProgress(event)
|
||||
|
||||
await inProgress(event).catch((err) => log('Processing event error: ', err))
|
||||
|
||||
switch (type) {
|
||||
case JobTypes.Summarize: {
|
||||
output = await onNewSummarizationJob(event)
|
||||
output = await onNewSummarizationJob(event).catch((err) =>
|
||||
logJobError(JobTypes.Summarize, err)
|
||||
)
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
case JobTypes.SpeechToText: {
|
||||
output = await speechToTextJob(event)
|
||||
output = await speechToTextJob(event).catch((err) =>
|
||||
logJobError(JobTypes.SpeechToText, err)
|
||||
)
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
case JobTypes.BlockChainBlockNumber: {
|
||||
output = await blockChainBlockNumberJob(event)
|
||||
output = await blockChainBlockNumberJob(event).catch((err) =>
|
||||
logJobError(JobTypes.BlockChainBlockNumber, err)
|
||||
)
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
case JobTypes.RelayInfo: {
|
||||
output = await relayInfoJob(event)
|
||||
output = await relayInfoJob(event).catch((err) =>
|
||||
logJobError(JobTypes.RelayInfo, err)
|
||||
)
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
@ -225,7 +246,9 @@ async function processJobEvent(event: NDKEvent, type: JobType): Promise<void> {
|
||||
complete(event, missingAmount(), { output })
|
||||
}
|
||||
|
||||
await validateJobRequest(event)
|
||||
await validateJobRequest(event).catch((err) =>
|
||||
log(`Job validation error: `, err)
|
||||
)
|
||||
|
||||
if (jobAmount > paidAmount && waitForPaymentBeforeProcessing()) {
|
||||
await reqPayment()
|
||||
|
@ -6,9 +6,15 @@ import { ndk } from '../main.js'
|
||||
import { addAmount } from '../job-types/speech-to-text.js'
|
||||
|
||||
export async function validateJobRequest(event: NDKEvent): Promise<void> {
|
||||
await validateExpiration(event)
|
||||
await validateRequester(event)
|
||||
await validateNoRecentResults(event)
|
||||
await validateExpiration(event).catch((err) => {
|
||||
throw `Expiration error: ${err}`
|
||||
})
|
||||
await validateRequester(event).catch((err) => {
|
||||
throw `Requester error: ${err}`
|
||||
})
|
||||
await validateNoRecentResults(event).catch((err) => {
|
||||
throw `No recent results error: ${err}`
|
||||
})
|
||||
}
|
||||
|
||||
export async function requirePayment(
|
||||
|
Loading…
Reference in New Issue
Block a user