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!

πŸ₯… Catch + pass down any upstream/API errors

Alicia Sykes5fdf50ceb5f5
diff --git a/services/cors-proxy.js b/services/cors-proxy.js
index 8b0522c6..fedc18d8 100644
--- a/services/cors-proxy.js
+++ b/services/cors-proxy.js
@@ -124,7 +124,38 @@ const handler = (req, res) => {
};
request(requestConfig).then(
(response) => send(200, response.data),
- (error) => send(500, { error }),
+ (error) => {
+ // Handle unexpected or partial error objects
+ const err = (error && typeof error === 'object') ? error : {};
+ const upstream = (err.response && typeof err.response === 'object') ? err.response : null;
+ const upstreamStatus = upstream && typeof upstream.status === 'number' ? upstream.status : null;
+
+ // Make response code and error type
+ let proxyStatus;
+ let type;
+ if (upstreamStatus !== null && upstreamStatus >= 400) {
+ proxyStatus = upstreamStatus;
+ type = 'upstream_status';
+ } else if (err.timeout) {
+ proxyStatus = 504;
+ type = 'upstream_timeout';
+ } else {
+ proxyStatus = 502;
+ type = 'upstream_error';
+ }
+
+ send(proxyStatus, {
+ error: {
+ type,
+ name: err.name,
+ message: err.message,
+ code: err.code,
+ status: upstreamStatus,
+ statusText: upstream && upstream.statusText,
+ data: upstream && upstream.data,
+ },
+ });
+ },
);
};
diff --git a/services/request.js b/services/request.js
index 0e1d11a0..68a0c245 100644
--- a/services/request.js
+++ b/services/request.js
@@ -14,12 +14,15 @@ const zlib = require('zlib');
const { URL } = require('url');
class RequestError extends Error {
- constructor(message, { response, code, errno } = {}) {
+ constructor(message, {
+ response, code, errno, timeout,
+ } = {}) {
super(message);
this.name = 'RequestError';
this.response = response || undefined;
this.code = code || undefined;
this.errno = errno || undefined;
+ this.timeout = timeout === true ? true : undefined;
}
// Return a JSON-safe summary, to prevent the any circular references
@@ -29,6 +32,7 @@ class RequestError extends Error {
message: this.message,
code: this.code,
errno: this.errno,
+ timeout: this.timeout,
status: this.response && this.response.status,
statusText: this.response && this.response.statusText,
data: this.response && this.response.data,
@@ -208,7 +212,7 @@ function request(config) {
req.destroy();
reject(new RequestError(
`timeout of ${timeout}ms exceeded`,
- { code: 'ECONNABORTED' },
+ { code: 'ECONNABORTED', timeout: true },
));
});
}
diff --git a/tests/server/cors-proxy.test.js b/tests/server/cors-proxy.test.js
index 7682695a..19b772e4 100644
--- a/tests/server/cors-proxy.test.js
+++ b/tests/server/cors-proxy.test.js
@@ -1,4 +1,5 @@
// @vitest-environment node
+import http from 'http';
import {
describe, it, expect, afterEach, beforeAll, afterAll, vi,
} from 'vitest';
@@ -101,6 +102,88 @@ describe('CORS proxy', () => {
});
});
+// Spin up a controllable target server, then drive the proxy through real
+// failure paths (upstream 4xx/5xx, network refused, port unreachable). This
+// is the only way to verify the error-classification branches end-to-end β€”
+// vi.mock can't reach into a `require()` graph that's already been loaded
+// by services/app at the top of this file.
+describe('CORS proxy error classification', () => {
+ let target;
+ let targetUrl;
+ let handler = (req, res) => res.status(500).end();
+
+ beforeAll(() => new Promise((resolve) => {
+ target = http.createServer((req, res) => handler(req, res));
+ target.listen(0, '127.0.0.1', () => {
+ targetUrl = `http://127.0.0.1:${target.address().port}`;
+ resolve();
+ });
+ }));
+ afterAll(() => new Promise((resolve) => target.close(resolve)));
+
+ it('mirrors upstream 4xx status (so frontend can branch on err.response.status)', async () => {
+ handler = (req, res) => {
+ res.statusCode = 429;
+ res.statusMessage = 'Too Many Requests';
+ res.end('rate limited');
+ };
+ const res = await request(app).get('/cors-proxy').set('Target-URL', targetUrl);
+ expect(res.status).toBe(429);
+ expect(res.body.error.type).toBe('upstream_status');
+ expect(res.body.error.status).toBe(429);
+ expect(res.body.error.statusText).toBe('Too Many Requests');
+ });
+
+ it('mirrors upstream 5xx status', async () => {
+ handler = (req, res) => { res.statusCode = 503; res.end('busy'); };
+ const res = await request(app).get('/cors-proxy').set('Target-URL', targetUrl);
+ expect(res.status).toBe(503);
+ expect(res.body.error.type).toBe('upstream_status');
+ });
+
+ it('returns 502 + upstream_error when upstream refuses connection', async () => {
+ // Port 1 is reserved (tcpmux) β€” guaranteed nothing listening, ECONNREFUSED
+ const res = await request(app).get('/cors-proxy').set('Target-URL', 'http://127.0.0.1:1');
+ expect(res.status).toBe(502);
+ expect(res.body.error.type).toBe('upstream_error');
+ // Specific cause is still preserved in the body, just not in the type label
+ expect(res.body.error.code).toBe('ECONNREFUSED');
+ });
+
+ it('does not crash when target server hangs up mid-request', async () => {
+ handler = (req, res) => { res.socket.destroy(); };
+ const res = await request(app).get('/cors-proxy').set('Target-URL', targetUrl);
+ expect(res.status).toBe(502);
+ expect(res.body.error.type).toBe('upstream_error');
+ });
+
+});
+
+// Verify the request.js β†’ cors-proxy timeout contract end-to-end. The proxy
+// classifies via a `timeout: true` marker on the RequestError, not via the
+// 'ECONNABORTED' string (which collides with a real libuv errno).
+describe('request.js timeout marker', () => {
+ const request_ = require('../../services/request');
+
+ let target;
+ let targetUrl;
+ beforeAll(() => new Promise((resolve) => {
+ target = http.createServer(() => { /* never respond */ });
+ target.listen(0, '127.0.0.1', () => {
+ targetUrl = `http://127.0.0.1:${target.address().port}`;
+ resolve();
+ });
+ }));
+ afterAll(() => new Promise((resolve) => target.close(resolve)));
+
+ it('rejects with timeout:true (and ECONNABORTED preserved on .code for back-compat)', async () => {
+ await expect(request_({ url: targetUrl, timeout: 50 })).rejects.toMatchObject({
+ timeout: true,
+ code: 'ECONNABORTED',
+ });
+ });
+});
+
describe('CORS proxy env-var substitution', () => {
const SET_KEYS = [];
const setEnv = (k, v) => { SET_KEYS.push(k); process.env[k] = v; };