feat: Store cause for errors where relevant

This commit is contained in:
Joachim Van Herwegen
2021-06-30 16:03:36 +02:00
parent e44c337d0f
commit 6cf539c171
10 changed files with 17 additions and 14 deletions

View File

@@ -4,6 +4,7 @@
"Configuration",
"Error",
"EventEmitter",
"HttpErrorOptions",
"LRUCache",
"Provider",
"ValuePreferencesArg"

View File

@@ -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 });
}
}
}

View File

@@ -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 });
}
}
}

View File

@@ -12,14 +12,15 @@ import { IdpInteractionError } from '../util/IdpInteractionError';
export function throwIdpInteractionError(error: unknown, prefilled: Record<string, string> = {}): 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 });
}
}

View File

@@ -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

View File

@@ -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 containers containment triples;

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -46,9 +46,9 @@ export class WaterfallHandler<TIn, TOut> implements AsyncHandler<TIn, TOut> {
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);

View File

@@ -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 });
}
}