ATlast — you'll never need to find your favorites on another platform again. Find your favs in the ATmosphere.
atproto
16
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor: move error middleware to hono

claude keeps not doing strict types and its hooks aren't working so i
get to fix that next <3

byarielm.fyi 229f630e e5dd8261

verified
+89
+88
packages/api/src/middleware/error.ts
··· 1 + import { Context } from "hono"; 2 + import { 3 + ApiError, 4 + AuthenticationError, 5 + ValidationError, 6 + NotFoundError, 7 + DatabaseError, 8 + } from "../errors"; 9 + 10 + /** 11 + * Error handler middleware for Hono 12 + * Translates custom error classes into proper HTTP responses 13 + * Provides user-friendly messages while logging detailed debug info 14 + */ 15 + export const errorHandler = (err: Error, c: Context) => { 16 + // Log full error details for debugging 17 + console.error("[ERROR]", { 18 + timestamp: new Date().toISOString(), 19 + path: c.req.path, 20 + method: c.req.method, 21 + error: err.message, 22 + stack: err.stack, 23 + // Include user DID if available from auth middleware 24 + userDid: c.get("did") || "unauthenticated", 25 + }); 26 + 27 + // Specific error type handling with user-friendly messages 28 + if (err instanceof ValidationError) { 29 + return c.json( 30 + { 31 + success: false, 32 + error: err.message, 33 + }, 34 + 400, 35 + ); 36 + } 37 + 38 + if (err instanceof AuthenticationError) { 39 + return c.json( 40 + { 41 + success: false, 42 + error: "Your session has expired. Please log in again.", 43 + }, 44 + 401, 45 + ); 46 + } 47 + 48 + if (err instanceof NotFoundError) { 49 + return c.json( 50 + { 51 + success: false, 52 + error: err.message, 53 + }, 54 + 404, 55 + ); 56 + } 57 + 58 + if (err instanceof DatabaseError) { 59 + return c.json( 60 + { 61 + success: false, 62 + error: "Database operation failed. Please try again later.", 63 + }, 64 + 500, 65 + ); 66 + } 67 + 68 + // Handle generic ApiError (custom status codes) 69 + if (err instanceof ApiError) { 70 + return c.json( 71 + { 72 + success: false, 73 + error: err.message, 74 + ...(err.details && { details: err.details }), 75 + }, 76 + err.statusCode as any, 77 + ); 78 + } 79 + 80 + // Generic error fallback (unknown errors) 81 + return c.json( 82 + { 83 + success: false, 84 + error: "Something went wrong. Please try again later.", 85 + }, 86 + 500, 87 + ); 88 + };
+1
packages/api/src/middleware/index.ts
··· 1 + export * from "./error";