使用 servicestack 捕获异常 [英] Catching exceptions with servicestack
问题描述
我们已经将 ServiceStack 用于基于 REST 的服务已经有一段时间了,到目前为止效果非常好.
We have been using ServiceStack for REST based services for a while now and so far it has been amazing.
我们所有的服务都写成:
All of our services have been written as:
public class MyRestService : RestService<RestServiceDto>
{
public override object OnGet(RestServiceDto request)
{
}
}
对于每个 DTO,我们都有 Response 等效对象:
For each DTO we have Response equivalent object:
public class RestServiceDto
{
public ResponseStatus ResponseStatus {get;set;}
}
它会处理所有抛出的异常.
which handles all the exceptions should they get thrown.
我注意到如果在 OnGet()
或 OnPost()
方法中抛出异常,那么 http 状态描述包含异常类的名称,其中好像我扔了一个:
What I noticed is if an exception is thrown in the OnGet()
or OnPost()
methods, then the http status description contains the name of the exception class where as if I threw a:
new HttpError(HttpStatus.NotFound, "Some Message");
然后 http 状态描述包含文本Some Message".
then the http status description contains the text "Some Message".
由于一些其余服务正在抛出异常而其他服务正在抛出 new HttpError()
,我想知道是否有一种方法可以在不更改我所有 REST 服务的情况下捕获任何异常并抛出 <代码>new HttpError()?
Since some of the rest services are throwing exceptions and others are throwing new HttpError()
, I was wondering if there was a way without changing all my REST services to catch any exceptions and throw a new HttpError()?
例如,如果 OnGet()
方法抛出异常,那么捕获它并抛出一个 new HttpError()
?
So for example, if the OnGet()
method throws an exception, then catch it and throw a new HttpError()
?
推荐答案
使用旧 API - 继承自定义基类
当你使用旧的 API 来处理一般的异常时,你应该提供一个自定义基类并覆盖 HandleException 方法,例如:
Using Old API - inherit a custom base class
As you're using the old API to handle exceptions generically you should provide a Custom Base class and override the HandleException method, e.g:
public class MyRestServiceBase<TRequest> : RestService<TRequest>
{
public override object HandleException(TRequest request, Exception ex)
{
...
return new HttpError(..);
}
}
然后利用自定义错误处理让您的所有服务都继承您的类,例如:
Then to take advantage of the custom Error handling have all your services inherit your class instead, e.g:
public class MyRestService : MyRestServiceBase<RestServiceDto>
{
public override object OnGet(RestServiceDto request)
{
}
}
使用新 API - 使用 ServiceRunner
否则,如果您正在使用 ServiceStack 改进的新 API,那么您就不会'不需要让所有服务都继承一个基类,相反,您可以通过覆盖 CreateServiceRunner 来告诉 ServiceStack 在您的 AppHost 中使用自定义运行器:
Using New API - use a ServiceRunner
Otherwise if you're using ServiceStack's improved New API then you don't need to have all services inherit a base class, instead you can just tell ServiceStack to use a custom runner in your AppHost by overriding CreateServiceRunner:
public override IServiceRunner<TRequest> CreateServiceRunner<TRequest>(
ActionContext actionContext)
{
return new MyServiceRunner<TRequest>(this, actionContext);
}
其中 MyServiceRunner 只是一个实现您感兴趣的自定义钩子的自定义类,例如:
Where MyServiceRunner is just a just custom class implementing the custom hooks you're interested in, e.g:
public class MyServiceRunner<T> : ServiceRunner<T> {
public override object HandleException(IRequestContext requestContext,
TRequest request, Exception ex) {
// Called whenever an exception is thrown in your Services Action
}
}
这篇关于使用 servicestack 捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!