@@ -5,6 +5,7 @@ import APIError from '@api/core/exceptions/api'
55import { NextRequest , NextResponse } from 'next/server'
66import { withErrorHandler } from '@api/core/utils/withErrorHandler'
77import { CopilotApiError } from '@/types/CopilotApiError'
8+ import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library'
89import { z } from 'zod'
910
1011jest . mock ( '@/utils/CopilotAPI' , ( ) => ( {
@@ -13,10 +14,21 @@ jest.mock('@/utils/CopilotAPI', () => ({
1314
1415describe ( 'withErrorHandler util' , ( ) => {
1516 let req : NextRequest
17+ const buildPrismaError = ( code : string ) =>
18+ new PrismaClientKnownRequestError ( 'Prisma known request error' , {
19+ code,
20+ clientVersion : 'test' ,
21+ meta : { } ,
22+ } )
1623
1724 beforeEach ( ( ) => {
1825 jest . clearAllMocks ( )
1926 req = buildNextRequest ( `/?token=iu-token` )
27+ jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => undefined )
28+ } )
29+
30+ afterEach ( ( ) => {
31+ jest . restoreAllMocks ( )
2032 } )
2133
2234 it ( 'catches and builds proper response for APIError' , async ( ) => {
@@ -28,6 +40,7 @@ describe('withErrorHandler util', () => {
2840 const response = await nextResponse . json ( )
2941 expect ( response . error ) . toBe ( 'Please provide a valid token' )
3042 expect ( nextResponse . status ) . toBe ( httpStatus . UNAUTHORIZED )
43+ expect ( console . error ) . not . toHaveBeenCalled ( )
3144 } )
3245
3346 it ( 'catches and builds proper response for ZodError' , async ( ) => {
@@ -38,9 +51,9 @@ describe('withErrorHandler util', () => {
3851
3952 const nextResponse = await withErrorHandler ( handler ) ( req , null )
4053 const response = await nextResponse . json ( )
41- expect ( response . error [ 0 ] . expected ) . toBe ( 'string' )
42- expect ( response . error [ 0 ] . received ) . toBe ( 'number' )
54+ expect ( response . error ) . toBe ( 'Expected string, received number' )
4355 expect ( nextResponse . status ) . toBe ( httpStatus . UNPROCESSABLE_ENTITY )
56+ expect ( console . error ) . not . toHaveBeenCalled ( )
4457 } )
4558
4659 it ( 'catches and builds proper response for CopilotApiError' , async ( ) => {
@@ -52,6 +65,57 @@ describe('withErrorHandler util', () => {
5265 const response = await nextResponse . json ( )
5366 expect ( response . error ) . toBe ( 'Please provide a valid token' )
5467 expect ( nextResponse . status ) . toBe ( httpStatus . UNAUTHORIZED )
68+ expect ( console . error ) . not . toHaveBeenCalled ( )
69+ } )
70+
71+ it ( 'catches and builds proper response for Prisma not found errors without logging' , async ( ) => {
72+ const handler = async ( _req : NextRequest , _params : any ) => {
73+ throw buildPrismaError ( 'P2025' )
74+ }
75+
76+ const nextResponse = await withErrorHandler ( handler ) ( req , null )
77+ const response = await nextResponse . json ( )
78+ expect ( response . error ) . toBe ( 'The requested resource was not found' )
79+ expect ( nextResponse . status ) . toBe ( httpStatus . NOT_FOUND )
80+ expect ( console . error ) . not . toHaveBeenCalled ( )
81+ } )
82+
83+ it ( 'catches and builds proper response for invalid Prisma UUID errors without logging' , async ( ) => {
84+ const handler = async ( _req : NextRequest , _params : any ) => {
85+ throw buildPrismaError ( 'P2023' )
86+ }
87+
88+ const nextResponse = await withErrorHandler ( handler ) ( req , null )
89+ const response = await nextResponse . json ( )
90+ expect ( response . error ) . toBe ( 'The requested resource was not found' )
91+ expect ( nextResponse . status ) . toBe ( httpStatus . NOT_FOUND )
92+ expect ( console . error ) . not . toHaveBeenCalled ( )
93+ } )
94+
95+ it ( 'logs unclassified Prisma known request errors' , async ( ) => {
96+ const error = buildPrismaError ( 'P2002' )
97+ const handler = async ( _req : NextRequest , _params : any ) => {
98+ throw error
99+ }
100+
101+ const nextResponse = await withErrorHandler ( handler ) ( req , null )
102+ const response = await nextResponse . json ( )
103+ expect ( response . error ) . toBe ( 'Something went wrong' )
104+ expect ( nextResponse . status ) . toBe ( httpStatus . BAD_REQUEST )
105+ expect ( console . error ) . toHaveBeenCalledWith ( error )
106+ } )
107+
108+ it ( 'logs unexpected errors' , async ( ) => {
109+ const error = new Error ( 'boom' )
110+ const handler = async ( _req : NextRequest , _params : any ) => {
111+ throw error
112+ }
113+
114+ const nextResponse = await withErrorHandler ( handler ) ( req , null )
115+ const response = await nextResponse . json ( )
116+ expect ( response . error ) . toBe ( 'Something went wrong' )
117+ expect ( nextResponse . status ) . toBe ( httpStatus . BAD_REQUEST )
118+ expect ( console . error ) . toHaveBeenCalledWith ( error )
55119 } )
56120
57121 it ( 'returns proper response if no errors are encountered' , async ( ) => {
0 commit comments