Improved error handling #8
3 changed files with 52 additions and 60 deletions
|
|
@ -1,7 +1,6 @@
|
|||
{
|
||||
"name": "shacl-tulip",
|
||||
"private": true,
|
||||
"version": "0.0.1",
|
||||
"version": "0.0.2",
|
||||
"type": "module",
|
||||
"main": "./src/index.js",
|
||||
"module": "./src/index.js",
|
||||
|
|
|
|||
|
|
@ -49,25 +49,28 @@ export class RdfDataset {
|
|||
*/
|
||||
async loadRDF(url, headers = {}) {
|
||||
this.beforeLoadFn()
|
||||
readRDF(url, headers)
|
||||
.then(quadStream => {
|
||||
// Load prefixes
|
||||
quadStream.on('prefix', (prefix, ns) => {
|
||||
this.onPrefixFn(prefix, ns)
|
||||
}).on('end', () => {
|
||||
this.onPrefixEndFn()
|
||||
})
|
||||
// Load data
|
||||
quadStream.on('data', quad => {
|
||||
this.onDataFn(quad)
|
||||
}).on('end', async () => {
|
||||
await this.onDataEndFn()
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error reading RDF data:', error);
|
||||
throw error
|
||||
});
|
||||
const result = await readRDF(url, headers)
|
||||
|
||||
// Bubble up error
|
||||
if (!result.success) {
|
||||
return result
|
||||
}
|
||||
|
||||
const quadStream = result.quadStream
|
||||
// Load prefixes
|
||||
quadStream.on('prefix', (prefix, ns) => {
|
||||
this.onPrefixFn(prefix, ns)
|
||||
}).on('end', () => {
|
||||
this.onPrefixEndFn()
|
||||
})
|
||||
// Load data
|
||||
quadStream.on('data', quad => {
|
||||
this.onDataFn(quad)
|
||||
}).on('end', async () => {
|
||||
await this.onDataEndFn()
|
||||
});
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -13,9 +13,11 @@ import formatsPretty from '@rdfjs/formats/pretty.js'
|
|||
import rdf from 'rdf-ext'
|
||||
|
||||
export async function readRDF(file_url, headers = { "Content-Type": "text/turtle" }) {
|
||||
const url = file_url;
|
||||
try {
|
||||
console.log(url)
|
||||
var res = null
|
||||
res = await fetch(file_url,
|
||||
res = await fetch(url,
|
||||
{
|
||||
formats,
|
||||
headers: headers
|
||||
|
|
@ -25,48 +27,36 @@ export async function readRDF(file_url, headers = { "Content-Type": "text/turtle
|
|||
if (['text/plain', 'text/html'].indexOf(res.headers.get('content-type')) >= 0) {
|
||||
// default to turtle
|
||||
headers['Content-Type'] = 'text/turtle';
|
||||
res = await fetch(file_url, { formats, headers });
|
||||
res = await fetch(url, { formats, headers });
|
||||
}
|
||||
if (res.ok) {
|
||||
if (res.headers.get('content-type').indexOf('application/json') >= 0) {
|
||||
console.log(res.json())
|
||||
throw new Error(`readRDF error: cannot read json data`);
|
||||
}
|
||||
const quadStream = await res.quadStream()
|
||||
// quadStream.on('error', err => console.error(err))
|
||||
return quadStream
|
||||
} else {
|
||||
throw new Error(`readRDF error:: ${res.statusText}`);
|
||||
|
||||
if (!res.ok) {
|
||||
// throw new Error(`readRDF error: ${res.statusText}`)
|
||||
const code = res.status || 'Unknown';
|
||||
const error = new Error(`readRDF error: HTTP ${code} from ${url}`);
|
||||
error.status = code;
|
||||
error.url = url;
|
||||
error.response = res;
|
||||
throw error
|
||||
}
|
||||
|
||||
if (res.headers.get('content-type').indexOf('application/json') >= 0) {
|
||||
throw new Error(`readRDF error: reading json data is not supported`);
|
||||
}
|
||||
|
||||
const quadStream = await res.quadStream()
|
||||
return {
|
||||
success: true,
|
||||
quadStream,
|
||||
url: url,
|
||||
message: 'RDF data loaded successfully',
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('readRDF error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export async function postRDF(endpoint, dataset, format = 'text/turtle', headers = {}, prefixes) {
|
||||
|
||||
try {
|
||||
const rdfPretty = rdf.clone() // clone the default environment
|
||||
rdfPretty.formats.import(formatsPretty) // import pretty print serializers
|
||||
// Ensure we have the correct content-type
|
||||
headers['Content-Type'] = format;
|
||||
// Serialize the dataset to the desired format
|
||||
const body = await rdfPretty.io.dataset.toText('text/turtle', dataset)
|
||||
const response = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
formats,
|
||||
headers,
|
||||
body,
|
||||
prefixes,
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`postRDF error: ${response.statusText}`);
|
||||
}
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error('postRDF error:', error);
|
||||
throw error;
|
||||
return {
|
||||
success: false,
|
||||
error,
|
||||
url: url,
|
||||
message: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue