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
@@ -3,6 +3,7 @@ import { auth, checkProjectAccess } from '@/lib/auth';
import { db } from '@/lib/db';
import { getApprovalCandidatesForProject } from '@/lib/approval-workflow';
import { apiErrors, successResponse, withCacheControl } from '@/lib/api-response';
import { logError } from '@/lib/logger';
type RouteParams = { params: Promise<{ projectId: string }> };
@@ -28,7 +29,7 @@ export async function GET(_request: NextRequest, { params }: RouteParams) {
const response = successResponse({ candidates });
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error fetching approval candidates:', error);
logError('Error fetching approval candidates:', error);
return apiErrors.internalError('Failed to fetch approval candidates');
}
}
@@ -4,6 +4,7 @@ import { auth, checkProjectAccess } from '@/lib/auth';
import { ProjectMemberRole } from '@prisma/client';
import { rateLimit } from '@/lib/rate-limit';
import { apiErrors, successResponse, withCacheControl } from '@/lib/api-response';
import { logError } from '@/lib/logger';
type RouteParams = { params: Promise<{ projectId: string; memberId: string }> };
@@ -65,7 +66,7 @@ export async function PATCH(request: NextRequest, { params }: RouteParams) {
const response = successResponse(updatedMember);
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error updating member role:', error);
logError('Error updating member role:', error);
return apiErrors.internalError('Failed to update member role');
}
}
@@ -116,7 +117,7 @@ export async function DELETE(request: NextRequest, { params }: RouteParams) {
const response = successResponse({ message: 'Member removed' });
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error removing member:', error);
logError('Error removing member:', error);
return apiErrors.internalError('Failed to remove member');
}
}
@@ -4,6 +4,7 @@ import { auth, checkProjectAccess } from '@/lib/auth';
import { db } from '@/lib/db';
import { rateLimit } from '@/lib/rate-limit';
import { apiErrors, successResponse, withCacheControl } from '@/lib/api-response';
import { logError } from '@/lib/logger';
type RouteParams = { params: Promise<{ projectId: string; invitationId: string }> };
@@ -65,7 +66,7 @@ export async function DELETE(request: NextRequest, { params }: RouteParams) {
const response = successResponse({ message: 'Invitation canceled' });
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error canceling project invitation:', error);
logError('Error canceling project invitation:', error);
return apiErrors.internalError('Failed to cancel invitation');
}
}
@@ -5,6 +5,7 @@ import { InvitationRole, ProjectMemberRole } from '@prisma/client';
import { rateLimit } from '@/lib/rate-limit';
import { buildInvitationUrl, createOrRefreshInvitation, sendInvitationEmail } from '@/lib/invitations';
import { apiErrors, successResponse, withCacheControl } from '@/lib/api-response';
import { logError } from '@/lib/logger';
type RouteParams = { params: Promise<{ projectId: string }> };
@@ -78,7 +79,7 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
const response = successResponse({ members, owner, pendingInvitations });
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error fetching project members:', error);
logError('Error fetching project members:', error);
return apiErrors.internalError('Failed to fetch members');
}
}
@@ -172,7 +173,7 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
const response = successResponse({ message: 'Invitation email sent.' });
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error inviting project member:', error);
logError('Error inviting project member:', error);
return apiErrors.internalError('Failed to invite member');
}
}
+4 -3
View File
@@ -6,6 +6,7 @@ import { collectProjectMediaUrls, deleteMediaFilesBestEffort } from '@/lib/r2-cl
import { cleanupBunnyStreamVideosBestEffort } from '@/lib/bunny-stream-cleanup';
import { buildCleanupWarnings, logCleanupWarnings } from '@/lib/cleanup-warnings';
import { apiErrors, successResponse, withCacheControl } from '@/lib/api-response';
import { logError } from '@/lib/logger';
type RouteParams = { params: Promise<{ projectId: string }> };
@@ -79,7 +80,7 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
const response = successResponse(project);
return withCacheControl(response, 'private, max-age=30, stale-while-revalidate=60');
} catch (error) {
console.error('Error fetching project:', error);
logError('Error fetching project:', error);
return apiErrors.internalError('Failed to fetch project');
}
}
@@ -128,7 +129,7 @@ export async function PATCH(request: NextRequest, { params }: RouteParams) {
const response = successResponse(project);
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error updating project:', error);
logError('Error updating project:', error);
return apiErrors.internalError('Failed to update project');
}
}
@@ -212,7 +213,7 @@ export async function DELETE(request: NextRequest, { params }: RouteParams) {
});
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error deleting project:', error);
logError('Error deleting project:', error);
return apiErrors.internalError('Failed to delete project');
}
}
@@ -3,6 +3,7 @@ import { db } from '@/lib/db';
import { auth, checkProjectAccess } from '@/lib/auth';
import { rateLimit } from '@/lib/rate-limit';
import { apiErrors, successResponse, withCacheControl } from '@/lib/api-response';
import { logError } from '@/lib/logger';
type RouteParams = { params: Promise<{ projectId: string; tagId: string }> };
@@ -68,7 +69,7 @@ export async function PATCH(request: NextRequest, { params }: RouteParams) {
const response = successResponse(tag);
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error updating tag:', error);
logError('Error updating tag:', error);
if ((error as { code?: string }).code === 'P2002') {
return apiErrors.conflict('Tag name already exists');
}
@@ -115,7 +116,7 @@ export async function DELETE(request: NextRequest, { params }: RouteParams) {
const response = successResponse({ message: 'Tag deleted' });
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error deleting tag:', error);
logError('Error deleting tag:', error);
return apiErrors.internalError('Failed to delete tag');
}
}
+3 -2
View File
@@ -5,6 +5,7 @@ import { rateLimit } from '@/lib/rate-limit';
import { validateShareLinkAccess } from '@/lib/share-links';
import { getShareSessionFromRequest } from '@/lib/share-session';
import { apiErrors, successResponse, withCacheControl } from '@/lib/api-response';
import { logError } from '@/lib/logger';
type RouteParams = { params: Promise<{ projectId: string }> };
@@ -64,7 +65,7 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
: 'private, no-cache';
return withCacheControl(response, cacheControl);
} catch (error) {
console.error('Error fetching tags:', error);
logError('Error fetching tags:', error);
return apiErrors.internalError('Failed to fetch tags');
}
}
@@ -125,7 +126,7 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
const response = successResponse(tag, 201);
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error creating tag:', error);
logError('Error creating tag:', error);
if ((error as { code?: string }).code === 'P2002') {
return apiErrors.conflict('Tag name already exists');
}
@@ -7,6 +7,7 @@ import { collectVideoMediaUrls, deleteMediaFilesBestEffort } from '@/lib/r2-clea
import { cleanupBunnyStreamVideosBestEffort } from '@/lib/bunny-stream-cleanup';
import { buildCleanupWarnings, logCleanupWarnings } from '@/lib/cleanup-warnings';
import { apiErrors, successResponse, withCacheControl } from '@/lib/api-response';
import { logError } from '@/lib/logger';
type RouteParams = { params: Promise<{ projectId: string; videoId: string }> };
@@ -134,7 +135,7 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
return withCacheControl(response, 'private, no-cache');
} catch (error) {
console.error('Error fetching video:', error);
logError('Error fetching video:', error);
return apiErrors.internalError('Failed to fetch video');
}
}
@@ -189,7 +190,7 @@ export async function PATCH(request: NextRequest, { params }: RouteParams) {
const response = successResponse(updatedVideo);
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error updating video:', error);
logError('Error updating video:', error);
return apiErrors.internalError('Failed to update video');
}
}
@@ -270,7 +271,7 @@ export async function DELETE(request: NextRequest, { params }: RouteParams) {
});
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error deleting video:', error);
logError('Error deleting video:', error);
return apiErrors.internalError('Failed to delete video');
}
}
@@ -7,6 +7,7 @@ import { apiErrors, successResponse, withCacheControl } from '@/lib/api-response
import { db } from '@/lib/db';
import { rateLimit } from '@/lib/rate-limit';
import { MAX_SHARE_PASSWORD_LENGTH } from '@/lib/share-links';
import { logError } from '@/lib/logger';
type RouteParams = { params: Promise<{ projectId: string; videoId: string }> };
@@ -123,7 +124,7 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error fetching video share link:', error);
logError('Error fetching video share link:', error);
return apiErrors.internalError('Failed to fetch video share link');
}
}
@@ -238,7 +239,7 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error creating video share link:', error);
logError('Error creating video share link:', error);
return apiErrors.internalError('Failed to create video share link');
}
}
@@ -314,7 +315,7 @@ export async function PATCH(request: NextRequest, { params }: RouteParams) {
const response = successResponse(serializeShareLink(request, videoId, updated));
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error updating video share link:', error);
logError('Error updating video share link:', error);
return apiErrors.internalError('Failed to update video share link');
}
}
@@ -345,7 +346,7 @@ export async function DELETE(request: NextRequest, { params }: RouteParams) {
const response = successResponse({ message: 'Video share link revoked' });
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error deleting video share link:', error);
logError('Error deleting video share link:', error);
return apiErrors.internalError('Failed to delete video share link');
}
}
@@ -5,6 +5,7 @@ import { rateLimit } from '@/lib/rate-limit';
import { cleanupBunnyStreamVideosBestEffort } from '@/lib/bunny-stream-cleanup';
import { buildCleanupWarnings, logCleanupWarnings } from '@/lib/cleanup-warnings';
import { apiErrors, successResponse, withCacheControl } from '@/lib/api-response';
import { logError } from '@/lib/logger';
type RouteParams = { params: Promise<{ projectId: string; videoId: string; versionId: string }> };
@@ -75,7 +76,7 @@ export async function PATCH(request: NextRequest, { params }: RouteParams) {
const response = successResponse(updated);
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error updating version:', error);
logError('Error updating version:', error);
return apiErrors.internalError('Failed to update version');
}
}
@@ -148,7 +149,7 @@ export async function DELETE(request: NextRequest, { params }: RouteParams) {
});
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error deleting version:', error);
logError('Error deleting version:', error);
return apiErrors.internalError('Failed to delete version');
}
}
@@ -6,6 +6,7 @@ import { rateLimit } from '@/lib/rate-limit';
import { notifyProjectOwner } from '@/lib/notifications';
import { apiErrors, successResponse, withCacheControl } from '@/lib/api-response';
import { verifyBunnyUploadToken } from '@/lib/bunny-upload-token';
import { logError } from '@/lib/logger';
type RouteParams = { params: Promise<{ projectId: string; videoId: string }> };
@@ -42,7 +43,7 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
const response = successResponse({ versions });
return withCacheControl(response, 'private, max-age=30, stale-while-revalidate=60');
} catch (error) {
console.error('Error fetching versions:', error);
logError('Error fetching versions:', error);
return apiErrors.internalError('Failed to fetch versions');
}
}
@@ -166,13 +167,13 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
versionLabel: version.versionLabel || `Version ${version.versionNumber}`,
addedBy: session.user.name || 'A team member',
url: `${baseUrl}/watch/${video.id}`,
}).catch((err) => console.error('Notification failed:', err));
}).catch((err) => logError('Notification failed:', err));
}
const response = successResponse(version, 201);
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error creating version:', error);
logError('Error creating version:', error);
return apiErrors.internalError('Failed to create version');
}
}
@@ -7,6 +7,7 @@ import crypto from 'crypto';
import { cleanupBunnyStreamVideos } from '@/lib/bunny-stream-cleanup';
import { createBunnyUploadToken, verifyBunnyUploadToken } from '@/lib/bunny-upload-token';
import { isBunnyUploadsFeatureEnabled } from '@/lib/feature-flags';
import { logError } from '@/lib/logger';
type RouteParams = { params: Promise<{ projectId: string }> };
@@ -74,7 +75,7 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
});
if (!bunnyRes.ok) {
console.error('Failed to create Bunny Stream video', await bunnyRes.text());
logError('Failed to create Bunny Stream video', await bunnyRes.text());
return apiErrors.internalError('Failed to initialize video upload with provider');
}
@@ -107,7 +108,7 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error initializing Bunny upload:', error);
logError('Error initializing Bunny upload:', error);
return apiErrors.internalError('Failed to initialize upload');
}
}
@@ -153,7 +154,7 @@ export async function DELETE(request: NextRequest, { params }: RouteParams) {
const response = successResponse({ message: 'Pending upload cleaned up' });
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error cleaning up pending Bunny upload:', error);
logError('Error cleaning up pending Bunny upload:', error);
return apiErrors.internalError('Failed to cleanup pending upload');
}
}
+4 -3
View File
@@ -6,6 +6,7 @@ import { rateLimit } from '@/lib/rate-limit';
import { notifyProjectOwner } from '@/lib/notifications';
import { apiErrors, successResponse, withCacheControl } from '@/lib/api-response';
import { verifyBunnyUploadToken } from '@/lib/bunny-upload-token';
import { logError } from '@/lib/logger';
type RouteParams = { params: Promise<{ projectId: string }> };
@@ -53,7 +54,7 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
const response = successResponse({ videos });
return withCacheControl(response, 'private, max-age=30, stale-while-revalidate=60');
} catch (error) {
console.error('Error fetching videos:', error);
logError('Error fetching videos:', error);
return apiErrors.internalError('Failed to fetch videos');
}
}
@@ -167,13 +168,13 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
videoTitle: title.trim(),
addedBy: session.user.name || 'A team member',
url: `${baseUrl}/watch/${video.id}`,
}).catch((err) => console.error('Notification failed:', err));
}).catch((err) => logError('Notification failed:', err));
}
const response = successResponse(video, 201);
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error creating video:', error);
logError('Error creating video:', error);
return apiErrors.internalError('Failed to create video');
}
}
+3 -2
View File
@@ -6,6 +6,7 @@ import { rateLimit } from '@/lib/rate-limit';
import { buildBillingAccessWhereInput } from '@/lib/billing';
import { apiErrors, successResponse, withCacheControl } from '@/lib/api-response';
import { DEFAULT_COMMENT_TAGS } from '@/lib/comment-tags';
import { logError } from '@/lib/logger';
// GET /api/projects - List all projects for the authenticated user
export async function GET(request: NextRequest) {
@@ -94,7 +95,7 @@ export async function GET(request: NextRequest) {
return withCacheControl(response, 'private, max-age=30, stale-while-revalidate=60');
} catch (error) {
console.error('Error fetching projects:', error);
logError('Error fetching projects:', error);
return apiErrors.internalError('Failed to fetch projects');
}
}
@@ -194,7 +195,7 @@ export async function POST(request: NextRequest) {
const response = successResponse(project, 201);
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error creating project:', error);
logError('Error creating project:', error);
return apiErrors.internalError('Failed to create project');
}
}