Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x 1x 10x 10x 1x 1x 10x 10x 1x 1x 1x 1x 1x 1x 1x 10x 10x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x 1x | export const dynamic = 'force-dynamic';
import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
import { addSecurityHeaders } from '@/lib/security';
import {
withAdmin,
withErrorHandling,
type ApiSuccessResponse,
type ApiErrorResponse,
} from '@/lib/api';
import type { Prisma } from '@prisma/client';
/**
* Error list item returned by the API
*/
interface ErrorListItem {
id: number;
level: string;
category: string;
message: string;
stack: string | null;
url: string | null;
metadata: Prisma.JsonValue;
createdAt: Date;
fingerprint: string;
}
/**
* Pagination info returned by the API
*/
interface PaginationInfo {
page: number;
limit: number;
total: number;
totalPages: number;
}
/**
* Error list response structure
*/
interface ErrorListResponse {
data: ErrorListItem[];
pagination: PaginationInfo;
filters: {
categories: string[];
levels: string[];
};
}
/**
* Build Prisma where clause from filter parameters
*/
function buildWhereClause(params: {
category?: string;
level?: string;
search?: string;
startDate?: string;
endDate?: string;
}): Prisma.ErrorLogWhereInput {
const where: Prisma.ErrorLogWhereInput = {};
if (params.category) {
where.category = params.category;
}
if (params.level) {
where.level = params.level;
}
if (params.search) {
// Note: MySQL default collation is case-insensitive, so we don't need mode parameter
where.OR = [
{ message: { contains: params.search } },
{ stack: { contains: params.search } },
{ url: { contains: params.search } },
];
}
if (params.startDate || params.endDate) {
where.createdAt = {};
if (params.startDate) {
where.createdAt.gte = new Date(params.startDate);
}
if (params.endDate) {
where.createdAt.lte = new Date(params.endDate);
}
}
return where;
}
/**
* GET /api/admin/monitoring/errors
*
* Returns paginated, filterable, sortable list of error logs.
* Requires admin authentication.
*
* Query params:
* - page: Page number (default: 1)
* - limit: Items per page (default: 20, max: 100)
* - category: Filter by category
* - level: Filter by level (error, warn, info)
* - search: Full-text search in message, stack, url
* - startDate: Filter errors after this date (ISO string)
* - endDate: Filter errors before this date (ISO string)
* - sortBy: Sort field (default: createdAt)
* - sortOrder: Sort direction (asc, desc) (default: desc)
*/
async function handleGet(
request: NextRequest
): Promise<NextResponse<ApiSuccessResponse<ErrorListResponse> | ApiErrorResponse>> {
const { searchParams } = new URL(request.url);
// Parse pagination params
const page = Math.max(1, parseInt(searchParams.get('page') || '1', 10));
const limit = Math.min(100, Math.max(1, parseInt(searchParams.get('limit') || '20', 10)));
// Parse filter params
const category = searchParams.get('category') || undefined;
const level = searchParams.get('level') || undefined;
const search = searchParams.get('search') || undefined;
const startDate = searchParams.get('startDate') || undefined;
const endDate = searchParams.get('endDate') || undefined;
// Parse sort params
const sortBy = searchParams.get('sortBy') || 'createdAt';
const sortOrder = (searchParams.get('sortOrder') || 'desc') as 'asc' | 'desc';
// Validate sortBy to prevent injection
const allowedSortFields = ['createdAt', 'level', 'category', 'message'];
const safeSortBy = allowedSortFields.includes(sortBy) ? sortBy : 'createdAt';
// Build where clause
const where = buildWhereClause({ category, level, search, startDate, endDate });
// Fetch data in parallel
const [errors, total, categories, levels] = await Promise.all([
prisma.errorLog.findMany({
where,
orderBy: { [safeSortBy]: sortOrder },
skip: (page - 1) * limit,
take: limit,
select: {
id: true,
level: true,
category: true,
message: true,
stack: true,
url: true,
metadata: true,
createdAt: true,
fingerprint: true,
},
}),
prisma.errorLog.count({ where }),
// Get distinct categories for filter dropdown
prisma.errorLog.groupBy({
by: ['category'],
_count: true,
orderBy: { _count: { category: 'desc' } },
}),
// Get distinct levels for filter dropdown
prisma.errorLog.groupBy({
by: ['level'],
_count: true,
}),
]);
const response: ErrorListResponse = {
data: errors,
pagination: {
page,
limit,
total,
totalPages: Math.ceil(total / limit),
},
filters: {
categories: categories.map(c => c.category),
levels: levels.map(l => l.level),
},
};
return addSecurityHeaders(
NextResponse.json({
success: true,
data: response,
})
) as NextResponse<ApiSuccessResponse<ErrorListResponse> | ApiErrorResponse>;
}
export const GET = withErrorHandling(withAdmin(handleGet));
|