| 190 | * @returns The response object. |
| 191 | */ |
| 192 | export function handleError(err: Error, c: Context<HonoEnv>): Response { |
| 193 | if (err instanceof ApiError) { |
| 194 | return c.json<ErrorSchemaType, StatusCode>( |
| 195 | { |
| 196 | success: false, |
| 197 | error: { |
| 198 | code: err.code, |
| 199 | status: err.status, |
| 200 | message: err.message, |
| 201 | docs: |
| 202 | err.docs || |
| 203 | `https://langbase.com/docs/api-reference/errors/${err.code.toLowerCase()}`, |
| 204 | rateLimit: err.rateLimit as |
| 205 | | RateLimitErrorResponse |
| 206 | | undefined |
| 207 | } |
| 208 | }, |
| 209 | { status: err.status } |
| 210 | ); |
| 211 | } |
| 212 | |
| 213 | if (err instanceof ApiErrorZod) { |
| 214 | return c.json<ErrorSchemaType, StatusCode>( |
| 215 | { |
| 216 | success: false, |
| 217 | error: { |
| 218 | code: err.code, |
| 219 | status: err.status, |
| 220 | message: err.message, |
| 221 | docs: `https://langbase.com/docs/api-reference/errors/${err.code.toLowerCase()}`, |
| 222 | cause: err.cause as ZodError | undefined |
| 223 | } |
| 224 | }, |
| 225 | { status: err.status } |
| 226 | ); |
| 227 | } |
| 228 | |
| 229 | return c.json<ErrorSchemaType, StatusCode>( |
| 230 | { |
| 231 | success: false, |
| 232 | error: { |
| 233 | code: 'INTERNAL_SERVER_ERROR', |
| 234 | status: 500, |
| 235 | message: 'Something unexpected happened.', |
| 236 | docs: 'https://langbase.com/docs/api-reference/errors/internal_server_error' |
| 237 | } |
| 238 | }, |
| 239 | { status: 500 } |
| 240 | ); |
| 241 | } |
| 242 | |
| 243 | export function errorResponse( |
| 244 | c: Context, |