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 = `
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 };