|
| 1 | +import { Data } from 'effect' |
| 2 | +import { HttpServerResponse } from '@effect/platform' |
| 3 | + |
| 4 | +export class NotFoundError extends Data.TaggedError('NotFoundError')<{ |
| 5 | + readonly message: string |
| 6 | +}> {} |
| 7 | + |
| 8 | +export class UnauthorizedError extends Data.TaggedError('UnauthorizedError')<{ |
| 9 | + readonly message: string |
| 10 | +}> {} |
| 11 | + |
| 12 | +export class ForbiddenError extends Data.TaggedError('ForbiddenError')<{ |
| 13 | + readonly message: string |
| 14 | +}> {} |
| 15 | + |
| 16 | +export class ValidationError extends Data.TaggedError('ValidationError')<{ |
| 17 | + readonly message: string |
| 18 | +}> {} |
| 19 | + |
| 20 | +export class ConflictError extends Data.TaggedError('ConflictError')<{ |
| 21 | + readonly message: string |
| 22 | +}> {} |
| 23 | + |
| 24 | +export class RateLimitedError extends Data.TaggedError('RateLimitedError')<{ |
| 25 | + readonly message: string |
| 26 | + readonly retryAfter: number |
| 27 | +}> {} |
| 28 | + |
| 29 | +export class SandboxNotRunningError extends Data.TaggedError('SandboxNotRunningError')<{ |
| 30 | + readonly message: string |
| 31 | +}> {} |
| 32 | + |
| 33 | +export class NotImplementedError extends Data.TaggedError('NotImplementedError')<{ |
| 34 | + readonly message: string |
| 35 | +}> {} |
| 36 | + |
| 37 | +export type ApiError = |
| 38 | + | NotFoundError |
| 39 | + | UnauthorizedError |
| 40 | + | ForbiddenError |
| 41 | + | ValidationError |
| 42 | + | ConflictError |
| 43 | + | RateLimitedError |
| 44 | + | SandboxNotRunningError |
| 45 | + | NotImplementedError |
| 46 | + |
| 47 | +const STATUS_MAP: Record<ApiError['_tag'], number> = { |
| 48 | + NotFoundError: 404, |
| 49 | + UnauthorizedError: 401, |
| 50 | + ForbiddenError: 403, |
| 51 | + ValidationError: 400, |
| 52 | + ConflictError: 409, |
| 53 | + RateLimitedError: 429, |
| 54 | + SandboxNotRunningError: 409, |
| 55 | + NotImplementedError: 501, |
| 56 | +} |
| 57 | + |
| 58 | +const CODE_MAP: Record<ApiError['_tag'], string> = { |
| 59 | + NotFoundError: 'not_found', |
| 60 | + UnauthorizedError: 'unauthorized', |
| 61 | + ForbiddenError: 'forbidden', |
| 62 | + ValidationError: 'validation_error', |
| 63 | + ConflictError: 'conflict', |
| 64 | + RateLimitedError: 'rate_limited', |
| 65 | + SandboxNotRunningError: 'sandbox_not_running', |
| 66 | + NotImplementedError: 'not_implemented', |
| 67 | +} |
| 68 | + |
| 69 | +export function errorToResponse(error: ApiError, requestId: string) { |
| 70 | + return HttpServerResponse.unsafeJson( |
| 71 | + { |
| 72 | + error: CODE_MAP[error._tag], |
| 73 | + message: error.message, |
| 74 | + request_id: requestId, |
| 75 | + retry_after: error._tag === 'RateLimitedError' ? error.retryAfter : null, |
| 76 | + }, |
| 77 | + { |
| 78 | + status: STATUS_MAP[error._tag], |
| 79 | + headers: { 'content-type': 'application/json' }, |
| 80 | + }, |
| 81 | + ) |
| 82 | +} |
| 83 | + |
| 84 | +/** Formats any error as an API error response. Handles both ApiError and unknown errors. */ |
| 85 | +export function formatApiError(error: unknown, requestId: string = '') { |
| 86 | + const tag = |
| 87 | + typeof error === 'object' && error !== null && '_tag' in error |
| 88 | + ? (error as { _tag: string })._tag |
| 89 | + : null |
| 90 | + if (tag !== null && tag in STATUS_MAP) { |
| 91 | + return errorToResponse(error as ApiError, requestId) |
| 92 | + } |
| 93 | + return HttpServerResponse.unsafeJson( |
| 94 | + { |
| 95 | + error: 'internal_error', |
| 96 | + message: 'An unexpected error occurred', |
| 97 | + request_id: requestId, |
| 98 | + retry_after: null, |
| 99 | + }, |
| 100 | + { status: 500, headers: { 'content-type': 'application/json' } }, |
| 101 | + ) |
| 102 | +} |
0 commit comments