Skip to content

Commit

Permalink
Before sending to Sentry check if the error is a known chain error
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutgon committed Oct 22, 2024
1 parent 861414b commit 23afd49
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { runDocumentAtCommit } from '@latitude-data/core/services/commits/runDoc
import { captureException } from '$/common/sentry'
import { Factory } from 'hono/factory'
import { streamSSE } from 'hono/streaming'
import { getUnknownError } from 'node_modules/@latitude-data/core/src/jobs/utils/getUnknownError'
import { z } from 'zod'

import { chainEventPresenter, getData } from './_shared'
Expand Down Expand Up @@ -59,7 +60,11 @@ export const runHandler = factory.createHandlers(
}
},
(error: Error) => {
captureException(error)
const unknownError = getUnknownError(error)

if (unknownError) {
captureException(error)
}

return Promise.resolve()
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export default function Preview({
)}

<div className='flex flex-row w-full items-center justify-center'>
{error || (metadata?.errors.length ?? 0) > 0 ? (
{false && (error || (metadata?.errors.length ?? 0) > 0) ? (
<Tooltip
side='bottom'
trigger={
Expand Down
8 changes: 6 additions & 2 deletions apps/workers/src/workers/_shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as jobs from '@latitude-data/core/jobs/definitions'
import { NotFoundError } from '@latitude-data/core/lib/errors'
import { captureException } from '$/utils/sentry'
import { Processor } from 'bullmq'
import { getUnknownError } from 'node_modules/@latitude-data/core/src/jobs/utils/getUnknownError'

export const buildProcessor =
(queues: Queues[]): Processor =>
Expand All @@ -18,9 +19,12 @@ export const buildProcessor =
try {
await jobFn(job)
} catch (error) {
captureException(error as Error)
const unknownError = getUnknownError(error)

throw error
if (unknownError) {
captureException(error as Error)
throw error
}
}
} else {
throw new NotFoundError(`Job ${job.name} not found`)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,16 @@
import { Job } from 'bullmq'

import { RunErrorCodes } from '../../../constants'
import { Result } from '../../../lib'
import { queues } from '../../../queues'
import {
DocumentLogsWithErrorsRepository,
EvaluationsRepository,
} from '../../../repositories'
import { isChainError } from '../../../services/chains/ChainStreamConsumer'
import { runEvaluation } from '../../../services/evaluations/run'
import { WebsocketClient } from '../../../websockets/workers'
import { getUnknownError } from '../../utils/getUnknownError'
import { ProgressTracker } from '../../utils/progressTracker'

/**
* We only throw an error in an evaluation run in 2 situations
*
* 1. Is a ChainError of type `unknown`. This is the catch in `runChain` not knowing what happened.
* 2. is not a `ChainError` so it means something exploded.
*/
function throwIfUnknownError(
error: unknown | undefined,
): asserts error is Error {
const isAllGood =
!error || (isChainError(error) && error.errorCode !== RunErrorCodes.Unknown)

if (isAllGood) return

throw error
}

async function fetchData({
workspaceId,
evaluationId,
Expand Down Expand Up @@ -107,5 +89,7 @@ export async function runEvaluationJob(job: Job<RunEvaluationJobData>) {
},
})

throwIfUnknownError(error)
const unknownError = getUnknownError(error)

if (unknownError) throw unknownError
}
17 changes: 17 additions & 0 deletions packages/core/src/jobs/utils/getUnknownError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { RunErrorCodes } from '../../constants'
import { isChainError } from '../../services/chains/ChainStreamConsumer'

/**
* We only throw an error if is not a known run error.
*
* 1. Is a ChainError of type `unknown`. This is the catch in `runChain` not knowing what happened.
* 2. is not a `ChainError` so it means something exploded.
*/
export function getUnknownError(error: Error | unknown | undefined) {
const isAllGood =
!error || (isChainError(error) && error.errorCode !== RunErrorCodes.Unknown)

if (isAllGood) return null

return error as Error
}

0 comments on commit 23afd49

Please sign in to comment.