简体   繁体   English

在OpenRasta处理程序中处理异常时,转换为响应的最佳方法是什么?

[英]When handling exceptions in OpenRasta handlers, what is the best way to convert to a response?

I'm using OpenRasta 2.0 to build a REST API and its extensibility is fantastic - we've managed for instance to plug-in OAuth authentication using DotNetOpenAuth fairly painlessly. 我正在使用OpenRasta 2.0构建REST API,并且它的可扩展性非常出色-例如,我们已经非常轻松地设法使用DotNetOpenAuth来插入OAuth身份验证。

However I've come to the point now where I need to define our responses for error conditions. 但是,现在我要定义错误条件的响应。 We're adopting the standards regarding HTTP error codes - but I'm concious of returning meaningful responses as well, pretty much like Twitter (the perennial example of REST) does: 我们正在采用有关HTTP错误代码的标准-但我也很想返回有意义的响应,就像Twitter(REST的长期示例)所做的那样:

{
 "error":"This method requires authentication.",
 "request":"\/1\/statuses\/followers.json"
}

Is the best way to return an OperationResult from all of our handlers, manually capture exceptions and map to a ResponseResource? 从我们所有的处理程序中返回OperationResult,手动捕获异常并映射到ResponseResource的最佳方法是吗? This seems to me to be a fair amount of overhead compared with how the rest of OpenRasta functions. 在我看来,与其余OpenRasta的功能相比,这是相当大的开销。

Or should we write some kind of contributor to capture exceptions thrown in the pipeline and globally handle the issues? 还是我们应该编写某种贡献者来捕获管道中抛出的异常并全局处理问题? Perhaps translating only exceptions of particular types (RestException?). 也许只翻译特定类型的异常(RestException?)。

Basically I'm after a feeling for what the best practice for this would be and how others have handled it. 基本上,我对这是什么最佳实践以及其他人如何处理的感觉。

Thanks. 谢谢。

EDIT: 编辑:

After looking at this for some time today I'm having trouble figuring out how to wrap the handler call - I've declared myself a OperationInterceptor derived class and have hooked that into the pipeline with ResourceSpace.Uses.PipelineContributor<OperationInterceptorContributor>() and set a custom dependency up ResourceSpace.Uses.CustomDependency<IOperationInterceptor, ExceptionHandlerInterceptor>(DependencyLifetime.PerRequest) but no matter in which of the methods I try and wrap in a try-catch, the exception still bubbles. 今天看了一段时间后,我很难弄清楚如何包装处理程序调用-我已经声明自己是OperationInterceptor派生的类,并通过ResourceSpace.Uses.PipelineContributor<OperationInterceptorContributor>()将其钩接到管道中在ResourceSpace.Uses.CustomDependency<IOperationInterceptor, ExceptionHandlerInterceptor>(DependencyLifetime.PerRequest)设置自定义依赖项ResourceSpace.Uses.CustomDependency<IOperationInterceptor, ExceptionHandlerInterceptor>(DependencyLifetime.PerRequest)但是无论我尝试将哪种方法包装在try-catch中,该异常仍然会冒泡。

Is it RewriteOperation , BeforeExecute or AfterExecute that is the most appropriate place to trap - and if so can you give me a clue as to how to start? 是最适合捕获的地方是RewriteOperationBeforeExecute还是AfterExecute如果是,您能否给我一个有关如何开始的线索?

Thanks. 谢谢。

There's a couple of things you can do to achieve what you want. 您可以做几件事来实现自己想要的。

First, you can build an IOperationInterceptor that wraps the call to your handler in a try/catch block, and assign the correct OperationResult on the ICommunicationContext. 首先,您可以构建一个IOperationInterceptor,将对处理程序的调用包装在try / catch块中,并在ICommunicationContext上分配正确的OperationResult。

Then if you want this to be serialized in json, you'll want to assign the ResponseResource property of your operationresult to a type that describes your error (let's call it "TitsUpResource" for now). 然后,如果要将此序列化为json,则需要将operationresult的ResponseResource属性分配为描述错误的类型(现在将其称为“ TitsUpResource”)。

Finally, register that type as a resource with no URI, so you can add the json codec to it 最后,将该类型注册为没有URI的资源,因此您可以向其添加json编解码器

ResourceSpace.Has.ResourcesOfType().WithoutUri.TranscodedBy or whatever else you may want. ResourceSpace.Has.ResourcesOfType()。WithoutUri.TranscodedBy或您可能想要的其他任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM