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
@@ -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');
}
}