Skip to content

Commit 2dccf39

Browse files
committed
fix(errors): catch fetch exceptions.
We need to handle these more gracefully but for now this gets the site up and operational again.
1 parent d2bb54d commit 2dccf39

File tree

1 file changed

+51
-41
lines changed

1 file changed

+51
-41
lines changed

routes/index.js

+51-41
Original file line numberDiff line numberDiff line change
@@ -74,27 +74,32 @@ async function createResoniteApiError(res, type) {
7474
* @returns
7575
*/
7676
async function handle(type, req, res, next) {
77-
var apiResponse = await fetch(getUrl(type, req));
78-
if (!apiResponse.ok) {
79-
var error = await createResoniteApiError(apiResponse, type);
80-
return next(error);
77+
try {
78+
var apiResponse = await fetch(getUrl(type, req));
79+
if (!apiResponse.ok) {
80+
var error = await createResoniteApiError(apiResponse, type);
81+
return next(error);
82+
}
83+
84+
var json = await apiResponse.json();
85+
86+
if (type ==="world" && json.recordType !== "world") {
87+
return next(createError(400, "go.resonite.com only works for Session and world link."));
88+
}
89+
90+
if (type === "sessionList"){
91+
json.title = getOpenGraphTitle(type);
92+
}
93+
94+
json = preProcess(json, type);
95+
json = addMetadata(type,json);
96+
json.urlPath = req.getUrl();
97+
98+
res.status(200).render(type, json);
99+
} catch (error) {
100+
console.log(error);
101+
return next(createError(503, "Unable to connect to Resonite API, please try again soon."));
81102
}
82-
83-
var json = await apiResponse.json();
84-
85-
if (type ==="world" && json.recordType !== "world") {
86-
return next(createError(400, "go.resonite.com only works for Session and world link."));
87-
}
88-
89-
if (type === "sessionList"){
90-
json.title = getOpenGraphTitle(type);
91-
}
92-
93-
json = preProcess(json, type);
94-
json = addMetadata(type,json);
95-
json.urlPath = req.getUrl();
96-
97-
res.status(200).render(type, json);
98103
}
99104

100105
function addMetadata(pageType, json) {
@@ -114,29 +119,34 @@ function addMetadata(pageType, json) {
114119
* @returns
115120
*/
116121
async function handleJson(type, req, res, next) {
117-
var apiResponse = await fetch(getUrl(type, req));
118-
if (!apiResponse.ok) {
119-
res.status(apiResponse.status);
120-
return next();
121-
}
122-
123-
var json = await apiResponse.json();
124122

125-
if (type ==="world" && json.recordType !== "world") {
126-
res.status(400);
127-
return next();
123+
try {
124+
var apiResponse = await fetch(getUrl(type, req));
125+
if (!apiResponse.ok) {
126+
res.status(apiResponse.status);
127+
return next();
128+
}
129+
130+
var json = await apiResponse.json();
131+
132+
if (type ==="world" && json.recordType !== "world") {
133+
res.status(400);
134+
return next();
135+
}
136+
137+
json = preProcess(json, type);
138+
var title = getOpenGraphTitle(type);
139+
res.json({
140+
title: title,
141+
author_name: title,
142+
author_url: req.getUrl().replace("/json",""),
143+
provider_name: "Resonite",
144+
provider_url: "https://resonite.com",
145+
});
146+
} catch (error) {
147+
console.log(error);
148+
return next(createError(503, "Unable to connect to Resonite API, please try again soon."));
128149
}
129-
130-
json = preProcess(json, type);
131-
// title is the TOP link
132-
var title = getOpenGraphTitle(type);
133-
res.json({
134-
title: title,
135-
author_name: title,
136-
author_url: req.getUrl().replace("/json",""),
137-
provider_name: "Resonite",
138-
provider_url: "https://resonite.com",
139-
});
140150
}
141151

142152
/**

0 commit comments

Comments
 (0)