Skip to content

Commit 34e2af1

Browse files
committed
Added errror reporting library.
1 parent 9e241ac commit 34e2af1

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/lib/errorReportingService.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { ErrorInfo } from 'react'
2+
3+
// This is the relative path to the API endpoint we created.
4+
const ERROR_REPORTING_ENDPOINT = '/api/errors'
5+
6+
interface ReportOptions {
7+
isFatal?: boolean
8+
errorInfo?: ErrorInfo
9+
// You can add more web-specific context here if needed
10+
// e.g., userAgent, screenResolution, etc.
11+
}
12+
13+
/**
14+
* Sends a client-side error report to the Next.js API endpoint.
15+
* @param error The error object.
16+
* @param options An object containing additional context.
17+
*/
18+
export const reportError = async (error: Error, options?: ReportOptions): Promise<void> => {
19+
const report = {
20+
message: error.message,
21+
stack: error.stack,
22+
platform: 'web', // Identify that the error came from the web client
23+
options,
24+
}
25+
26+
try {
27+
await fetch(ERROR_REPORTING_ENDPOINT, {
28+
method: 'POST',
29+
headers: {
30+
'Content-Type': 'application/json',
31+
},
32+
body: JSON.stringify(report),
33+
})
34+
} catch (e) {
35+
console.error('Failed to send error report:', e)
36+
}
37+
}

0 commit comments

Comments
 (0)