Skip to content

Commit

Permalink
feat: Handle distribution timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
ddeboer committed Dec 1, 2023
1 parent 6087a9d commit 2134ed8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
8 changes: 4 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
34 changes: 28 additions & 6 deletions src/analyzer/distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response | NetworkError> {
if (distribution.isSparql()) {
return fetch(distribution.accessUrl!, {
method: 'POST',
Expand All @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 2134ed8

Please sign in to comment.