From ae4dd941ba6d957c913e43cd2825ec23eb3fe75e Mon Sep 17 00:00:00 2001 From: Maximiliano Osorio Date: Thu, 23 Nov 2023 11:15:57 -0300 Subject: [PATCH] fix: improve 404 error --- index.ts | 9 +++++---- src/renderHtml.ts | 8 ++++---- src/types.ts | 9 ++++++++- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/index.ts b/index.ts index 64d5211..9360f7a 100644 --- a/index.ts +++ b/index.ts @@ -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 = `

Not Found

There is no data for ${uri}

Endpoint: ${config.sparqlQueryUrl}

`; + 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 () => { diff --git a/src/renderHtml.ts b/src/renderHtml.ts index f0523a8..d20ff6d 100644 --- a/src/renderHtml.ts +++ b/src/renderHtml.ts @@ -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, diff --git a/src/types.ts b/src/types.ts index 9a64e09..b285c46 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,3 +1,5 @@ +import type * as RDF from "@rdfjs/types"; + interface Config { sparqlQueryUrl: string; port: number; @@ -10,4 +12,9 @@ interface Prefix { uri: string; } -export { Config, Prefix }; +interface QueryResults { + quads: RDF.Quad[]; + query: string; +} + +export { Config, Prefix, QueryResults };