In line with the ServiceStack documentation, we have a global service exception handler. The docs say that this handler should log the exception then call DtoUtils.HandleE
Do not call DtoUtils.HandleException
as it logs the error. Don't call ServiceRunner.HandleException
either, it calls DtoUtils.HandleException
.
Call DtoUtils.CreateErrorResponse
to make the response (it is used by DtoUtils.HandleException
). The ToResponseStatus
helper is also in DtoUtils
My AppServiceRunner is now like this:
public class AppServiceRunner : ServiceRunner
{
public AppServiceRunner(AppHost appHost, ActionContext actionContext)
: base(appHost, actionContext)
{
}
public override object HandleException(IRequestContext requestContext,
T request, Exception ex)
{
LogException(requestContext, request, ex);
var responseStatus = ex.ToResponseStatus();
return DtoUtils.CreateErrorResponse(request, ex, responseStatus);
}
private void LogException(IRequestContext requestContext, T request, Exception ex)
{
// since AppHost.CreateServiceRunner can be called before AppHost.Configure
// don't get the logger in the constructor, only make it when it is needed
var logger = MakeLogger();
var requestType = typeof(T);
var message = string.Format("Exception at URI:'{0}' on service {1} : {2}",
requestContext.AbsoluteUri, requestType.Name, request.ToJson());
logger.Error(message, ex);
}
private static ILog MakeLogger()
{
return LogManager.GetLogger(typeof(AppServiceRunner));
}
}
Now the only service errors that I get are those generated by this code.