-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add middleware handler for error code telemetry
- Loading branch information
Showing
6 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
...src/client/components/react-dev-overlay/server/get-next-error-feedback-middleware.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { getNextErrorFeedbackMiddleware } from './get-next-error-feedback-middleware' | ||
import type { IncomingMessage, ServerResponse } from 'http' | ||
|
||
describe('getNextErrorFeedbackMiddleware', () => { | ||
const mockTelemetry = { | ||
record: jest.fn().mockResolvedValue({}), | ||
} as any | ||
|
||
let mockReq: Partial<IncomingMessage> | ||
let mockRes: Partial<ServerResponse> | ||
let mockNext: jest.Mock | ||
|
||
beforeEach(() => { | ||
mockReq = { | ||
url: '/__nextjs_error_feedback?errorCode=TEST_ERROR&wasHelpful=true', | ||
} | ||
mockRes = { | ||
setHeader: jest.fn(), | ||
end: jest.fn(), | ||
} | ||
mockNext = jest.fn() | ||
jest.clearAllMocks() | ||
}) | ||
|
||
it('calls next() if path does not match', async () => { | ||
mockReq.url = '/some-other-path' | ||
|
||
await getNextErrorFeedbackMiddleware(mockTelemetry)( | ||
mockReq as IncomingMessage, | ||
mockRes as ServerResponse, | ||
mockNext | ||
) | ||
|
||
expect(mockNext).toHaveBeenCalled() | ||
expect(mockTelemetry.record).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('records telemetry when feedback is submitted', async () => { | ||
await getNextErrorFeedbackMiddleware(mockTelemetry)( | ||
mockReq as IncomingMessage, | ||
mockRes as ServerResponse, | ||
mockNext | ||
) | ||
|
||
expect(mockTelemetry.record).toHaveBeenCalledWith({ | ||
eventName: 'NEXT_ERROR_FEEDBACK', | ||
payload: { | ||
errorCode: 'TEST_ERROR', | ||
wasHelpful: true, | ||
}, | ||
}) | ||
expect(mockRes.statusCode).toBe(204) | ||
}) | ||
|
||
it('returns 400 if params are missing', async () => { | ||
mockReq.url = '/__nextjs_error_feedback' | ||
|
||
await getNextErrorFeedbackMiddleware(mockTelemetry)( | ||
mockReq as IncomingMessage, | ||
mockRes as ServerResponse, | ||
mockNext | ||
) | ||
|
||
expect(mockRes.statusCode).toBe(400) | ||
expect(mockTelemetry.record).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('returns 500 if telemetry recording fails', async () => { | ||
mockTelemetry.record.mockRejectedValueOnce(new Error('Failed to record')) | ||
|
||
await getNextErrorFeedbackMiddleware(mockTelemetry)( | ||
mockReq as IncomingMessage, | ||
mockRes as ServerResponse, | ||
mockNext | ||
) | ||
|
||
expect(mockRes.statusCode).toBe(500) | ||
}) | ||
}) |
40 changes: 40 additions & 0 deletions
40
...next/src/client/components/react-dev-overlay/server/get-next-error-feedback-middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { eventErrorFeedback } from '../../../../telemetry/events/error-feedback' | ||
import { badRequest, internalServerError, noContent } from './shared' | ||
|
||
import type { ServerResponse, IncomingMessage } from 'http' | ||
import type { Telemetry } from '../../../../telemetry/storage' | ||
|
||
// Handles HTTP requests to /__nextjs_error_feedback endpoint for collecting user feedback on error messages | ||
export function getNextErrorFeedbackMiddleware(telemetry: Telemetry) { | ||
return async function ( | ||
req: IncomingMessage, | ||
res: ServerResponse, | ||
next: () => void | ||
): Promise<void> { | ||
const { pathname, searchParams } = new URL(`http://n${req.url}`) | ||
|
||
if (pathname !== '/__nextjs_error_feedback') { | ||
return next() | ||
} | ||
|
||
try { | ||
const errorCode = searchParams.get('errorCode') | ||
const wasHelpful = searchParams.get('wasHelpful') | ||
|
||
if (!errorCode || !wasHelpful) { | ||
return badRequest(res) | ||
} | ||
|
||
await telemetry.record( | ||
eventErrorFeedback({ | ||
errorCode, | ||
wasHelpful: wasHelpful === 'true', | ||
}) | ||
) | ||
|
||
return noContent(res) | ||
} catch (error) { | ||
return internalServerError(res) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export const eventNameErrorFeedback = 'NEXT_ERROR_FEEDBACK' | ||
|
||
export type EventErrorFeedback = { | ||
errorCode: string | ||
wasHelpful: boolean | ||
} | ||
|
||
/** | ||
* Records telemetry for error feedback. | ||
* | ||
* @example | ||
* ```ts | ||
* telemetry.record(eventErrorFeedback({ | ||
* errorCode: 'E1', | ||
* wasHelpful: true | ||
* })) | ||
* ``` | ||
*/ | ||
export function eventErrorFeedback(event: EventErrorFeedback): { | ||
eventName: string | ||
payload: EventErrorFeedback | ||
} { | ||
return { | ||
eventName: eventNameErrorFeedback, | ||
payload: event, | ||
} | ||
} |