1

I am writing a plugin for servicestack and I want to take advantage of the error handling built in to the services. The idea is that I want to perform some logic before the route is handled, and possibly return a 403 error. I figured the way to do this was to add a RequestFilter and throw an UnauthorizedAccessException from inside of it.

This doesn't work though, and the response ends up being empty. It looks like the try/catch that would normally handle these isn't applied to RequestFilters. DtoUtils has a HandleException() method which creates an error response, but I am not sure how to properly add this to the IHttpResponse that the RequestFilter receives.

I am wondering if there is another way to add pre-route logic where you can throw exceptions like you can from inside services, or if there is a recommended way to create the error response from a RequestFilter.

Edit

I want the error returned by the Plugin to match (as closely as possible) errors returned by Exceptions thrown in a service.

DtoUtils.HandleException looks like the way SS constructs the response, but it throws a StackOverflowException when I try to call it from the plugin like this:

var error = new HttpError(HttpStatusCode.Forbidden, "UnauthorizedAccessException", message);
//_appHost is stored by the original Register() call
var result = DtoUtils.HandleException(_appHost, request, error);
response.WriteToResponse(request, result);
response.EndServiceStackRequest();
4

2 回答 2

2

You're on track with the request filter, I suspect you are just not setting up the response correctly.

I suggest checking out how the required role filter works and writing something similar.

https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.ServiceInterface/RequiredRoleAttribute.cs

res.StatusCode = (int)HttpStatusCode.Forbidden;
res.StatusDescription = "Invalid Role";
res.EndServiceStackRequest();
于 2013-05-06T17:40:37.913 回答
0

I ended up using this method, but I am not sure its the best way. It still doesn't check for a custom global error handler, which would be ideal.

response.WriteErrorToResponse(request,
                              request.ResponseContentType,
                              request.HttpMethod,
                              message,
                              new UnauthorizedAccessException(message),
                              (int)HttpStatusCode.Forbidden);
response.EndServiceStackRequest();
于 2013-05-06T20:03:46.270 回答