fix: Prevent server from crashing if requested data can't be handled

This commit is contained in:
Joachim Van Herwegen
2020-07-13 10:41:10 +02:00
parent ea788ba406
commit ccd3f1738c
6 changed files with 31 additions and 5 deletions

View File

@@ -98,7 +98,7 @@ export class AuthenticatedLdpHandler extends HttpHandler {
const writeData = { response: input.response, description, error: err };
return this.responseWriter.handleSafe(writeData);
await this.responseWriter.handleSafe(writeData);
}
/**

View File

@@ -12,6 +12,11 @@ export class SimpleResponseWriter extends ResponseWriter {
if (!input.description && !input.error) {
throw new UnsupportedHttpError('Either a description or an error is required for output.');
}
if (input.description && input.description.body) {
if (input.description.body.dataType !== 'binary' && input.description.body.dataType !== 'string') {
throw new UnsupportedHttpError('Only string or binary results are supported.');
}
}
}
public async handle(input: { response: HttpResponse; description?: ResponseDescription; error?: Error }): Promise<void> {

View File

@@ -21,7 +21,13 @@ export class ExpressHttpServer {
}));
app.use(async(request, response): Promise<void> => {
await this.handler.handleSafe({ request, response });
try {
await this.handler.handleSafe({ request, response });
} catch (error) {
const errMsg = `${error.name}: ${error.message}\n${error.stack}`;
process.stderr.write(errMsg);
response.status(500).send(errMsg);
}
});
return app.listen(port);
}