fix: separate 400/500 error cases in feedback proxy route
This commit is contained in:
@@ -3,19 +3,24 @@ import { NextRequest, NextResponse } from 'next/server'
|
|||||||
const API_BASE_URL = process.env.API_BASE_URL || process.env.NEXT_PUBLIC_API_BASE_URL
|
const API_BASE_URL = process.env.API_BASE_URL || process.env.NEXT_PUBLIC_API_BASE_URL
|
||||||
|
|
||||||
export async function POST(req: NextRequest) {
|
export async function POST(req: NextRequest) {
|
||||||
|
let body: unknown
|
||||||
try {
|
try {
|
||||||
const body = await req.json()
|
body = await req.json()
|
||||||
|
} catch {
|
||||||
|
return NextResponse.json({ error: 'Invalid request' }, { status: 400 })
|
||||||
|
}
|
||||||
|
|
||||||
const ip =
|
const ip =
|
||||||
req.headers.get('x-forwarded-for')?.split(',')[0]?.trim() ??
|
req.headers.get('x-forwarded-for')?.split(',')[0]?.trim() ??
|
||||||
req.headers.get('x-real-ip') ??
|
req.headers.get('x-real-ip') ??
|
||||||
'unknown'
|
'unknown'
|
||||||
const userAgent = req.headers.get('user-agent') ?? 'unknown'
|
const userAgent = req.headers.get('user-agent') ?? 'unknown'
|
||||||
|
|
||||||
|
try {
|
||||||
const res = await fetch(`${API_BASE_URL}/api/v1/feedback`, {
|
const res = await fetch(`${API_BASE_URL}/api/v1/feedback`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ ...body, ipAddress: ip, userAgent }),
|
body: JSON.stringify({ ...(body as object), ipAddress: ip, userAgent }),
|
||||||
cache: 'no-store',
|
cache: 'no-store',
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -28,6 +33,6 @@ export async function POST(req: NextRequest) {
|
|||||||
return NextResponse.json({ ok: true })
|
return NextResponse.json({ ok: true })
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('[/api/feedback] proxy error:', e)
|
console.error('[/api/feedback] proxy error:', e)
|
||||||
return NextResponse.json({ error: 'Invalid request' }, { status: 400 })
|
return NextResponse.json({ error: 'Service unavailable' }, { status: 500 })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user