Error Tracking
Global error handlers
Section titled “Global error handlers”The fastest way to capture errors you didn’t explicitly handle:
gun.attachGlobalErrorHandlers();This attaches listeners for:
| Environment | Events |
|---|---|
| Browser | window.error, window.unhandledrejection |
| Node.js | process.uncaughtException, process.unhandledRejection |
Captured errors are logged at error level in the global_errors bucket with the error message and stack trace in the context.
Call gun.detachGlobalErrorHandlers() to remove the listeners, or gun.destroy() to clean up everything.
Manual error logging
Section titled “Manual error logging”For errors you catch yourself:
try { await riskyOperation();} catch (err) { gun.error({ bucket: "api", message: `Operation failed: ${err.message}`, context: { error: err.message, stack: err.stack, code: err.code, }, tags: { operation: "riskyOperation", errorType: err.constructor.name, }, });}Axios / fetch interceptors
Section titled “Axios / fetch interceptors”Catch HTTP errors globally:
// Axiosaxios.interceptors.response.use( (response) => response, (error) => { gun.error({ bucket: "http", message: `HTTP ${error.response?.status || "network"}: ${error.config.url}`, context: { url: error.config.url, method: error.config.method, status: error.response?.status, data: error.response?.data, }, tags: { status: String(error.response?.status || "network_error"), method: error.config.method.toUpperCase(), }, }); return Promise.reject(error); });// Fetch wrapperasync function fetchWithLogging(url, options = {}) { try { const res = await fetch(url, options); if (!res.ok) { gun.error({ bucket: "http", message: `HTTP ${res.status}: ${url}`, context: { status: res.status, statusText: res.statusText }, tags: { status: String(res.status), method: options.method || "GET" }, }); } return res; } catch (err) { gun.error({ bucket: "http", message: `Network error: ${url}`, context: { error: err.message }, tags: { status: "network_error", method: options.method || "GET" }, }); throw err; }}Pattern: error tags
Section titled “Pattern: error tags”Use consistent tags for errors so you can filter by error type in the desktop app:
gun.error({ bucket: "api", message: "Validation failed", tags: { errorType: "validation", // validation, auth, network, timeout, unknown severity: "low", // low, medium, high, critical recoverable: "true", // true, false },});