Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/app/api/core/utils/withErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,9 @@ export const withErrorHandler = (handler: RequestHandler): RequestHandler => {
return await handler(req, params)
} catch (error: unknown) {
const response = normalizeError(error)
const expectedPrismaError = isExpectedPrismaError(error)

if (shouldLogError(error, response)) {
console.error(error instanceof ZodError ? error.format() : error)
} else if (expectedPrismaError) {
// Keep a breadcrumb for mapped Prisma errors (e.g. P2025) without alerting Sentry —
// a nested-write failure can still signal a real data-integrity issue worth seeing.
console.warn(error)
}

return NextResponse.json({ error: response.message, errors: response.errors }, { status: response.status })
Expand Down
10 changes: 3 additions & 7 deletions src/app/api/tests/utils/withErrorHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ describe('withErrorHandler util', () => {
beforeEach(() => {
jest.clearAllMocks()
jest.spyOn(console, 'error').mockImplementation()
jest.spyOn(console, 'warn').mockImplementation()
req = buildNextRequest(`/?token=iu-token`)
})

Expand Down Expand Up @@ -63,7 +62,7 @@ describe('withErrorHandler util', () => {
expect(console.error).not.toHaveBeenCalled()
})

it('maps Prisma not-found errors to 404 and warns instead of erroring', async () => {
it('maps Prisma not-found errors to 404 without logging', async () => {
const error = new PrismaClientKnownRequestError('Record not found', {
code: 'P2025',
clientVersion: '5.19.0',
Expand All @@ -77,10 +76,9 @@ describe('withErrorHandler util', () => {
expect(response.error).toBe('The requested resource was not found')
expect(nextResponse.status).toBe(httpStatus.NOT_FOUND)
expect(console.error).not.toHaveBeenCalled()
expect(console.warn).toHaveBeenCalledWith(error)
})

it('maps Prisma invalid UUID errors to 404 and warns instead of erroring', async () => {
it('maps Prisma invalid UUID errors to 404 without logging', async () => {
const error = new PrismaClientKnownRequestError('Malformed UUID', {
code: 'P2023',
clientVersion: '5.19.0',
Expand All @@ -95,7 +93,6 @@ describe('withErrorHandler util', () => {
expect(response.error).toBe('The requested resource was not found')
expect(nextResponse.status).toBe(httpStatus.NOT_FOUND)
expect(console.error).not.toHaveBeenCalled()
expect(console.warn).toHaveBeenCalledWith(error)
})

it('logs non-UUID Prisma P2023 errors instead of hiding them as 404', async () => {
Expand All @@ -115,7 +112,7 @@ describe('withErrorHandler util', () => {
expect(console.error).toHaveBeenCalledWith(error)
})

it('maps raw query invalid UUID errors to 404 and warns instead of erroring', async () => {
it('maps raw query invalid UUID errors to 404 without logging', async () => {
const error = new PrismaClientKnownRequestError('Raw query failed', {
code: 'P2010',
clientVersion: '5.19.0',
Expand All @@ -130,7 +127,6 @@ describe('withErrorHandler util', () => {
expect(response.error).toBe('The requested resource was not found')
expect(nextResponse.status).toBe(httpStatus.NOT_FOUND)
expect(console.error).not.toHaveBeenCalled()
expect(console.warn).toHaveBeenCalledWith(error)
})

it('logs unclassified Prisma known request errors', async () => {
Expand Down
Loading