我有一个 ServiceStack REST 服务,我需要实现自定义错误处理。我已经能够通过将 AppHostBase.ServiceExceptionHandler 设置为自定义函数来自定义服务错误。
但是,对于其他类型的错误,例如验证错误,这不起作用。我怎样才能涵盖所有情况?
换句话说,我试图实现两件事:
- 为可能弹出的每种异常设置我自己的 HTTP 状态代码,包括非服务错误(验证)
- 为每种错误类型返回我自己的自定义错误对象(不是默认的 ResponseStatus)
我将如何实现这一目标?
我有一个 ServiceStack REST 服务,我需要实现自定义错误处理。我已经能够通过将 AppHostBase.ServiceExceptionHandler 设置为自定义函数来自定义服务错误。
但是,对于其他类型的错误,例如验证错误,这不起作用。我怎样才能涵盖所有情况?
换句话说,我试图实现两件事:
我将如何实现这一目标?
全局处理程序AppHostBase.ServiceExceptionHandler只处理服务异常。要处理发生在服务之外的异常,您可以设置全局AppHostBase.ExceptionHandler处理程序,例如:
public override void Configure(Container container)
{
//Handle Exceptions occurring in Services:
this.ServiceExceptionHandler = (request, exception) => {
//log your exceptions here
...
//call default exception handler or prepare your own custom response
return DtoUtils.HandleException(this, request, exception);
};
//Handle Unhandled Exceptions occurring outside of Services,
//E.g. in Request binding or filters:
this.ExceptionHandler = (req, res, operationName, ex) => {
res.Write("Error: {0}: {1}".Fmt(ex.GetType().Name, ex.Message));
res.EndServiceStackRequest(skipHeaders: true);
};
}
要创建 DTO 并将其序列化到非服务 ExceptionHandler中的响应流,您需要访问并使用正确的序列化程序来处理来自 IAppHost.ContentTypeFilters 的请求。
有关更多详细信息,请参见错误处理 wiki 页面。
我对@mythz 的回答进行了改进。
public override void Configure(Container container) {
//Handle Exceptions occurring in Services:
this.ServiceExceptionHandlers.Add((httpReq, request, exception) = > {
//log your exceptions here
...
//call default exception handler or prepare your own custom response
return DtoUtils.CreateErrorResponse(request, exception);
});
//Handle Unhandled Exceptions occurring outside of Services
//E.g. Exceptions during Request binding or in filters:
this.UncaughtExceptionHandlers.Add((req, res, operationName, ex) = > {
res.Write("Error: {0}: {1}".Fmt(ex.GetType().Name, ex.Message));
#if !DEBUG
var message = "An unexpected error occurred."; // Because we don't want to expose our internal information to the outside world.
#else
var message = ex.Message;
#endif
res.WriteErrorToResponse(req, req.ContentType, operationName, message, ex, ex.ToStatusCode()); // Because we don't want to return a 200 status code on an unhandled exception.
});
}