mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
fix: Make sure there is always a fallback for error handling
This commit is contained in:
@@ -117,10 +117,10 @@ export * from './ldp/http/PreferenceParser';
|
||||
export * from './ldp/http/RawBodyParser';
|
||||
export * from './ldp/http/RequestParser';
|
||||
export * from './ldp/http/ResponseWriter';
|
||||
export * from './ldp/http/SafeErrorHandler';
|
||||
export * from './ldp/http/SparqlUpdateBodyParser';
|
||||
export * from './ldp/http/SparqlUpdatePatch';
|
||||
export * from './ldp/http/TargetExtractor';
|
||||
export * from './ldp/http/TextErrorHandler';
|
||||
|
||||
// LDP/Operations
|
||||
export * from './ldp/operations/DeleteOperationHandler';
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { getStatusCode } from '../../util/errors/ErrorUtil';
|
||||
import { getLoggerFor } from '../../logging/LogUtil';
|
||||
import { createErrorMessage, getStatusCode } from '../../util/errors/ErrorUtil';
|
||||
import { guardedStreamFrom } from '../../util/StreamUtil';
|
||||
import { toLiteral } from '../../util/TermUtil';
|
||||
import { HTTP, XSD } from '../../util/Vocabularies';
|
||||
@@ -9,17 +10,27 @@ import type { ResponseDescription } from './response/ResponseDescription';
|
||||
|
||||
/**
|
||||
* Returns a simple text description of an error.
|
||||
* This class is mostly a failsafe in case all other solutions fail.
|
||||
* This class is a failsafe in case the wrapped error handler fails.
|
||||
*/
|
||||
export class TextErrorHandler extends ErrorHandler {
|
||||
export class SafeErrorHandler extends ErrorHandler {
|
||||
protected readonly logger = getLoggerFor(this);
|
||||
|
||||
private readonly errorHandler: ErrorHandler;
|
||||
private readonly showStackTrace: boolean;
|
||||
|
||||
public constructor(showStackTrace = false) {
|
||||
public constructor(errorHandler: ErrorHandler, showStackTrace = false) {
|
||||
super();
|
||||
this.errorHandler = errorHandler;
|
||||
this.showStackTrace = showStackTrace;
|
||||
}
|
||||
|
||||
public async handle({ error }: ErrorHandlerArgs): Promise<ResponseDescription> {
|
||||
public async handle(input: ErrorHandlerArgs): Promise<ResponseDescription> {
|
||||
try {
|
||||
return await this.errorHandler.handleSafe(input);
|
||||
} catch (error: unknown) {
|
||||
this.logger.debug(`Recovering from error handler failure: ${createErrorMessage(error)}`);
|
||||
}
|
||||
const { error } = input;
|
||||
const statusCode = getStatusCode(error);
|
||||
const metadata = new RepresentationMetadata('text/plain');
|
||||
metadata.add(HTTP.terms.statusCodeNumber, toLiteral(statusCode, XSD.terms.integer));
|
||||
Reference in New Issue
Block a user