简体   繁体   English

RESTier提交逻辑,将错误消息推送到顶层

[英]RESTier Submit Logic, push error message to top level

I am using RESTier 0.6.0 library and would like to perform some checks on a resource before deleting it. 我正在使用RESTier 0.6.0库,并且想在删除资源之前对资源进行一些检查。 In the event that the checks fail, I am throwing an Exception to stop the delete operation. 如果检查失败,则抛出异常以停止删除操作。 However, the error message that I am using to instantiate the Exception is not getting pushed out at the highest level. 但是,我用来实例化Exception的错误消息没有在最高级别被推出。 I can only view the message as an inner exception when serving my Web API from Visual Studio. 从Visual Studio提供Web API时,我只能将消息视为内部异常。 Is there a way to get this error message to push out at the highest level? 有没有办法使此错误消息在最高级别发布?

protected void OnDeletingGw_Pack(Gw_Pack pack)
{
    var trades = ModelContext.Gw_PackJunction.Where(e => e.PackID == pack.PackID).ToList();

    if (pack.Groupage == true || trades.Count > 1)
    {
            // Don't delete a pack if it is a groupage container or if it's associated with more than one Trade.
            throw new Exception("The container you are trying to delete is either marked as groupage or is functioning as a groupage container in another file.");
    }
}

Currently any Exception thrown within the OnDelete submit logic methods in the EntityFrameworkApi results in an error which looks like this: 当前,在EntityFrameworkApi中OnDelete提交逻辑方法中引发的任何Exception都会导致出现如下错误:

{ "error":{ "code":"","message":"An error has occurred." {“ error”:{“ code”:“”,“ message”:“发生了错误。” } } }}

... which is not particularly useful to the client. ...对客户而言不是特别有用。

UPDATE: I noted in the RESTier documentation MkDocs version that an example was given where an ODataException (rather than Exception) was thrown. 更新:我在RESTier文档MkDocs版本中注意到,给出了一个抛出ODataException(而不是Exception)的示例。 I changed this in my code, but the error object returned by the published Restier service still only has the basic "An error has occurred" information. 我在代码中对此进行了更改,但是已发布的Restier服务返回的错误对象仍然仅具有基本的“发生错误”信息。

Assistance is greatly appreciated! 非常感谢您的协助!

When throwing an Exception within the RESTier EntityFrameworkApi (inside and OnUpdate<EntitySet> method for example), the exception is deserialized as part of the inner exception (internalexception). 当在RESTier EntityFrameworkApi中引发异常时(例如inside和OnUpdate<EntitySet>方法),该异常被反序列化为内部异常(internalexception)的一部分。 So in order to view any exceptions thrown within the RESTier API, one needs to... 因此,为了查看RESTier API中引发的任何异常,需要...

set the IncludeErrorDetailPolicy property on the HttpConfiguration class like this: 像这样在HttpConfiguration类上设置IncludeErrorDetailPolicy属性:

config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always

Structure of error received by client 客户收到的错误的结构

See this post: OData controller returns different error for local and different for remote machine 看到这篇文章: OData控制器为本地返回不同的错误,为远程计算机返回不同的错误

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

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