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