Skip to content

Commit

Permalink
fix: improve 404 error
Browse files Browse the repository at this point in the history
  • Loading branch information
mosoriob committed Nov 23, 2023
1 parent 2549867 commit ae4dd94
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
9 changes: 5 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ app.get("/*", (req: Request, res: Response) => {
req.log.info(`Request for ${uri}`);
res.format({
"text/html": async () => {
try {
const data = await handleQueryHtml(uri, config.sparqlQueryUrl);
const data = await handleQueryHtml(uri, config.sparqlQueryUrl);
if (data.outcomingQuads.length === 0 && data.incomingQuads.length === 0) {
const message = `<h1>Not Found</h1> <p>There is no data for <a href="${uri}">${uri}</a></p> <p> Endpoint: <a href="${config.sparqlQueryUrl}">${config.sparqlQueryUrl}</a></p>`;
return res.status(404).send(message);
} else {
return res.render("results", data);
} catch (error) {
return res.status(404).send("Not Found");
}
},
"application/n-quads": async () => {
Expand Down
8 changes: 4 additions & 4 deletions src/renderHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { toPrefix } from "./utils";
const handleQueryHtml = async (uri: string, endpoint: string) => {
const outcomingQuads = await performQueryHtml(uri, endpoint);
const incomingQuads = await performIncomingQueryHtml(uri, endpoint);
if (outcomingQuads.length === 0 && incomingQuads.length === 0) {
throw new Error("Not Found");
}
const title = getTitle();
const title =
outcomingQuads.length === 0 && incomingQuads.length === 0
? "Not Found"
: getTitle();
return {
incomingQuads: incomingQuads,
outcomingQuads: outcomingQuads,
Expand Down
9 changes: 8 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type * as RDF from "@rdfjs/types";

interface Config {
sparqlQueryUrl: string;
port: number;
Expand All @@ -10,4 +12,9 @@ interface Prefix {
uri: string;
}

export { Config, Prefix };
interface QueryResults {
quads: RDF.Quad[];
query: string;
}

export { Config, Prefix, QueryResults };

0 comments on commit ae4dd94

Please sign in to comment.