chore: improved error handling

This commit is contained in:
Otto 2024-07-01 14:12:53 +03:00
parent 90cdcf8745
commit 3bd70e1281
3 changed files with 49 additions and 14 deletions

View File

@ -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
}

View File

@ -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()

View File

@ -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(