All files / src/api errors.ts

89.28% Statements 25/28
54.16% Branches 13/24
100% Functions 10/10
88.46% Lines 23/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130                4x           12x 12x 12x 12x 12x           3x       1x       1x       2x       1x       1x       1x                                       4x   5x   5x   2x                       3x 3x     3x                                   2x 1x                               1x        
import { ErrorHandler } from "hono";
import { ContentfulStatusCode } from "hono/utils/http-status";
 
/**
 * Standardized API error class.
 * Throw this from any route handler — the errorHandler middleware
 * will format it into `{ error: { message, code, details? } }`.
 */
export class ApiError extends Error {
    public readonly statusCode: number;
    public readonly code: string;
    public readonly details?: unknown;
 
    constructor(statusCode: number, code: string, message: string, details?: unknown) {
        super(message);
        this.name = "ApiError";
        this.statusCode = statusCode;
        this.code = code;
        this.details = details;
    }
 
    // ── Factory methods ──────────────────────────────────────────────
 
    static badRequest(message: string, code = "BAD_REQUEST", details?: unknown): ApiError {
        return new ApiError(400, code, message, details);
    }
 
    static unauthorized(message: string, code = "UNAUTHORIZED"): ApiError {
        return new ApiError(401, code, message);
    }
 
    static forbidden(message: string, code = "FORBIDDEN"): ApiError {
        return new ApiError(403, code, message);
    }
 
    static notFound(message: string, code = "NOT_FOUND"): ApiError {
        return new ApiError(404, code, message);
    }
 
    static conflict(message: string, code = "CONFLICT"): ApiError {
        return new ApiError(409, code, message);
    }
 
    static internal(message: string, code = "INTERNAL_ERROR"): ApiError {
        return new ApiError(500, code, message);
    }
 
    static serviceUnavailable(message: string, code = "SERVICE_UNAVAILABLE"): ApiError {
        return new ApiError(503, code, message);
    }
}
 
/**
 * Canonical error response shape:
 * `{ error: { message: string, code: string, details?: unknown } }`
 */
export interface ErrorResponse {
    error: {
        message: string;
        code: string;
        details?: unknown;
    };
}
 
/**
 * Hono error-handling middleware (`app.onError`).
 * Converts any error into the canonical `{ error: { message, code } }` shape.
 */
export const errorHandler: ErrorHandler = (err, c) => {
    // Typecast custom error properties
    const error = err as Error & { statusCode?: number; code?: string; details?: unknown };
 
    if (error instanceof ApiError) {
        // Operational errors — log at warn level
        console.warn(
            `⚠️ [API] ${c.req.method} ${c.req.path} → ${error.statusCode} ${error.code}: ${error.message}`
        );
        return c.json({
            error: {
                message: error.message,
                code: error.code,
                ...(error.details !== undefined && { details: error.details })
            }
        } satisfies ErrorResponse, (error.statusCode || 500) as ContentfulStatusCode);
    }
 
    const statusCode = error.statusCode || codeToStatus(error.code) || 500;
    const code = error.code || "INTERNAL_ERROR";
 
    // Unexpected errors — log at error level with full stack
    console.error(
        `❌ [API] ${c.req.method} ${c.req.path} → ${statusCode} ${code}: ${error.message}`
    );
    console.error(error.stack || error);
 
    return c.json({
        error: {
            message: error.message || "An unexpected error occurred",
            code,
            ...(error.details !== undefined && { details: error.details })
        }
    } satisfies ErrorResponse, statusCode as ContentfulStatusCode);
};
 
/**
 * Map known error codes to HTTP status codes.
 */
function codeToStatus(code?: string): number | undefined {
    if (!code) return undefined;
    const map: Record<string, number> = {
        BAD_REQUEST: 400,
        INVALID_INPUT: 400,
        WEAK_PASSWORD: 400,
        UNAUTHORIZED: 401,
        INVALID_CREDENTIALS: 401,
        INVALID_TOKEN: 401,
        FORBIDDEN: 403,
        NOT_FOUND: 404,
        CONFLICT: 409,
        EMAIL_EXISTS: 409,
        ROLE_EXISTS: 409,
        INTERNAL_ERROR: 500,
        NOT_CONFIGURED: 503,
        SERVICE_UNAVAILABLE: 503,
    };
    return map[code];
}