Skip to content

Commit 0e5a58b

Browse files
nullcoderClaude
andcommitted
fix: replace console.error with proper logger throughout API
Replaces all console.error calls with proper logger usage for consistency and better error tracking across the API layer. ## Changes - **api-errors.ts**: Replace console.error with createLogger("api-errors") - **gists/route.ts**: Replace console.error with createLogger("api:gists:post") - **api-errors.test.ts**: Update test mocks to use logger instead of console.error ## Benefits - Consistent logging format with timestamps and context - Better error tracking and debugging capability - Follows established logging patterns throughout codebase - Proper error object handling instead of raw console output 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <claude@ghostpaste.dev>
1 parent 6995174 commit 0e5a58b

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

app/api/gists/route.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { FILE_LIMITS } from "@/lib/constants";
55
import { AppError } from "@/types/errors";
66
import { generateSalt, hashPin } from "@/lib/auth";
77
import { errorResponse, ApiErrors, validationError } from "@/lib/api-errors";
8+
import { createLogger } from "@/lib/logger";
89
import type { CreateGistResponse } from "@/types/api";
910
import type { GistMetadata } from "@/types/models";
1011

@@ -57,6 +58,8 @@ async function parseMultipartFormData(request: NextRequest): Promise<{
5758
};
5859
}
5960

61+
const logger = createLogger("api:gists:post");
62+
6063
/**
6164
* POST /api/gists
6265
* Creates a new encrypted gist
@@ -160,12 +163,18 @@ export async function POST(request: NextRequest) {
160163
}
161164

162165
// Log unexpected errors
163-
console.error("Storage error:", error);
166+
logger.error(
167+
"Storage error:",
168+
error instanceof Error ? error : new Error(String(error))
169+
);
164170
return errorResponse(ApiErrors.storageError("Failed to store gist data"));
165171
}
166172
} catch (error) {
167173
// Handle unexpected errors
168-
console.error("Unexpected error in POST /api/gists:", error);
174+
logger.error(
175+
"Unexpected error in POST /api/gists:",
176+
error instanceof Error ? error : new Error(String(error))
177+
);
169178
return errorResponse(
170179
error instanceof Error ? error : new Error("Unknown error")
171180
);

lib/api-errors.test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,21 @@ vi.mock("next/server", () => ({
1515
},
1616
}));
1717

18+
// Mock logger
19+
const mockLogger = {
20+
error: vi.fn(),
21+
warn: vi.fn(),
22+
info: vi.fn(),
23+
debug: vi.fn(),
24+
};
25+
26+
vi.mock("@/lib/logger", () => ({
27+
createLogger: vi.fn(() => mockLogger),
28+
}));
29+
1830
describe("API Error Utilities", () => {
1931
beforeEach(() => {
2032
vi.clearAllMocks();
21-
vi.spyOn(console, "error").mockImplementation(() => {});
2233
});
2334

2435
describe("toApiErrorResponse", () => {
@@ -74,7 +85,7 @@ describe("API Error Utilities", () => {
7485

7586
errorResponse(error);
7687

77-
expect(console.error).toHaveBeenCalledWith("Unexpected error:", error);
88+
expect(mockLogger.error).toHaveBeenCalledWith("Unexpected error:", error);
7889
expect(NextResponse.json).toHaveBeenCalledWith(
7990
{
8091
error: ErrorCode.INTERNAL_SERVER_ERROR,

lib/api-errors.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import { NextResponse } from "next/server";
66
import { AppError, ErrorCode } from "@/types/errors";
77
import type { ApiErrorResponse } from "@/types/api";
8+
import { createLogger } from "@/lib/logger";
89

910
/**
1011
* Convert an AppError to ApiErrorResponse format
@@ -30,7 +31,11 @@ export function errorResponse(
3031
}
3132

3233
// Handle unexpected errors
33-
console.error("Unexpected error:", error);
34+
const logger = createLogger("api-errors");
35+
logger.error(
36+
"Unexpected error:",
37+
error instanceof Error ? error : new Error(String(error))
38+
);
3439
return NextResponse.json<ApiErrorResponse>(
3540
{
3641
error: ErrorCode.INTERNAL_SERVER_ERROR,

0 commit comments

Comments
 (0)