I’d like to stop internal exceptions from reaching the client. For example if an ApplicationException is thrown deep in the call stack it doesn’t make sense to return that to the client.
One approach I’ve been trying out is is to only return exceptions that are thrown from a Service
, like this:
ServiceExceptionHandlers.Add((httpReq, request, exception) =>
{
if (exception.TargetSite == null || !exception.TargetSite.DeclaringType.HasInterface(typeof(IService)))
return new HttpError(HttpStatusCode.InternalServerError)
{
StatusDescription = "ServerError",
Response = new ErrorResponse
{
ResponseStatus = new ResponseStatus("ServerError", "Unexpected error. Sorry!")
}
};
return null;
});
Alternatively I’ve considered creating my own custom exception hierarchy where all exceptions that should be returned to the client are a ServiceError
. Other exception types will be hidden and an HttpError
will be thrown instead, as above.
Has anyone used either of these approaches or something else to achieve the same result?