Refactor error logging across the application to use a centralized logger

- Introduced a new logger utility (`logError`) to standardize error logging.
- Replaced all instances of `console.error` with `logError` in various API routes and libraries.
- Enhanced error logging to sanitize sensitive information, particularly for Prisma and Stripe errors.
- Ensured consistent error handling and logging practices throughout the codebase.
This commit is contained in:
Yusuf İpek
2026-04-10 21:10:09 +03:00
parent 07f7b6fb02
commit 8014fc3986
60 changed files with 235 additions and 115 deletions
@@ -6,6 +6,7 @@ import { getApprovalCandidatesForProject } from '@/lib/approval-workflow';
import { notifyUsers } from '@/lib/notifications';
import { rateLimit } from '@/lib/rate-limit';
import { apiErrors, successResponse, withCacheControl } from '@/lib/api-response';
import { logError } from '@/lib/logger';
type RouteParams = { params: Promise<{ versionId: string }> };
@@ -54,7 +55,7 @@ export async function GET(_request: NextRequest, { params }: RouteParams) {
const response = successResponse({ requests });
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error fetching approvals:', error);
logError('Error fetching approvals:', error);
return apiErrors.internalError('Failed to fetch approvals');
}
}
@@ -165,7 +166,7 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
message: message || undefined,
url: requestUrl,
}).catch((error) => {
console.error('Approval request notification failed:', error);
logError('Approval request notification failed:', error);
});
const response = successResponse({ request: created }, 201);
@@ -177,7 +178,7 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
if (isSerializableConflict(error)) {
return apiErrors.conflict('Request state changed. Please try again.');
}
console.error('Error creating approval request:', error);
logError('Error creating approval request:', error);
return apiErrors.internalError('Failed to create approval request');
}
}
@@ -9,6 +9,7 @@ import {
} from '@/lib/comment-export';
import { apiErrors, withCacheControl } from '@/lib/api-response';
import { rateLimit } from '@/lib/rate-limit';
import { logError } from '@/lib/logger';
type RouteParams = { params: Promise<{ versionId: string }> };
const MAX_EXPORT_COMMENTS = 5000;
@@ -157,7 +158,7 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error exporting comments:', error);
logError('Error exporting comments:', error);
return apiErrors.internalError('Failed to export comments');
}
}
@@ -11,6 +11,7 @@ import { r2Client, R2_BUCKET_NAME } from '@/lib/r2';
import { ensureGuestIdentityFromRequest, getGuestIdentityFromRequest, setGuestIdentityCookie } from '@/lib/guest-identity';
import { extractImageFileNameFromProxyUrl, sanitizeAssetDisplayName } from '@/lib/video-assets';
import { validateAnnotationStrokes } from '@/lib/validation';
import { logError } from '@/lib/logger';
type RouteParams = { params: Promise<{ versionId: string }> };
const SAFE_IMAGE_PATH = /^\/api\/upload\/image\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.[a-z0-9]+$/i;
@@ -180,7 +181,7 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
response.headers.set('ETag', etag);
return withCacheControl(response, 'private, no-cache');
} catch (error) {
console.error('Error fetching comments:', error);
logError('Error fetching comments:', error);
return apiErrors.internalError('Failed to fetch comments');
}
}
@@ -391,7 +392,7 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
parentAuthor: parentComment?.author?.name || parentComment?.guestName || 'Someone',
timestamp: ts,
url: `${baseUrl}/watch/${version.video.id}`,
}).catch((err) => console.error('Notification failed:', err));
}).catch((err) => logError('Notification failed:', err));
} else {
notifyProjectOwner(project.ownerId, {
type: 'new_comment',
@@ -401,7 +402,7 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
commentText: content?.trim() || (imageUrl ? '(image attachment)' : '(voice note)'),
timestamp: ts,
url: `${baseUrl}/watch/${version.video.id}`,
}).catch((err) => console.error('Notification failed:', err));
}).catch((err) => logError('Notification failed:', err));
}
}
@@ -428,7 +429,7 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
}
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error creating comment:', error);
logError('Error creating comment:', error);
return apiErrors.internalError('Failed to create comment');
}
}
@@ -7,6 +7,7 @@ import { getShareSessionFromRequest } from '@/lib/share-session';
import { resolveServerBunnyCdnHostname } from '@/lib/bunny-cdn';
import { NextRequest } from 'next/server';
import { DownloadEgressSource } from '@prisma/client';
import { logError } from '@/lib/logger';
type RouteParams = { params: Promise<{ versionId: string }> };
type BunnyDownloadSourcePreference = 'auto' | 'original' | 'compressed';
@@ -483,12 +484,12 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
},
});
} catch (egressError) {
console.error('Failed to record download egress event:', egressError);
logError('Failed to record download egress event:', egressError);
}
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error downloading version:', error);
logError('Error downloading version:', error);
return apiErrors.internalError('Failed to download video');
}
}