Skip to content
Draft
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: 4 additions & 1 deletion src/app/api/core/utils/withErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export const withErrorHandler = (handler: RequestHandler): RequestHandler => {
if (error instanceof ZodError) {
formattedError = error.format() as ZodFormattedError<string>
}
console.error(formattedError)

// Default staus and message for JSON error response
let status: number = (error as StatusableError).status || httpStatus.BAD_REQUEST
Expand Down Expand Up @@ -65,6 +64,10 @@ export const withErrorHandler = (handler: RequestHandler): RequestHandler => {
}
}

if (status >= httpStatus.INTERNAL_SERVER_ERROR) {
console.error(formattedError)
}

return NextResponse.json({ error: message, errors }, { status })
}
}
Expand Down
46 changes: 44 additions & 2 deletions src/app/api/tests/utils/withErrorHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ jest.mock('@/utils/CopilotAPI', () => ({

describe('withErrorHandler util', () => {
let req: NextRequest
let consoleErrorSpy: jest.SpyInstance

beforeEach(() => {
jest.clearAllMocks()
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => undefined)
req = buildNextRequest(`/?token=iu-token`)
})

afterEach(() => {
consoleErrorSpy.mockRestore()
})

it('catches and builds proper response for APIError', async () => {
const handler = async (_req: NextRequest, _params: any) => {
throw new APIError(httpStatus.UNAUTHORIZED, 'Please provide a valid token')
Expand All @@ -28,6 +34,7 @@ describe('withErrorHandler util', () => {
const response = await nextResponse.json()
expect(response.error).toBe('Please provide a valid token')
expect(nextResponse.status).toBe(httpStatus.UNAUTHORIZED)
expect(consoleErrorSpy).not.toHaveBeenCalled()
})

it('catches and builds proper response for ZodError', async () => {
Expand All @@ -38,9 +45,9 @@ describe('withErrorHandler util', () => {

const nextResponse = await withErrorHandler(handler)(req, null)
const response = await nextResponse.json()
expect(response.error[0].expected).toBe('string')
expect(response.error[0].received).toBe('number')
expect(response.error).toBe('Expected string, received number')
expect(nextResponse.status).toBe(httpStatus.UNPROCESSABLE_ENTITY)
expect(consoleErrorSpy).not.toHaveBeenCalled()
})

it('catches and builds proper response for CopilotApiError', async () => {
Expand All @@ -52,6 +59,40 @@ describe('withErrorHandler util', () => {
const response = await nextResponse.json()
expect(response.error).toBe('Please provide a valid token')
expect(nextResponse.status).toBe(httpStatus.UNAUTHORIZED)
expect(consoleErrorSpy).not.toHaveBeenCalled()
})

it('catches and builds proper response for Copilot SDK style ApiError', async () => {
const error = new Error('Not Found') as Error & {
status: number
body: { message: string }
}
error.name = 'ApiError'
error.status = httpStatus.NOT_FOUND
error.body = { message: 'Not Found' }

const handler = async (_req: NextRequest, _params: any) => {
throw error
}

const nextResponse = await withErrorHandler(handler)(req, null)
const response = await nextResponse.json()
expect(response.error).toBe('Not Found')
expect(nextResponse.status).toBe(httpStatus.NOT_FOUND)
expect(consoleErrorSpy).not.toHaveBeenCalled()
})

it('logs server errors while returning the standardized response', async () => {
const error = new APIError(httpStatus.INTERNAL_SERVER_ERROR, 'Unexpected failure')
const handler = async (_req: NextRequest, _params: any) => {
throw error
}

const nextResponse = await withErrorHandler(handler)(req, null)
const response = await nextResponse.json()
expect(response.error).toBe('Unexpected failure')
expect(nextResponse.status).toBe(httpStatus.INTERNAL_SERVER_ERROR)
expect(consoleErrorSpy).toHaveBeenCalledWith(error)
})

it('returns proper response if no errors are encountered', async () => {
Expand All @@ -63,5 +104,6 @@ describe('withErrorHandler util', () => {
const response = await nextResponse.json()
expect(response.message).toBe('Yay!')
expect(nextResponse.status).toBe(httpStatus.OK)
expect(consoleErrorSpy).not.toHaveBeenCalled()
})
})
Loading