fix(core): update

This commit is contained in:
Philipp Kunz 2020-06-16 15:03:10 +00:00
parent f98c2cc5e7
commit 2879fb8cee
2 changed files with 32 additions and 2 deletions

View File

@ -1,4 +1,5 @@
import * as plugins from './typedrequest.plugins';
import { TypedResponseError } from './typedrequest.classes.typedresponseerror';
type THandlerFunction<T extends plugins.typedRequestInterfaces.ITypedRequest> = (
requestArg: T['request']
@ -26,8 +27,27 @@ export class TypedHandler<T extends plugins.typedRequestInterfaces.ITypedRequest
'this handler has been given a wrong method to answer to. Please use a TypedRouter to filter requests'
);
}
const response = await this.handlerFunction(typedRequestArg.request);
typedRequestArg.response = response;
let typedResponseError: TypedResponseError;
const response = await this.handlerFunction(typedRequestArg.request).catch(e => {
if (e instanceof TypedResponseError) {
typedResponseError = e;
} else {
throw e;
}
});
if (typedResponseError) {
typedRequestArg.error = {
text: typedResponseError.errorText,
data: typedResponseError.errorData
};
}
if (response) {
typedRequestArg.response = response;
}
return typedRequestArg;
}
}

View File

@ -0,0 +1,10 @@
import * as plugins from './typedrequest.plugins';
export class TypedResponseError {
public errorText: string;
public errorData: any;
constructor(errorTextArg: string, errorDataArg: any) {
this.errorText = errorTextArg;
this.errorData = errorDataArg;
}
}