add error checking
All checks were successful
Publish Docker Images / docker (push) Successful in 1m16s
All checks were successful
Publish Docker Images / docker (push) Successful in 1m16s
This commit is contained in:
parent
c9ec7f5912
commit
4402b42756
@ -27,6 +27,7 @@ export default class ProxyRoute extends Route {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// TODO: handle rate limiting? and/or caching?
|
// TODO: handle rate limiting? and/or caching?
|
||||||
|
try {
|
||||||
const response = await axios.get(url, {
|
const response = await axios.get(url, {
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@ -59,5 +60,10 @@ export default class ProxyRoute extends Route {
|
|||||||
|
|
||||||
// Return the JSON response
|
// Return the JSON response
|
||||||
res.status(response.status).set(headers).json(data);
|
res.status(response.status).set(headers).json(data);
|
||||||
|
} catch (ex) {
|
||||||
|
res
|
||||||
|
.status(500)
|
||||||
|
.json(RouteMessages.internalServerError("Error fetching URL"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@ import { Request, Response } from "express";
|
|||||||
import { Route, RouteMessages } from "server";
|
import { Route, RouteMessages } from "server";
|
||||||
import { nodeManager } from "..";
|
import { nodeManager } from "..";
|
||||||
|
|
||||||
|
const IGNORED_PATHS = ["/favicon.ico"];
|
||||||
|
|
||||||
export default class ProxyRoute extends Route {
|
export default class ProxyRoute extends Route {
|
||||||
constructor() {
|
constructor() {
|
||||||
super({ path: "/" });
|
super({ path: "/" });
|
||||||
@ -13,6 +15,10 @@ export default class ProxyRoute extends Route {
|
|||||||
res.status(400).json(RouteMessages.badRequest("No URL provided"));
|
res.status(400).json(RouteMessages.badRequest("No URL provided"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (IGNORED_PATHS.includes(url)) {
|
||||||
|
res.status(404).send("Not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const node = nodeManager.getRandomNode();
|
const node = nodeManager.getRandomNode();
|
||||||
if (!node) {
|
if (!node) {
|
||||||
@ -22,10 +28,19 @@ export default class ProxyRoute extends Route {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
const response = await node.fetch(url);
|
const response = await node.fetch(url);
|
||||||
const data = response.data;
|
const data = response.data;
|
||||||
|
|
||||||
|
if (response.status === 500) {
|
||||||
|
res.status(500).json(RouteMessages.internalServerError(data));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Return the JSON response
|
// Return the JSON response
|
||||||
res.status(response.status).set(response.headers).json(data);
|
res.status(response.status).set(response.headers).json(data);
|
||||||
|
} catch (ex: any) {
|
||||||
|
res.status(500).json(RouteMessages.internalServerError(ex.message || ex));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user