Skip to content

Commit f5a04cb

Browse files
committed
Better error handeling
1 parent 1e58696 commit f5a04cb

3 files changed

Lines changed: 71 additions & 24 deletions

File tree

APIServer/public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ <h2>Execute a term</h2>
172172
<use href="svg.svg#execute"></use>
173173
</svg>
174174
</button>
175-
<pre id="doReductionOutput"></pre>
175+
<output id="doReductionOutput"></output>
176176
</form>
177177
<form
178178
id="createLanguage"

APIServer/public/js/GraphRedex.ts

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,10 @@ export default class GraphRedex<N extends GRND, E extends GRED> {
538538
return true;
539539
}
540540
} else {
541-
if (data.every((x) => typeof x === "object" && "_id" in x)) {
541+
if (
542+
data.length > 0 &&
543+
data.every((x) => typeof x === "object" && "_id" in x)
544+
) {
542545
if (
543546
confirm(
544547
"Returned data does not conain any edges. Make a highlight instead?",
@@ -547,7 +550,11 @@ export default class GraphRedex<N extends GRND, E extends GRED> {
547550
return this.highlightIfGraph(data);
548551
}
549552
} else {
550-
throw "Rendering nodes is not implemented yet";
553+
if (data.length == 0) {
554+
throw "result is empty ";
555+
} else {
556+
throw "Rendering nodes is not implemented yet";
557+
}
551558
}
552559
}
553560
return false;
@@ -781,10 +788,7 @@ export default class GraphRedex<N extends GRND, E extends GRED> {
781788
this.updateLangs();
782789
form.classed("closed", true);
783790
})
784-
.catch((e) => {
785-
console.log("ERROR", e);
786-
alert("something went wrong\n\n" + e);
787-
});
791+
.catch(errorAlert);
788792

789793
return false;
790794
});
@@ -812,17 +816,22 @@ export default class GraphRedex<N extends GRND, E extends GRED> {
812816
output.textContent = "success";
813817
form.classed("closed", true);
814818
})
815-
.catch((e) => {
819+
.catch((data) => {
820+
console.error([data]);
816821
submitBtn.attr("disabled", null);
817-
output.textContent = JSON.stringify(e, null, 2);
818-
if ("e" in data) {
819-
output.textContent = data.e;
820-
821-
if ("errors" in data) {
822-
const errEl = document.createElement("pre");
823-
errEl.textContent = data.errors;
824-
output.appendChild(errEl);
822+
if (typeof data == "object") {
823+
output.textContent = JSON.stringify(data, null, 2);
824+
if ("err" in data) {
825+
output.textContent = data.err;
826+
827+
if ("errors" in data) {
828+
const errEl = document.createElement("pre");
829+
errEl.textContent = data.errors;
830+
output.appendChild(errEl);
831+
}
825832
}
833+
} else {
834+
output.textContent = data;
826835
}
827836
});
828837
});
@@ -874,10 +883,14 @@ export default class GraphRedex<N extends GRND, E extends GRED> {
874883
}
875884
})
876885
.catch((error) => {
877-
if (typeof error === "string" && error[0] === "<") {
878-
output.innerHTML = error;
886+
if (typeof error === "object") {
887+
output.textContent = error.err ?? JSON.stringify(error);
879888
} else {
880-
output.textContent = "ERROR:" + error;
889+
if (typeof error === "string" && error[0] === "<") {
890+
output.innerHTML = error;
891+
} else {
892+
output.textContent = "ERROR:" + error;
893+
}
881894
}
882895
});
883896
});
@@ -990,7 +1003,7 @@ export default class GraphRedex<N extends GRND, E extends GRED> {
9901003
if (name) {
9911004
this.saveQry(name, d3.select("#qry").property("value"))
9921005
.then(() => alert("saved"))
993-
.catch((x) => alert(x));
1006+
.catch(errorAlert);
9941007
}
9951008
return false;
9961009
});
@@ -1051,7 +1064,7 @@ export default class GraphRedex<N extends GRND, E extends GRED> {
10511064
.then(() => {
10521065
alert("removed");
10531066
})
1054-
.catch((e) => alert(e));
1067+
.catch(errorAlert);
10551068
}
10561069
return false;
10571070
});
@@ -1063,7 +1076,7 @@ export default class GraphRedex<N extends GRND, E extends GRED> {
10631076
.then(() => {
10641077
alert("removed");
10651078
})
1066-
.catch((e) => alert(e));
1079+
.catch(errorAlert);
10671080
this.curExample = null;
10681081
this.reset();
10691082
}
@@ -1183,3 +1196,16 @@ function isInputDataArray<N extends GRND, E extends GRED>(
11831196
}
11841197
return false;
11851198
}
1199+
1200+
function errorAlert(e: any) {
1201+
console.error("ERROR", e);
1202+
if (typeof e === "object") {
1203+
if (e.err ?? false) {
1204+
alert("something went wrong\n\n" + e.err);
1205+
} else {
1206+
alert("something went wrong\n\n" + JSON.stringify(e));
1207+
}
1208+
} else {
1209+
alert("something went wrong\n\n" + e);
1210+
}
1211+
}

APIServer/src/Server.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,10 @@ export default class Server {
172172
req: express.Request,
173173
res: express.Response,
174174
) => {
175-
const file: MulterDiskFile = req["file"]; // tslint:disable-line
175+
const file: MulterDiskFile = req["file"] ?? null; // tslint:disable-line
176+
if (file === null) {
177+
throw "no file found";
178+
}
176179
const filenameParts = file.originalname.split(".");
177180
const extension: string = filenameParts.pop();
178181
const fileName: string =
@@ -290,6 +293,8 @@ export default class Server {
290293
.run(req.params.name, user, lang, term)
291294
.then((example) => {
292295
res.status(201).jsonp({
296+
ok: true,
297+
succes: true,
293298
lang: lang,
294299
term: term,
295300
output: example.baseTerm,
@@ -298,10 +303,12 @@ export default class Server {
298303
})
299304
.catch((e) => {
300305
res.status(500).jsonp({
306+
ok: false,
307+
succes: false,
301308
lang: lang,
302309
term: term,
303310
output: null,
304-
e: "something went wrong",
311+
err: "something went wrong",
305312
errors: e.toString(),
306313
});
307314
});
@@ -341,6 +348,20 @@ export default class Server {
341348
},
342349
),
343350
);
351+
352+
this.app.use((err, _req, res, next) => {
353+
if (res.headersSent) {
354+
console.error(err, "headers sent!!");
355+
return next(err);
356+
}
357+
res.status(500);
358+
console.error(err, "hey");
359+
if (typeof err === "string") {
360+
res.jsonp({ succes: false, ok: false, err: err });
361+
} else {
362+
res.jsonp({ succes: false, ok: false, err: err.toString() });
363+
}
364+
});
344365
}
345366

346367
/* Helper functions allowing async */

0 commit comments

Comments
 (0)