Skip to content

armgit / dashy

Public Imported from GitHub
0

πŸš€ A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!

πŸ₯… Gracefully catch/parse widget fetch errors

Alicia Sykesb33d3d2c447e
diff --git a/src/mixins/WidgetMixin.js b/src/mixins/WidgetMixin.js
index 959ec16b..4346c967 100644
--- a/src/mixins/WidgetMixin.js
+++ b/src/mixins/WidgetMixin.js
@@ -43,6 +43,9 @@ const WidgetMixin = {
useProxy() {
return this.options.useProxy || this.overrideProxyChoice;
},
+ requestTimeout() {
+ return this.options.timeout || this.defaultTimeout;
+ },
/* Returns either a number in ms to continuously update widget data. Or 0 for no updates */
updateInterval() {
const usersInterval = this.options.updateInterval;
@@ -109,9 +112,8 @@ const WidgetMixin = {
const CustomHeaders = options || null;
const headers = this.useProxy
? { 'Target-URL': endpoint, CustomHeaders: JSON.stringify(CustomHeaders) } : CustomHeaders;
- const timeout = this.options.timeout || this.defaultTimeout;
const requestConfig = {
- method, url, headers, data, timeout,
+ method, url, headers, data, timeout: this.requestTimeout,
};
// Make request
return new Promise((resolve, reject) => {
@@ -123,7 +125,8 @@ const WidgetMixin = {
resolve(response.data);
})
.catch((dataFetchError) => {
- this.error('Unable to fetch data', dataFetchError);
+ const errorMessage = this.formatRequestError(dataFetchError);
+ this.error(errorMessage, dataFetchError);
reject(dataFetchError);
})
.finally(() => {
@@ -131,6 +134,34 @@ const WidgetMixin = {
});
});
},
+ /* Get user-facing error message from certain failed request types */
+ formatRequestError(err) {
+ if (!err) return 'Unable to fetch data';
+ // Client-side timeout: request never produced a response
+ if (err.timeout && !err.response) {
+ return `Request timed out after ${this.requestTimeout}ms. `
+ + 'Check the target is reachable, or increase the widget\'s `timeout` option';
+ }
+ // Errors surfaced by the cors-proxy carry classification + upstream detail
+ const upstream = err.response && err.response.data && err.response.data.error;
+ if (upstream) {
+ if (upstream.timeout) {
+ return 'Upstream server timed out. The target API is slow or unreachable.';
+ }
+ if (upstream.type === 'upstream_status' && upstream.status) {
+ const tail = upstream.statusText ? ` ${upstream.statusText}` : '';
+ return `Upstream returned ${upstream.status}${tail}.`;
+ }
+ if (upstream.type === 'upstream_error') {
+ return `Could not reach target server${upstream.code ? ` (${upstream.code})` : ''}.`;
+ }
+ }
+ // Likely CORS error, need useProxy
+ if (!err.response && !this.useProxy) {
+ return 'Failed to reach target from this context. Possibly a CORS error.';
+ }
+ return 'Unable to fetch data';
+ },
/* If the string is a build-time env-var placeholder, return its value
* Otherwise, will pass it through to the proxy for it to resolve server-side */
parseAsEnvVar(str) {
diff --git a/src/utils/request.js b/src/utils/request.js
index d616f6d6..550c8380 100644
--- a/src/utils/request.js
+++ b/src/utils/request.js
@@ -25,6 +25,7 @@ class RequestError extends Error {
this.response = opts.response || undefined;
this.request = opts.request || undefined;
this.code = opts.code || undefined;
+ this.timeout = opts.timeout === true ? true : undefined;
}
}
@@ -113,13 +114,11 @@ async function makeRequest(config) {
} catch (err) {
if (err instanceof RequestError) throw err;
// Network error or abort/timeout
- // Set request: true so callers can distinguish "no response" from other errors
- // (mirrors axios behavior where error.request is set when no response received)
- const error = new RequestError(err.message, { request: true });
- if (err.name === 'AbortError') {
- error.message = `timeout of ${timeout}ms exceeded`;
- error.code = 'ECONNABORTED';
- }
+ const isTimeout = err.name === 'AbortError';
+ const error = new RequestError(
+ isTimeout ? `timeout of ${timeout}ms exceeded` : err.message,
+ { request: true, code: isTimeout ? 'ECONNABORTED' : undefined, timeout: isTimeout },
+ );
throw error;
} finally {
if (timeoutId) clearTimeout(timeoutId);