deepkit / deepkit-framework

A new full-featured and high-performance TypeScript framework
https://deepkit.io/
MIT License
3.22k stars 123 forks source link

HttpAccessDeniedError error.message not available in httpWorkflow.onAccessDenied.event #398

Closed colorcube closed 1 year ago

colorcube commented 1 year ago

Sending custom error messages seems not to be easy:

    @eventDispatcher.listen(httpWorkflow.onAccessDenied)
    async onAccessDenied(event: typeof httpWorkflow.onAccessDenied.event) {
        event.send(new HtmlResponse(event.error.message || 'Access denied', 403));
    }

event.error.message is not available there - at least I haven't found the exception message

That means in http controller it's not possible to send custom messages like

throw new HttpAccessDeniedError('my custom "Access denied" message');

in controller something like that is possible get the message out but that's not very sexy

        } catch (e) {
            if (e instanceof HttpAccessDeniedError) {
                response.setHeader('X-Reason', e.message || 'unknown');
            }
            throw e;
        }
colorcube commented 1 year ago

I guess this is the code to be changed:

if (error instanceof HttpAccessDeniedError) {
    event.next('accessDenied', new HttpAccessDeniedEvent(event.injectorContext, event.request, event.response, event.route));
} else {
    const errorEvent = new HttpControllerErrorEvent(event.injectorContext, event.request, event.response, event.route, error);
    errorEvent.controllerActionTime = Date.now() - start;
    event.next('controllerError', errorEvent);
}

adding error to HttpAccessDeniedEvent ?!

marcj commented 1 year ago

adding error to HttpAccessDeniedEvent ?!

yes, we can do that

marcj commented 1 year ago

@colorcube added in https://github.com/deepkit/deepkit-framework/commit/53ffb33d5e775067dac34a9146c901d3cbd13acd. Please test and let me know. Thanks

colorcube commented 1 year ago

It works for me. Thanks