|
| 1 | +import { NextRequest, NextResponse } from 'next/server' |
| 2 | +import prisma from '@/lib/db' |
| 3 | + |
| 4 | +export async function POST(req: NextRequest) { |
| 5 | + try { |
| 6 | + const errorPayload = await req.json() |
| 7 | + |
| 8 | + // Basic validation |
| 9 | + if (!errorPayload || typeof errorPayload.message !== 'string') { |
| 10 | + return NextResponse.json( |
| 11 | + { error: 'Invalid error report payload. "message" is required.' }, |
| 12 | + { status: 400 } |
| 13 | + ) |
| 14 | + } |
| 15 | + |
| 16 | + // Extract data from the payload sent by the Expo app's 'reportError' service |
| 17 | + const { message, stack, platform, options } = errorPayload |
| 18 | + const { isFatal, errorInfo } = options || {} |
| 19 | + |
| 20 | + const newErrorReport = await prisma.errorReport.create({ |
| 21 | + data: { |
| 22 | + message, |
| 23 | + stack: stack || null, |
| 24 | + platform: platform || null, |
| 25 | + isFatal: isFatal || null, |
| 26 | + errorInfo: errorInfo || null, // errorInfo from React Error Boundary |
| 27 | + payload: errorPayload, // Store the full original payload for auditing |
| 28 | + }, |
| 29 | + }) |
| 30 | + |
| 31 | + console.log('Error report logged:', newErrorReport.id) |
| 32 | + return NextResponse.json( |
| 33 | + { message: 'Error logged successfully.', reportId: newErrorReport.id }, |
| 34 | + { status: 201 } |
| 35 | + ) |
| 36 | + } catch (error) { |
| 37 | + // This catches errors in the API endpoint itself |
| 38 | + console.error('Failed to save error report:', error) |
| 39 | + |
| 40 | + // Avoid using 'error()' as a variable name to prevent shadowing |
| 41 | + const e = error as Error |
| 42 | + console.error(e.stack) |
| 43 | + |
| 44 | + return NextResponse.json( |
| 45 | + { error: 'Internal server error while saving error report.' }, |
| 46 | + { status: 500 } |
| 47 | + ) |
| 48 | + } |
| 49 | +} |
0 commit comments