From 218c8f4662b41f8f4d534d8b54f912664c365769 Mon Sep 17 00:00:00 2001 From: Joachim Van Herwegen Date: Thu, 8 Apr 2021 14:33:25 +0200 Subject: [PATCH] fix: Use HttpErrors instead of Errors --- src/ldp/http/BasicRequestParser.ts | 3 ++- src/ldp/http/OriginalUrlExtractor.ts | 8 +++++--- src/ldp/representation/RepresentationMetadata.ts | 5 ++++- src/storage/accessors/InMemoryDataAccessor.ts | 3 ++- src/storage/conversion/ChainedConverter.ts | 3 ++- src/storage/mapping/BaseFileIdentifierMapper.ts | 3 ++- src/util/handlers/WaterfallHandler.ts | 2 +- 7 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/ldp/http/BasicRequestParser.ts b/src/ldp/http/BasicRequestParser.ts index b24c8f80d..58ada209d 100644 --- a/src/ldp/http/BasicRequestParser.ts +++ b/src/ldp/http/BasicRequestParser.ts @@ -1,4 +1,5 @@ import type { HttpRequest } from '../../server/HttpRequest'; +import { InternalServerError } from '../../util/errors/InternalServerError'; import type { Operation } from '../operations/Operation'; import type { BodyParser } from './BodyParser'; import type { MetadataExtractor } from './metadata/MetadataExtractor'; @@ -34,7 +35,7 @@ export class BasicRequestParser extends RequestParser { public async handle(request: HttpRequest): Promise { const { method } = request; if (!method) { - throw new Error('No method specified on the HTTP request'); + throw new InternalServerError('No method specified on the HTTP request'); } const target = await this.targetExtractor.handleSafe({ request }); const preferences = await this.preferenceParser.handleSafe({ request }); diff --git a/src/ldp/http/OriginalUrlExtractor.ts b/src/ldp/http/OriginalUrlExtractor.ts index facf4865a..5e689daf3 100644 --- a/src/ldp/http/OriginalUrlExtractor.ts +++ b/src/ldp/http/OriginalUrlExtractor.ts @@ -1,5 +1,7 @@ import type { TLSSocket } from 'tls'; import type { HttpRequest } from '../../server/HttpRequest'; +import { BadRequestHttpError } from '../../util/errors/BadRequestHttpError'; +import { InternalServerError } from '../../util/errors/InternalServerError'; import { parseForwarded } from '../../util/HeaderUtil'; import { toCanonicalUriPath } from '../../util/PathUtil'; import type { ResourceIdentifier } from '../representation/ResourceIdentifier'; @@ -18,7 +20,7 @@ export class OriginalUrlExtractor extends TargetExtractor { public async handle({ request: { url, connection, headers }}: { request: HttpRequest }): Promise { if (!url) { - throw new Error('Missing URL'); + throw new InternalServerError('Missing URL'); } // Extract host and protocol (possibly overridden by the Forwarded/X-Forwarded-* header) @@ -36,10 +38,10 @@ export class OriginalUrlExtractor extends TargetExtractor { // Perform a sanity check on the host if (!host) { - throw new Error('Missing Host header'); + throw new BadRequestHttpError('Missing Host header'); } if (/[/\\*]/u.test(host)) { - throw new Error(`The request has an invalid Host header: ${host}`); + throw new BadRequestHttpError(`The request has an invalid Host header: ${host}`); } // URL object applies punycode encoding to domain diff --git a/src/ldp/representation/RepresentationMetadata.ts b/src/ldp/representation/RepresentationMetadata.ts index be82e4c7a..a3c495ef5 100644 --- a/src/ldp/representation/RepresentationMetadata.ts +++ b/src/ldp/representation/RepresentationMetadata.ts @@ -1,6 +1,7 @@ import { DataFactory, Store } from 'n3'; import type { BlankNode, Literal, NamedNode, Quad, Term } from 'rdf-js'; import { getLoggerFor } from '../../logging/LogUtil'; +import { InternalServerError } from '../../util/errors/InternalServerError'; import { toSubjectTerm, toObjectTerm, toCachedNamedNode, isTerm } from '../../util/TermUtil'; import { CONTENT_TYPE, CONTENT_TYPE_TERM } from '../../util/Vocabularies'; import type { ResourceIdentifier } from './ResourceIdentifier'; @@ -265,7 +266,9 @@ export class RepresentationMetadata { } if (terms.length > 1) { this.logger.error(`Multiple results for ${typeof predicate === 'string' ? predicate : predicate.value}`); - throw new Error(`Multiple results for ${typeof predicate === 'string' ? predicate : predicate.value}`); + throw new InternalServerError( + `Multiple results for ${typeof predicate === 'string' ? predicate : predicate.value}`, + ); } return terms[0]; } diff --git a/src/storage/accessors/InMemoryDataAccessor.ts b/src/storage/accessors/InMemoryDataAccessor.ts index b885e27a9..e748646b8 100644 --- a/src/storage/accessors/InMemoryDataAccessor.ts +++ b/src/storage/accessors/InMemoryDataAccessor.ts @@ -3,6 +3,7 @@ import arrayifyStream from 'arrayify-stream'; import type { NamedNode } from 'rdf-js'; import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata'; import type { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier'; +import { InternalServerError } from '../../util/errors/InternalServerError'; import { NotFoundHttpError } from '../../util/errors/NotFoundHttpError'; import type { Guarded } from '../../util/GuardedStream'; import type { IdentifierStrategy } from '../../util/identifiers/IdentifierStrategy'; @@ -121,7 +122,7 @@ export class InMemoryDataAccessor implements DataAccessor { throw new NotFoundHttpError(); } if (this.isDataEntry(parent)) { - throw new Error('Invalid path.'); + throw new InternalServerError('Invalid path.'); } } diff --git a/src/storage/conversion/ChainedConverter.ts b/src/storage/conversion/ChainedConverter.ts index 83d9d05a8..fc6de1298 100644 --- a/src/storage/conversion/ChainedConverter.ts +++ b/src/storage/conversion/ChainedConverter.ts @@ -1,5 +1,6 @@ import type { Representation } from '../../ldp/representation/Representation'; import { getLoggerFor } from '../../logging/LogUtil'; +import { InternalServerError } from '../../util/errors/InternalServerError'; import { matchesMediaType } from './ConversionUtil'; import type { RepresentationConverterArgs } from './RepresentationConverter'; import { TypedRepresentationConverter } from './TypedRepresentationConverter'; @@ -79,7 +80,7 @@ export class ChainedConverter extends TypedRepresentationConverter { if (bestMatch.weight === 0) { this.logger.warn(`No match found between ${leftKeys} and ${rightKeys}`); - throw new Error(`No match found between ${leftKeys} and ${rightKeys}`); + throw new InternalServerError(`No match found between ${leftKeys} and ${rightKeys}`); } this.logger.debug(`${bestMatch.type} is the best match between ${leftKeys} and ${rightKeys}`); diff --git a/src/storage/mapping/BaseFileIdentifierMapper.ts b/src/storage/mapping/BaseFileIdentifierMapper.ts index e950b336c..e438842f0 100644 --- a/src/storage/mapping/BaseFileIdentifierMapper.ts +++ b/src/storage/mapping/BaseFileIdentifierMapper.ts @@ -2,6 +2,7 @@ import type { ResourceIdentifier } from '../../ldp/representation/ResourceIdenti import { getLoggerFor } from '../../logging/LogUtil'; import { APPLICATION_OCTET_STREAM } from '../../util/ContentTypes'; import { BadRequestHttpError } from '../../util/errors/BadRequestHttpError'; +import { InternalServerError } from '../../util/errors/InternalServerError'; import { NotFoundHttpError } from '../../util/errors/NotFoundHttpError'; import { decodeUriPathComponents, @@ -98,7 +99,7 @@ export class BaseFileIdentifierMapper implements FileIdentifierMapper { public async mapFilePathToUrl(filePath: string, isContainer: boolean): Promise { if (!filePath.startsWith(this.rootFilepath)) { this.logger.error(`Trying to access file ${filePath} outside of ${this.rootFilepath}`); - throw new Error(`File ${filePath} is not part of the file storage at ${this.rootFilepath}`); + throw new InternalServerError(`File ${filePath} is not part of the file storage at ${this.rootFilepath}`); } const relative = filePath.slice(this.rootFilepath.length); let url: string; diff --git a/src/util/handlers/WaterfallHandler.ts b/src/util/handlers/WaterfallHandler.ts index 50a5dc5d0..4e7880369 100644 --- a/src/util/handlers/WaterfallHandler.ts +++ b/src/util/handlers/WaterfallHandler.ts @@ -48,7 +48,7 @@ export class WaterfallHandler implements AsyncHandler { handler = await this.findHandler(input); } catch { this.logger.warn('All handlers failed. This might be the consequence of calling handle before canHandle.'); - throw new Error('All handlers failed'); + throw new InternalServerError('All handlers failed'); } return handler.handle(input);