forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorReporting.js
More file actions
138 lines (125 loc) · 3.21 KB
/
Copy patherrorReporting.js
File metadata and controls
138 lines (125 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import * as Sentry from '@sentry/nextjs';
/**
* Report an error to Sentry with additional context
* @param {Error} error - The error to report
* @param {Object} context - Additional context to include with the error
* @param {string} level - Error level: 'error', 'warning', 'info', 'debug'
*/
export function reportError(error, context = {}, level = 'error') {
if (process.env.NODE_ENV === 'development') {
console.error('Error:', error, 'Context:', context);
}
Sentry.withScope((scope) => {
// Add context as extras
if (context) {
Object.keys(context).forEach((key) => {
scope.setExtra(key, context[key]);
});
}
// Set level
scope.setLevel(level);
// Capture the exception
Sentry.captureException(error);
});
}
/**
* Report a message to Sentry
* @param {string} message - The message to report
* @param {Object} context - Additional context
* @param {string} level - Message level
*/
export function reportMessage(message, context = {}, level = 'info') {
if (process.env.NODE_ENV === 'development') {
console.log('Message:', message, 'Context:', context);
}
Sentry.withScope((scope) => {
if (context) {
Object.keys(context).forEach((key) => {
scope.setExtra(key, context[key]);
});
}
scope.setLevel(level);
Sentry.captureMessage(message, level);
});
}
/**
* Set user context for error reporting
* @param {Object} user - User information
*/
export function setUserContext(user) {
if (user) {
Sentry.setUser({
id: user.id,
email: user.email,
username: user.username,
publicKey: user.publicKey,
});
} else {
Sentry.setUser(null);
}
}
/**
* Add breadcrumb for debugging
* @param {string} message - Breadcrumb message
* @param {Object} data - Additional data
* @param {string} category - Breadcrumb category
* @param {string} level - Breadcrumb level
*/
export function addBreadcrumb(message, data = {}, category = 'custom', level = 'info') {
Sentry.addBreadcrumb({
message,
category,
level,
data,
});
}
/**
* Wrap an async function with error reporting
* @param {Function} fn - The async function to wrap
* @param {Object} context - Additional context for error reporting
*/
export function withErrorReporting(fn, context = {}) {
return async (...args) => {
try {
return await fn(...args);
} catch (error) {
reportError(error, {
...context,
functionName: fn.name,
arguments: args,
});
throw error;
}
};
}
/**
* Report wallet connection errors
*/
export function reportWalletError(error, walletType = 'unknown') {
reportError(error, {
errorType: 'wallet_connection',
walletType,
}, 'warning');
}
/**
* Report API errors
*/
export function reportApiError(error, endpoint, method = 'GET') {
reportError(error, {
errorType: 'api_error',
endpoint,
method,
statusCode: error.response?.status,
responseData: error.response?.data,
});
}
/**
* Report blockchain transaction errors
*/
export function reportTransactionError(error, transactionType, transactionData = {}) {
reportError(error, {
errorType: 'transaction_error',
transactionType,
...transactionData,
});
}