mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
chore: Move FetchUtil to util folder.
This commit is contained in:
29
src/util/FetchUtil.ts
Normal file
29
src/util/FetchUtil.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import fetch from '@rdfjs/fetch';
|
||||
import type { DatasetResponse } from '@rdfjs/fetch-lite';
|
||||
import type { Dataset } from 'rdf-js';
|
||||
import { getLoggerFor } from '../logging/LogUtil';
|
||||
import { createErrorMessage } from './errors/ErrorUtil';
|
||||
|
||||
const logger = getLoggerFor('FetchUtil');
|
||||
|
||||
/**
|
||||
* Fetches an RDF dataset from the given URL.
|
||||
*/
|
||||
export async function fetchDataset(url: string): Promise<Dataset> {
|
||||
let rawResponse: DatasetResponse<Dataset>;
|
||||
try {
|
||||
rawResponse = (await fetch(url)) as DatasetResponse<Dataset>;
|
||||
} catch (err: unknown) {
|
||||
logger.error(`Cannot fetch ${url}: ${createErrorMessage(err)}`);
|
||||
throw new Error(`Cannot fetch ${url}`);
|
||||
}
|
||||
let dataset: Dataset;
|
||||
try {
|
||||
dataset = await rawResponse.dataset();
|
||||
} catch (err: unknown) {
|
||||
logger.error(`Could not parse RDF in ${url}: ${createErrorMessage(err)}`);
|
||||
// Keeping the error message the same to prevent leaking possible information about intranet
|
||||
throw new Error(`Cannot fetch ${url}`);
|
||||
}
|
||||
return dataset;
|
||||
}
|
||||
Reference in New Issue
Block a user