generated from nhcarrigan/template
feat: error handling, logging, analytics, OG tags, and sticky sidebar (#44)
## Summary
- Add comprehensive try/catch error handling across all API routes, middleware, and the Hono global error handler, piping every unhandled error to the `@nhcarrigan/logger` service to prevent silent crashes and unhandled Promise rejections
- Add a `logError` utility on the frontend that forwards errors through the overridden `console.error` to the backend telemetry endpoint; apply it to every silent `catch {}` block in the game context, sound, notification, and clipboard utilities, and wrap the React tree in an `ErrorBoundary`
- Add Plausible analytics, Open Graph + Twitter Card meta tags, Tree-Nation widget, and Google Ads to `index.html`
- Make the game sidebar sticky with a `--resource-bar-height` CSS custom property offset so it stays viewport-height without overlapping the resource bar; reset sticky behaviour in the mobile responsive override
## Test plan
- [ ] Lint passes: `pnpm lint`
- [ ] Build passes: `pnpm build`
- [ ] Verify errors thrown in API routes appear in the logger service rather than crashing the process
- [ ] Verify frontend errors appear in the `/api/fe/error` backend log
- [ ] Verify Open Graph tags render correctly when sharing the URL
- [ ] Verify Plausible analytics fires on page load
- [ ] Verify Tree-Nation badge renders in the sidebar
- [ ] Verify sidebar stays fixed while the main content scrolls on desktop
- [ ] Verify mobile layout is unaffected
✨ This issue was created with help from Hikari~ 🌸
Reviewed-on: #44
Co-authored-by: Hikari <hikari@nhcarrigan.com>
Co-committed-by: Hikari <hikari@nhcarrigan.com>
This commit was merged in pull request #44.
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* @file Frontend logging routes that pipe client-side logs to the telemetry service.
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
import { Hono } from "hono";
|
||||
import { logger } from "../services/logger.js";
|
||||
|
||||
const validLevels = new Set([ "debug", "info", "warn" ]);
|
||||
|
||||
const frontendRouter = new Hono();
|
||||
|
||||
frontendRouter.post("/log", async(context) => {
|
||||
try {
|
||||
const body = await context.req.json<{ level: string; message: string }>();
|
||||
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions -- Runtime body validation
|
||||
if (!body.level || !body.message || !validLevels.has(body.level)) {
|
||||
return context.json({ error: "level and message are required" }, 400);
|
||||
}
|
||||
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- validated above */
|
||||
void logger.log(body.level as "debug" | "info" | "warn", `[FE] ${body.message}`);
|
||||
return context.json({ ok: true });
|
||||
} catch (error) {
|
||||
void logger.error(
|
||||
"frontend_log",
|
||||
error instanceof Error
|
||||
? error
|
||||
: new Error(String(error)),
|
||||
);
|
||||
return context.json({ error: "Internal server error" }, 500);
|
||||
}
|
||||
});
|
||||
|
||||
frontendRouter.post("/error", async(context) => {
|
||||
try {
|
||||
const body = await context.req.json<{ context: string; message: string }>();
|
||||
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions -- Runtime body validation
|
||||
if (!body.context || !body.message) {
|
||||
return context.json({ error: "context and message are required" }, 400);
|
||||
}
|
||||
void logger.error(`[FE] ${body.context}`, new Error(body.message));
|
||||
return context.json({ ok: true });
|
||||
} catch (error) {
|
||||
void logger.error(
|
||||
"frontend_error",
|
||||
error instanceof Error
|
||||
? error
|
||||
: new Error(String(error)),
|
||||
);
|
||||
return context.json({ error: "Internal server error" }, 500);
|
||||
}
|
||||
});
|
||||
|
||||
export { frontendRouter };
|
||||
Reference in New Issue
Block a user