From 6cf539c17156339ad40c9a9701e308629fb514f9 Mon Sep 17 00:00:00 2001 From: Joachim Van Herwegen Date: Wed, 30 Jun 2021 16:03:36 +0200 Subject: [PATCH] feat: Store cause for errors where relevant --- .componentsignore | 1 + src/authentication/BearerWebIdExtractor.ts | 2 +- src/authentication/DPoPWebIdExtractor.ts | 2 +- .../interaction/email-password/EmailPasswordUtil.ts | 7 ++++--- src/ldp/http/SparqlUpdateBodyParser.ts | 2 +- src/storage/DataAccessorBasedStore.ts | 3 ++- src/storage/RoutingResourceStore.ts | 2 +- src/storage/accessors/FileDataAccessor.ts | 2 +- src/util/handlers/WaterfallHandler.ts | 4 ++-- src/util/locking/RedisResourceLocker.ts | 6 +++--- 10 files changed, 17 insertions(+), 14 deletions(-) diff --git a/.componentsignore b/.componentsignore index 2e9c3c76b..0ac96dffd 100644 --- a/.componentsignore +++ b/.componentsignore @@ -4,6 +4,7 @@ "Configuration", "Error", "EventEmitter", + "HttpErrorOptions", "LRUCache", "Provider", "ValuePreferencesArg" diff --git a/src/authentication/BearerWebIdExtractor.ts b/src/authentication/BearerWebIdExtractor.ts index f68f2190e..167347e8f 100644 --- a/src/authentication/BearerWebIdExtractor.ts +++ b/src/authentication/BearerWebIdExtractor.ts @@ -36,7 +36,7 @@ export class BearerWebIdExtractor extends CredentialsExtractor { } catch (error: unknown) { const message = `Error verifying WebID via Bearer access token: ${(error as Error).message}`; this.logger.warn(message); - throw new BadRequestHttpError(message); + throw new BadRequestHttpError(message, { cause: error }); } } } diff --git a/src/authentication/DPoPWebIdExtractor.ts b/src/authentication/DPoPWebIdExtractor.ts index 1b04942c1..5cdd69cd3 100644 --- a/src/authentication/DPoPWebIdExtractor.ts +++ b/src/authentication/DPoPWebIdExtractor.ts @@ -57,7 +57,7 @@ export class DPoPWebIdExtractor extends CredentialsExtractor { } catch (error: unknown) { const message = `Error verifying WebID via DPoP-bound access token: ${(error as Error).message}`; this.logger.warn(message); - throw new BadRequestHttpError(message); + throw new BadRequestHttpError(message, { cause: error }); } } } diff --git a/src/identity/interaction/email-password/EmailPasswordUtil.ts b/src/identity/interaction/email-password/EmailPasswordUtil.ts index f4b555002..467374794 100644 --- a/src/identity/interaction/email-password/EmailPasswordUtil.ts +++ b/src/identity/interaction/email-password/EmailPasswordUtil.ts @@ -12,14 +12,15 @@ import { IdpInteractionError } from '../util/IdpInteractionError'; export function throwIdpInteractionError(error: unknown, prefilled: Record = {}): never { if (IdpInteractionError.isInstance(error)) { if (Object.keys(prefilled).length > 0) { - throw new IdpInteractionError(error.statusCode, error.message, { ...error.prefilled, ...prefilled }); + const { statusCode, message } = error; + throw new IdpInteractionError(statusCode, message, { ...error.prefilled, ...prefilled }, { cause: error }); } else { throw error; } } else if (HttpError.isInstance(error)) { - throw new IdpInteractionError(error.statusCode, error.message, prefilled); + throw new IdpInteractionError(error.statusCode, error.message, prefilled, { cause: error }); } else { - throw new IdpInteractionError(500, createErrorMessage(error), prefilled); + throw new IdpInteractionError(500, createErrorMessage(error), prefilled, { cause: error }); } } diff --git a/src/ldp/http/SparqlUpdateBodyParser.ts b/src/ldp/http/SparqlUpdateBodyParser.ts index b9028a6ff..f0da06913 100644 --- a/src/ldp/http/SparqlUpdateBodyParser.ts +++ b/src/ldp/http/SparqlUpdateBodyParser.ts @@ -29,7 +29,7 @@ export class SparqlUpdateBodyParser extends BodyParser { algebra = translate(sparql, { quads: true, baseIRI: metadata.identifier.value }); } catch (error: unknown) { this.logger.warn('Could not translate SPARQL query to SPARQL algebra', { error }); - throw new BadRequestHttpError(createErrorMessage(error)); + throw new BadRequestHttpError(createErrorMessage(error), { cause: error }); } // Prevent body from being requested again diff --git a/src/storage/DataAccessorBasedStore.ts b/src/storage/DataAccessorBasedStore.ts index 0be878886..90da48364 100644 --- a/src/storage/DataAccessorBasedStore.ts +++ b/src/storage/DataAccessorBasedStore.ts @@ -364,7 +364,8 @@ export class DataAccessorBasedStore implements ResourceStore { quads = await parseQuads(representation.data, { format: contentType, baseIRI: identifier.value }); } } catch (error: unknown) { - throw new BadRequestHttpError(`Can only create containers with RDF data. ${createErrorMessage(error)}`); + throw new BadRequestHttpError(`Can only create containers with RDF data. ${createErrorMessage(error)}`, + { cause: error }); } // Solid, §5.3: "Servers MUST NOT allow HTTP POST, PUT and PATCH to update a container’s containment triples; diff --git a/src/storage/RoutingResourceStore.ts b/src/storage/RoutingResourceStore.ts index 83ae03887..65866e85a 100644 --- a/src/storage/RoutingResourceStore.ts +++ b/src/storage/RoutingResourceStore.ts @@ -60,7 +60,7 @@ export class RoutingResourceStore implements ResourceStore { return await this.rule.handleSafe({ identifier }); } catch (error: unknown) { if (NotImplementedHttpError.isInstance(error)) { - throw new NotFoundHttpError(); + throw new NotFoundHttpError('', { cause: error }); } throw error; } diff --git a/src/storage/accessors/FileDataAccessor.ts b/src/storage/accessors/FileDataAccessor.ts index a82208ce2..39f786de5 100644 --- a/src/storage/accessors/FileDataAccessor.ts +++ b/src/storage/accessors/FileDataAccessor.ts @@ -155,7 +155,7 @@ export class FileDataAccessor implements DataAccessor { return await fsPromises.lstat(path); } catch (error: unknown) { if (isSystemError(error) && error.code === 'ENOENT') { - throw new NotFoundHttpError(); + throw new NotFoundHttpError('', { cause: error }); } throw error; } diff --git a/src/util/handlers/WaterfallHandler.ts b/src/util/handlers/WaterfallHandler.ts index d87a718fb..de1ac5cd0 100644 --- a/src/util/handlers/WaterfallHandler.ts +++ b/src/util/handlers/WaterfallHandler.ts @@ -46,9 +46,9 @@ export class WaterfallHandler implements AsyncHandler { try { handler = await this.findHandler(input); - } catch { + } catch (error: unknown) { this.logger.warn('All handlers failed. This might be the consequence of calling handle before canHandle.'); - throw new InternalServerError('All handlers failed'); + throw new InternalServerError('All handlers failed', { cause: error }); } return handler.handle(input); diff --git a/src/util/locking/RedisResourceLocker.ts b/src/util/locking/RedisResourceLocker.ts index c34b48e37..cb91e1ef6 100644 --- a/src/util/locking/RedisResourceLocker.ts +++ b/src/util/locking/RedisResourceLocker.ts @@ -94,7 +94,7 @@ export class RedisResourceLocker implements ResourceLocker, Finalizable { { ...defaultRedlockConfig, ...redlockOptions }, ); } catch (error: unknown) { - throw new InternalServerError(`Error initializing Redlock: ${error}`); + throw new InternalServerError(`Error initializing Redlock: ${error}`, { cause: error }); } } @@ -118,7 +118,7 @@ export class RedisResourceLocker implements ResourceLocker, Finalizable { assert(lock); } catch (error: unknown) { this.logger.debug(`Unable to acquire lock for ${resource}`); - throw new InternalServerError(`Unable to acquire lock for ${resource} (${error})`); + throw new InternalServerError(`Unable to acquire lock for ${resource} (${error})`, { cause: error }); } if (this.lockMap.get(resource)) { throw new InternalServerError(`Acquired duplicate lock on ${resource}`); @@ -145,7 +145,7 @@ export class RedisResourceLocker implements ResourceLocker, Finalizable { this.logger.debug(`Released lock for ${resource}, ${this.getLockCount()} active locks remaining!`); } catch (error: unknown) { this.logger.error(`Error releasing lock for ${resource} (${error})`); - throw new InternalServerError(`Unable to release lock for: ${resource}, ${error}`); + throw new InternalServerError(`Unable to release lock for: ${resource}, ${error}`, { cause: error }); } }