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!

πŸ₯… Properly catch/handle server errs, without circular crash

Alicia Sykesf1f5718dd29a
diff --git a/services/cors-proxy.js b/services/cors-proxy.js
index 502f0fa7..8d9538aa 100644
--- a/services/cors-proxy.js
+++ b/services/cors-proxy.js
@@ -37,20 +37,11 @@ module.exports = (req, res) => {
headers,
};
- // Extract a JSON-safe summary from an axios-like error (get circular dep without this!)
- const serializeError = (error) => ({
- message: error.message,
- code: error.code,
- status: error.response && error.response.status,
- statusText: error.response && error.response.statusText,
- data: error.response && error.response.data,
- });
-
- // Make the request, and respond with result
+ // Make the request, and respond with result.
request(requestConfig)
.then((response) => {
res.status(200).send(response.data);
}).catch((error) => {
- res.status(500).send({ error: serializeError(error) });
+ res.status(500).send({ error });
});
};
diff --git a/services/request.js b/services/request.js
index 6be2341e..90ff8254 100644
--- a/services/request.js
+++ b/services/request.js
@@ -21,6 +21,19 @@ class RequestError extends Error {
this.code = code || undefined;
this.errno = errno || undefined;
}
+
+ // Return a JSON-safe summary, to prevent the any circular references
+ toJSON() {
+ return {
+ name: this.name,
+ message: this.message,
+ code: this.code,
+ errno: this.errno,
+ status: this.response && this.response.status,
+ statusText: this.response && this.response.statusText,
+ data: this.response && this.response.data,
+ };
+ }
}
/**
@@ -135,9 +148,15 @@ function request(config) {
status: res.statusCode,
statusText: res.statusMessage,
headers: res.headers,
- // Expose the raw request object for socket access (status-check.js needs this)
- request: req,
};
+ // Expose the raw request object for socket access (status-check.js
+ // needs this). Defined as non-enumerable so JSON.stringify() skips
+ // it β€” the http.ClientRequest has circular socket references that
+ // would otherwise crash any endpoint forwarding the response.
+ Object.defineProperty(response, 'request', {
+ value: req,
+ enumerable: false,
+ });
if (res.statusCode >= 200 && res.statusCode < 300) {
resolve(response);