Skip to content

Error Tracking

The fastest way to capture errors you didn’t explicitly handle:

gun.attachGlobalErrorHandlers();

This attaches listeners for:

EnvironmentEvents
Browserwindow.error, window.unhandledrejection
Node.jsprocess.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.

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,
},
});
}

Catch HTTP errors globally:

// Axios
axios.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 wrapper
async 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;
}
}

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
},
});