|
| 1 | +import { HTTPException } from "@hono/hono/http-exception"; |
| 2 | +import type { LazyLocalizedString } from "./LazyLocalizedString.ts"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Options for creating a LocalizedHttpException. |
| 6 | + */ |
| 7 | +export interface LocalizedHttpExceptionOptions { |
| 8 | + /** |
| 9 | + * Technical message for this error. Logged for debugging purposes. |
| 10 | + * This is the only message guaranteed to be logged. |
| 11 | + */ |
| 12 | + readonly technicalMessage?: string; |
| 13 | + /** |
| 14 | + * HTTP status code to be used for this error. |
| 15 | + * Defaults to 500 (Internal Server Error). |
| 16 | + */ |
| 17 | + readonly status?: HTTPException["status"]; |
| 18 | + /** |
| 19 | + * Optional cause of this error. Logged for debugging purposes. |
| 20 | + */ |
| 21 | + readonly cause?: unknown; |
| 22 | + /** |
| 23 | + * Localized title for this error. Shown to the user. |
| 24 | + */ |
| 25 | + readonly localizedTitle?: LazyLocalizedString; |
| 26 | + /** |
| 27 | + * Localized message for this error. Shown to the user. |
| 28 | + */ |
| 29 | + readonly localizedMessage?: LazyLocalizedString; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * An HTTP exception that supports localization. |
| 34 | + * |
| 35 | + * Contains both technical details for logging and user-friendly localized messages. |
| 36 | + * |
| 37 | + * Use {@link fromCause} to convert arbitrary errors / exceptions into LocalizedHttpExceptions. |
| 38 | + * |
| 39 | + * By logging the LocalizedHttpException, the technical message and cause are preserved for debugging purposes. |
| 40 | + * Logs and user messages can be correlated using the {@link errorId}. |
| 41 | + * |
| 42 | + * See {@link LocalizedHttpExceptionOptions} for details on the available options. |
| 43 | + * |
| 44 | + * @example |
| 45 | + * |
| 46 | + * ```ts |
| 47 | + * throw new LocalizedHttpException({ |
| 48 | + * technicalMessage: "Database connection failed", |
| 49 | + * status: 503, |
| 50 | + * localizedTitle: lt`Service Unavailable`, |
| 51 | + * localizedMessage: lt`The service is currently unavailable. Please try again later.`, |
| 52 | + * }); |
| 53 | + * ``` |
| 54 | + * |
| 55 | + * @example |
| 56 | + * |
| 57 | + * ```ts |
| 58 | + * router.onError((err, c) => { |
| 59 | + * const localizedError = LocalizedHttpException.fromCause(err); |
| 60 | + * console.error("[router]", localizedError); |
| 61 | + * c.status(localizedError.status || 500); |
| 62 | + * return c.render( |
| 63 | + * <> |
| 64 | + * <h1>{ t(localizedError.options.localizedTitle ?? lt`Unknown Error`)}</h1> |
| 65 | + * <p>{ t(localizedError.options.localizedMessage ?? lt`An unexpected error occurred while processing your request.`) }</p> |
| 66 | + * <p>{ t(`Please specify the following error ID when contacting support:`) }</p> |
| 67 | + * <pre><code>{ localizedError.errorId }</code></pre> |
| 68 | + * </> |
| 69 | + * ); |
| 70 | + * }); |
| 71 | + * ``` |
| 72 | + */ |
| 73 | +export class LocalizedHttpException extends HTTPException { |
| 74 | + /** |
| 75 | + * Unique error ID for this exception instance. |
| 76 | + * Used for correlating logs with user reports. |
| 77 | + * |
| 78 | + * Format: ISO timestamp + "-" + first 8 characters of a UUIDv4 |
| 79 | + */ |
| 80 | + public readonly errorId: string = new Date().toISOString() + "-" + |
| 81 | + crypto.randomUUID().substring(0, 8); |
| 82 | + |
| 83 | + /** |
| 84 | + * Creates a new LocalizedHttpException. |
| 85 | + * Use {@link fromCause} to convert arbitrary errors / exceptions into LocalizedHttpExceptions. |
| 86 | + * @param options the options for the error. See {@link LocalizedHttpExceptionOptions} for details |
| 87 | + */ |
| 88 | + constructor( |
| 89 | + public readonly options: LocalizedHttpExceptionOptions = {}, |
| 90 | + ) { |
| 91 | + super(options.status ?? 500, { |
| 92 | + cause: options.cause, |
| 93 | + }); |
| 94 | + this.message = options.technicalMessage |
| 95 | + ? `<${this.errorId}> ${options.technicalMessage}` |
| 96 | + : `<${this.errorId}>`; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Converts an arbitrary error into a LocalizedHttpException. |
| 101 | + * |
| 102 | + * Useful for error handlers that can receive any kind of error. |
| 103 | + * After conversion, the returned LocalizedHttpException can be used to |
| 104 | + * retrieve localized messages for the user. |
| 105 | + * |
| 106 | + * By logging the returned LocalizedHttpException, the technical message |
| 107 | + * and cause are preserved for debugging purposes. |
| 108 | + * Logs and user messages can be correlated using the {@link errorId}. |
| 109 | + * @param error the original error |
| 110 | + * @returns a corresponding {@link LocalizedHttpException} |
| 111 | + */ |
| 112 | + static fromCause( |
| 113 | + error: unknown, |
| 114 | + ): LocalizedHttpException { |
| 115 | + if (error instanceof LocalizedHttpException) { |
| 116 | + return error; |
| 117 | + } |
| 118 | + if (error instanceof HTTPException) { |
| 119 | + return new LocalizedHttpException({ |
| 120 | + technicalMessage: error.message, |
| 121 | + status: error.status, |
| 122 | + cause: error, |
| 123 | + }); |
| 124 | + } |
| 125 | + return new LocalizedHttpException({ |
| 126 | + technicalMessage: "Unknown error (see cause for details)", |
| 127 | + cause: error, |
| 128 | + }); |
| 129 | + } |
| 130 | +} |
0 commit comments