|
1 | 1 | import { captureException } from '../../exports'; |
2 | 2 | import { SPAN_STATUS_ERROR } from '../../tracing'; |
3 | 3 | import type { Span } from '../../types-hoist/span'; |
| 4 | +import type { SpanStatusType } from '../../types-hoist/spanStatus'; |
4 | 5 | import { |
5 | 6 | GEN_AI_INPUT_MESSAGES_ATTRIBUTE, |
6 | 7 | GEN_AI_INPUT_MESSAGES_ORIGINAL_LENGTH_ATTRIBUTE, |
@@ -41,13 +42,35 @@ export function setMessagesAttribute(span: Span, messages: unknown): void { |
41 | 42 | }); |
42 | 43 | } |
43 | 44 |
|
| 45 | +const ANTHROPIC_ERROR_TYPE_TO_SPAN_STATUS: Record<string, SpanStatusType> = { |
| 46 | + invalid_request_error: 'invalid_argument', |
| 47 | + authentication_error: 'unauthenticated', |
| 48 | + permission_error: 'permission_denied', |
| 49 | + not_found_error: 'not_found', |
| 50 | + request_too_large: 'failed_precondition', |
| 51 | + rate_limit_error: 'resource_exhausted', |
| 52 | + api_error: 'internal_error', |
| 53 | + overloaded_error: 'unavailable', |
| 54 | +}; |
| 55 | + |
| 56 | +/** |
| 57 | + * Map an Anthropic API error type to a SpanStatusType value. |
| 58 | + * @see https://docs.anthropic.com/en/api/errors#error-shapes |
| 59 | + */ |
| 60 | +export function mapAnthropicErrorToStatusMessage(errorType: string | undefined): SpanStatusType { |
| 61 | + if (!errorType) { |
| 62 | + return 'internal_error'; |
| 63 | + } |
| 64 | + return ANTHROPIC_ERROR_TYPE_TO_SPAN_STATUS[errorType] || 'internal_error'; |
| 65 | +} |
| 66 | + |
44 | 67 | /** |
45 | 68 | * Capture error information from the response |
46 | 69 | * @see https://docs.anthropic.com/en/api/errors#error-shapes |
47 | 70 | */ |
48 | 71 | export function handleResponseError(span: Span, response: AnthropicAiResponse): void { |
49 | 72 | if (response.error) { |
50 | | - span.setStatus({ code: SPAN_STATUS_ERROR, message: response.error.type || 'internal_error' }); |
| 73 | + span.setStatus({ code: SPAN_STATUS_ERROR, message: mapAnthropicErrorToStatusMessage(response.error.type) }); |
51 | 74 |
|
52 | 75 | captureException(response.error, { |
53 | 76 | mechanism: { |
|
0 commit comments