使用 BreezeJs executeQuery 处理 WebApi 抛出的异常(未处理的拒绝原因)
Handling thrown exceptions from WebApi with BreezeJs executeQuery (Unhandled rejection reasons)
我在我的 MVC 应用程序中使用 Breeze.js 和 Q.js。
我目前在我的 webApi 上有一个动作
public IQueryable<Myobject> doMyAction()
这可以做两件事:
- Return 对象集合(如果用户允许)
- 抛出 HTTP 异常(如果用户不允许)(可能不是 "unauthorised" ,这只是一个例子)
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Unauthorized, "Not Allowed"));
This action gets called from javascript with breeze with:
var query = EntityQuery.from('doMyAction');
// execute the query
dcCore.manager.executeQuery(query)
.then(querySucceeded)
.fail(dcCore.queryFailed);
如果用户获得授权并且逻辑正确,那么我连接到我的数据库和 return 一个很好的 Myobject() 列表,一切都很好。
但是,如果我抛出异常说他们未经授权,那么在控制台中我会收到 401(未经授权)和消息:
[Q] Unhandled rejection reasons (should be empty):
这发生在它命中 .fail() 函数之前。
我也尝试在整个执行位周围放置一个 try/catch,希望我能捕捉到错误,但没有成功。
我明白 为什么 这在理论上会发生,因为我不是 returning executeQuery 期望的东西(事实上,我不是 return 一点,我在扔!),但是,
-真题
我应该如何处理来自 webApi 的 returning 错误,而不做扩展模型之类的事情?
我唯一还没有尝试的是让我的网站 API return 成为一种动态的或通用的对象,但这并不是最好的解决方案。
你有两种方法,第一种是直接在你的查询方法中抛出一个简单的异常,比如这样:
[HttpGet]
public IQueryable<Customer> CustomersStartingWith(string companyName) {
if (companyName == "null") {
throw new Exception("nulls should not be passed as 'null'");
}
...
}
或者,如果您希望对 HTTPResponse 进行更多控制,您可以执行以下操作:
[HttpGet]
public IQueryable<Customer> CustomersWithHttpError() {
var responseMsg = new HttpResponseMessage(HttpStatusCode.NotFound);
responseMsg.Content = new StringContent("Custom error message");
responseMsg.ReasonPhrase = "Custom Reason";
throw new HttpResponseException(responseMsg);
}
请注意,对于与上述 Query 操作相反的 Save 操作,最好的方法是抛出一个 EntityErrorsException。 (参见文档),尽管抛出一个简单的异常仍然有效。
希望对您有所帮助。
我在我的 MVC 应用程序中使用 Breeze.js 和 Q.js。
我目前在我的 webApi 上有一个动作
public IQueryable<Myobject> doMyAction()
这可以做两件事:
- Return 对象集合(如果用户允许)
- 抛出 HTTP 异常(如果用户不允许)(可能不是 "unauthorised" ,这只是一个例子)
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Unauthorized, "Not Allowed"));
var query = EntityQuery.from('doMyAction');
// execute the query
dcCore.manager.executeQuery(query)
.then(querySucceeded)
.fail(dcCore.queryFailed);
如果用户获得授权并且逻辑正确,那么我连接到我的数据库和 return 一个很好的 Myobject() 列表,一切都很好。
但是,如果我抛出异常说他们未经授权,那么在控制台中我会收到 401(未经授权)和消息:
[Q] Unhandled rejection reasons (should be empty):
这发生在它命中 .fail() 函数之前。
我也尝试在整个执行位周围放置一个 try/catch,希望我能捕捉到错误,但没有成功。
我明白 为什么 这在理论上会发生,因为我不是 returning executeQuery 期望的东西(事实上,我不是 return 一点,我在扔!),但是,
-真题
我应该如何处理来自 webApi 的 returning 错误,而不做扩展模型之类的事情?
我唯一还没有尝试的是让我的网站 API return 成为一种动态的或通用的对象,但这并不是最好的解决方案。
你有两种方法,第一种是直接在你的查询方法中抛出一个简单的异常,比如这样:
[HttpGet]
public IQueryable<Customer> CustomersStartingWith(string companyName) {
if (companyName == "null") {
throw new Exception("nulls should not be passed as 'null'");
}
...
}
或者,如果您希望对 HTTPResponse 进行更多控制,您可以执行以下操作:
[HttpGet]
public IQueryable<Customer> CustomersWithHttpError() {
var responseMsg = new HttpResponseMessage(HttpStatusCode.NotFound);
responseMsg.Content = new StringContent("Custom error message");
responseMsg.ReasonPhrase = "Custom Reason";
throw new HttpResponseException(responseMsg);
}
请注意,对于与上述 Query 操作相反的 Save 操作,最好的方法是抛出一个 EntityErrorsException。 (参见文档),尽管抛出一个简单的异常仍然有效。
希望对您有所帮助。