managarten/packages/shared-hono/src/error.ts
Till JS 919fcca4b7 refactor(shared-tailwind): rewrite themes.css to single-layer shadcn convention
Pre-launch theme system audit found multiple parallel layers in themes.css
(--theme-X full hsl strings, --X partial shadcn aliases, --color-X populated
by runtime store with raw channels) plus dead-code companion files. The
inconsistency caused light-mode regressions when scoped-CSS consumers
wrote `var(--color-X)` standalone — the variable holds raw HSL channels
which is invalid as a color value, browser fell back to inherited (white).

Rewrite to one consistent layer:

  - Source of truth: --color-X defined as raw HSL channels (e.g.
    `0 0% 17%`) in :root, .dark, and all variant [data-theme="..."]
    blocks. Matches the format the runtime store
    (@mana/shared-theme/src/utils.ts) writes, eliminating the
    static-fallback-vs-runtime mismatch and the corresponding flash
    of unstyled content on hydration.

  - @theme inline uses self-reference + Tailwind v4 <alpha-value>
    placeholder so utility classes generate correctly AND opacity
    modifiers work: `text-foreground/50` → `hsl(var(--color-foreground) / 0.5)`.

  - @layer components (.btn-primary, .card, .badge, etc.) wraps
    var(--color-X) refs with hsl() — they were broken in light mode
    too for the same reason.

Convention going forward (also documented in the file header):

  1. Markup: use Tailwind utility classes (text-foreground, bg-card, …)
  2. Scoped CSS: hsl(var(--color-X)) — always wrap with hsl()
  3. NEVER raw var(--color-X) in CSS — that's the bug pattern

Net file: 692 → 580 LOC. Single source layer, no indirection.

Also delete dead companion files (zero imports anywhere):
  - tailwind-v4.css (had broken self-reference, never imported)
  - theme-variables.css (legacy hex-based palette)
  - components.css (legacy component utilities)
  - index.js / preset.js / colors.js (Tailwind v3 preset format,
    irrelevant under Tailwind v4)

package.json exports map shrinks accordingly to just `./themes.css`.

Consumers using `hsl(var(--color-X))` (~379 files across mana-web,
manavoxel-web, arcade-web) keep working unchanged — the public API
name `--color-X` is preserved. Only the broken pattern `var(--color-X)`
(~61 files) needs a follow-up sweep, handled in a separate commit.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-09 01:13:06 +02:00

103 lines
2.4 KiB
TypeScript

/**
* Error handling middleware for Hono servers.
*
* Catches unhandled errors and returns consistent JSON responses.
*/
import type { Context, ErrorHandler } from 'hono';
import { HTTPException } from 'hono/http-exception';
import { logger } from '@mana/shared-logger';
/**
* Global error handler — register with `app.onError(errorHandler)`.
*
* Usage:
* ```ts
* import { errorHandler } from '@mana/shared-hono/error';
* const app = new Hono();
* app.onError(errorHandler);
* ```
*/
export function errorHandler(err: Error, c: Context) {
if (err instanceof HTTPException) {
return c.json(
{
error: err.message,
status: err.status,
},
err.status
);
}
logger.error('unhandled', {
path: c.req.path,
method: c.req.method,
message: err.message,
stack: err.stack,
});
return c.json(
{
error: 'Internal server error',
status: 500,
},
500
);
}
/**
* Not-found handler — register with `app.notFound(notFoundHandler)`.
*/
export function notFoundHandler(c: Context) {
return c.json({ error: 'Not found', status: 404 }, 404);
}
/**
* Service-style error handler — returns the legacy `{ statusCode, message }`
* envelope used by `services/mana-{credits,user,analytics,subscriptions,
* auth,events}`. Distinct from the `{ error, status }` shape returned by
* `errorHandler` above (used by `apps/api`).
*
* Replaces 5 byte-identical copies of `src/middleware/error-handler.ts`
* across services. Wire-compatible with their existing clients —
* including the `details` field that gets populated from
* `HTTPException.cause`.
*
* Usage:
* ```ts
* import { serviceErrorHandler } from '@mana/shared-hono';
* const app = new Hono();
* app.onError(serviceErrorHandler);
* ```
*
* Logs unhandled errors via `@mana/shared-logger` so structured JSON
* lines land in the production log sink instead of `console.error`.
*/
export const serviceErrorHandler: ErrorHandler = (err, c) => {
if (err instanceof HTTPException) {
const cause = err.cause as Record<string, unknown> | undefined;
return c.json(
{
statusCode: err.status,
message: err.message,
...(cause ? { details: cause } : {}),
},
err.status
);
}
logger.error('unhandled', {
path: c.req.path,
method: c.req.method,
message: err.message,
stack: err.stack,
});
return c.json(
{
statusCode: 500,
message: 'Internal server error',
},
500
);
};