add logging (#187)

This commit is contained in:
KM Koushik
2025-07-26 20:05:34 +10:00
committed by GitHub
parent 5612d7a3eb
commit 202fbeacb6
24 changed files with 490 additions and 134 deletions

View File

@@ -2,6 +2,7 @@ import { Context } from "hono";
import { HTTPException } from "hono/http-exception";
import { StatusCode, ContentfulStatusCode } from "hono/utils/http-status";
import { z } from "zod";
import { logger } from "../logger/log";
const ErrorCode = z.enum([
"BAD_REQUEST",
@@ -79,11 +80,10 @@ export function handleError(err: Error, c: Context): Response {
*/
if (err instanceof UnsendApiError) {
if (err.status >= 500) {
console.error(err.message, {
name: err.name,
code: err.code,
status: err.status,
});
logger.error(
{ name: err.name, code: err.code, status: err.status, err },
err.message
);
}
return c.json(
{
@@ -102,10 +102,10 @@ export function handleError(err: Error, c: Context): Response {
*/
if (err instanceof HTTPException) {
if (err.status >= 500) {
console.error("HTTPException", {
message: err.message,
status: err.status,
});
logger.error(
{ message: err.message, status: err.status, err },
"HTTPException"
);
}
const code = statusToCode(err.status);
return c.json(
@@ -122,12 +122,16 @@ export function handleError(err: Error, c: Context): Response {
/**
* We're lost here, all we can do is return a 500 and log it to investigate
*/
console.error("unhandled exception", {
name: err.name,
message: err.message,
cause: err.cause,
stack: err.stack,
});
logger.error(
{
err,
name: err.name,
message: err.message,
cause: err.cause,
stack: err.stack,
},
"unhandled exception"
);
return c.json(
{
error: {

View File

@@ -3,6 +3,7 @@ import { db } from "../db";
import { UnsendApiError } from "./api-error";
import { getTeamAndApiKey } from "../service/api-service";
import { isSelfHosted } from "~/utils/common";
import { logger } from "../logger/log";
/**
* Gets the team from the token. Also will check if the token is valid.
@@ -54,7 +55,9 @@ export const getTeamFromToken = async (c: Context) => {
lastUsed: new Date(),
},
})
.catch(console.error);
.catch((err) =>
logger.error({ err }, "Failed to update lastUsed on API key")
);
return { ...team, apiKeyId: apiKey.id };
};

View File

@@ -8,6 +8,7 @@ import { getTeamFromToken } from "~/server/public-api/auth";
import { isSelfHosted } from "~/utils/common";
import { UnsendApiError } from "./api-error";
import { Team } from "@prisma/client";
import { logger } from "../logger/log";
// Define AppEnv for Hono context
export type AppEnv = {
@@ -38,7 +39,7 @@ export function getApp() {
if (error instanceof UnsendApiError) {
throw error;
}
console.error("Error in getTeamFromToken middleware:", error);
logger.error({ err: error }, "Error in getTeamFromToken middleware");
throw new UnsendApiError({
code: "INTERNAL_SERVER_ERROR",
message: "Authentication failed",
@@ -84,7 +85,7 @@ export function getApp() {
// We rely on expire being set for new keys.
ttl = await redis.ttl(key);
} catch (error) {
console.error("Redis error during rate limiting:", error);
logger.error({ err: error }, "Redis error during rate limiting");
// Alternatively, you could fail closed by throwing an error here.
return next();
}