diff --git a/src/storage/conversion/RdfToQuadConverter.ts b/src/storage/conversion/RdfToQuadConverter.ts index 040416183..7169915af 100644 --- a/src/storage/conversion/RdfToQuadConverter.ts +++ b/src/storage/conversion/RdfToQuadConverter.ts @@ -3,6 +3,7 @@ import rdfParser from 'rdf-parse'; import type { Representation } from '../../ldp/representation/Representation'; import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata'; import { INTERNAL_QUADS } from '../../util/ContentTypes'; +import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError'; import { CONTENT_TYPE } from '../../util/UriConstants'; import { pipeStreamsAndErrors } from '../../util/Util'; import { checkRequest } from './ConversionUtil'; @@ -39,7 +40,7 @@ export class RdfToQuadConverter extends TypedRepresentationConverter { // Wrap the stream such that errors are transformed // (Node 10 requires both writableObjectMode and readableObjectMode) const data = new PassThrough({ writableObjectMode: true, readableObjectMode: true }); - pipeStreamsAndErrors(rawQuads, data); + pipeStreamsAndErrors(rawQuads, data, (error): Error => new UnsupportedHttpError(error.message)); return { binary: false, diff --git a/src/util/Util.ts b/src/util/Util.ts index b8949777a..0d1181435 100644 --- a/src/util/Util.ts +++ b/src/util/Util.ts @@ -2,7 +2,6 @@ import type { Readable, Writable } from 'stream'; import arrayifyStream from 'arrayify-stream'; import { DataFactory } from 'n3'; import type { Literal, NamedNode, Quad } from 'rdf-js'; -import { UnsupportedHttpError } from './errors/UnsupportedHttpError'; /** * Makes sure the input path has exactly 1 slash at the end. @@ -60,12 +59,14 @@ export const matchingMediaType = (mediaA: string, mediaB: string): boolean => { * Makes sure an error of the first stream gets passed to the second. * @param readable - Initial readable stream. * @param destination - The destination for writing data. + * @param mapError - Optional function that takes the error and converts it to a new error. * * @returns The destination stream. */ -export const pipeStreamsAndErrors = (readable: Readable, destination: T): T => { +export const pipeStreamsAndErrors = (readable: Readable, destination: T, + mapError?: (error: Error) => Error): T => { readable.pipe(destination); - readable.on('error', (error): boolean => destination.emit('error', new UnsupportedHttpError(error.message))); + readable.on('error', (error): boolean => destination.emit('error', mapError ? mapError(error) : error)); return destination; };