File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed
Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ import { Platform } from "react-native" ;
2+ import { ErrorInfo } from "react" ;
3+
4+ // The URL for your self-hosted error reporting endpoint.
5+ const ERROR_REPORTING_ENDPOINT = "https://new.codebuilder.org/api/errors" ;
6+
7+ interface ReportOptions {
8+ isFatal ?: boolean ;
9+ errorInfo ?: ErrorInfo ;
10+ }
11+
12+ interface ErrorReport {
13+ message : string ;
14+ stack ?: string ;
15+ timestamp : string ;
16+ platform : string ;
17+ options ?: ReportOptions ;
18+ }
19+
20+ /**
21+ * Sends an error report to your custom endpoint.
22+ * @param error The error object.
23+ * @param options An object containing additional context like isFatal or errorInfo.
24+ */
25+ export const reportError = async (
26+ error : Error ,
27+ options ?: ReportOptions
28+ ) : Promise < void > => {
29+ const report : ErrorReport = {
30+ message : error . message ,
31+ stack : error . stack ,
32+ timestamp : new Date ( ) . toISOString ( ) ,
33+ platform : Platform . OS ,
34+ options,
35+ } ;
36+
37+ try {
38+ const response = await fetch ( ERROR_REPORTING_ENDPOINT , {
39+ method : "POST" ,
40+ headers : {
41+ "Content-Type" : "application/json" ,
42+ } ,
43+ body : JSON . stringify ( report ) ,
44+ } ) ;
45+
46+ if ( ! response . ok ) {
47+ console . error ( "Failed to send error report:" , response . status ) ;
48+ }
49+ } catch ( e ) {
50+ console . error ( "Error sending error report:" , e ) ;
51+ }
52+ } ;
You can’t perform that action at this time.
0 commit comments