From 2134ed8c1d7f7a27b4449e4a2248a0ab3635b67b Mon Sep 17 00:00:00 2001 From: David de Boer Date: Fri, 1 Dec 2023 15:40:50 +0100 Subject: [PATCH] feat: Handle distribution timeouts --- jest.config.js | 8 ++++---- src/analyzer/distribution.ts | 34 ++++++++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/jest.config.js b/jest.config.js index 1fce51f..9fd82fe 100644 --- a/jest.config.js +++ b/jest.config.js @@ -10,10 +10,10 @@ export default { coverageReporters: ['json-summary', 'text'], coverageThreshold: { global: { - lines: 24.48, - statements: 24.48, - branches: 13.88, - functions: 23.52, + lines: 23.52, + statements: 23.52, + branches: 13.51, + functions: 22.85, }, }, transform: { diff --git a/src/analyzer/distribution.ts b/src/analyzer/distribution.ts index e3c9e03..587ae9f 100644 --- a/src/analyzer/distribution.ts +++ b/src/analyzer/distribution.ts @@ -7,7 +7,16 @@ import namedNode = DataFactory.namedNode; import blankNode = DataFactory.blankNode; import literal = DataFactory.literal; -async function probe(distribution: Distribution) { +class NetworkError { + constructor( + public readonly url: string, + public readonly message: string + ) {} +} + +async function probe( + distribution: Distribution +): Promise { if (distribution.isSparql()) { return fetch(distribution.accessUrl!, { method: 'POST', @@ -19,10 +28,15 @@ async function probe(distribution: Distribution) { }); } - return fetch(distribution.accessUrl!, { - method: 'HEAD', - headers: {Accept: distribution.mimeType!}, - }); + try { + return await fetch(distribution.accessUrl!, { + signal: AbortSignal.timeout(5000), + method: 'HEAD', + headers: {Accept: distribution.mimeType!}, + }); + } catch (e) { + return new NetworkError(distribution.accessUrl!, (e as Error).name); + } } export class DistributionAnalyzer implements Analyzer { @@ -49,7 +63,15 @@ export class DistributionAnalyzer implements Analyzer { ), ]); - if (result.status >= 200 && result.status < 400) { + if (result instanceof NetworkError) { + store.addQuad( + quad( + action, + namedNode('https://schema.org/error'), + literal(result.message) // TODO: find a URI for this, for example TimeoutError. + ) + ); + } else if (result.status >= 200 && result.status < 400) { const dataDownload = namedNode(result.url); store.addQuad( quad(action, namedNode('https://schema.org/result'), dataDownload)