简体   繁体   English

通知客户异常

[英]Notifying client about an exception

I have a Spring MVC based application server which communicated with client through JSON. 我有一台基于Spring MVC的应用程序服务器,该服务器通过JSON与客户端通信。 one of the queries a client can perform is "do I have pending messages ?" 客户端可以执行的查询之一是“我是否有待处理的消息?” to do that, the clients create an authentication object and post it to the server. 为此,客户端创建一个身份验证对象并将其发布到服务器。 In return, the server sends back a list of pending-messages, for ex. List<PendingMessage> 作为回报,服务器将发送待处理消息列表, for ex. List<PendingMessage> for ex. List<PendingMessage>

Now, If the aren't pending messages I'm returning null. 现在,如果没有待处理的消息,我将返回null。 but the question is, how to I notify the client If I have an error, for instance, the DB connection has lost, the authentication was wrong, or any other exception I'm willing to share with the client ? 但是问题是,如何通知客户端如果发生错误,例如,数据库连接丢失,身份验证错误或我愿意与客户端共享的任何其他异常?

You can catch the different types of exceptions the code can throw. 您可以捕获代码可能引发的不同类型的异常。 Based on that you can define your own custom message & throw that to the client. 在此基础上,您可以定义自己的自定义消息并将其扔给客户端。 On the client side you have to just do a getMessage() on the Exception. 在客户端,您只需在Exception上执行getMessage()。 This should solve the problem. 这应该可以解决问题。

You can do this purely by throwing exceptions. 您可以纯粹通过引发异常来执行此操作。 SpringMVC maps exceptions to standard http status codes(200s, 400s, 500s, etc.). SpringMVC将异常映射到标准的http状态代码(200s,400s,500s等)。 Just like anytime you are coding in Java you can create custom exceptions to fit your specific needs. 就像您使用Java进行编码一样,您可以创建自定义异常来满足您的特定需求。

If you're designing both the client and server, then you probably want to define some kind of protocol, which can be fun. 如果您同时在设计客户端和服务器,那么您可能想定义某种协议,这很有趣。 You you can think of your web server as a rpc server which returns data in a defined format for given requests. 您可以将Web服务器看作是rpc服务器,该服务器以给定请求的定义格式返回数据。

For example, using HTTP status codes, a successful response might return the following data: 例如,使用HTTP状态代码,成功的响应可能会返回以下数据:

{
    status: 200,
    message: "OK",
    data: {
        name: John Doe,
        age: 31
    },
    error: null
}

A request that results in an error: 导致错误的请求:

{
    status: 400,
    message: "Bad Request",
    data: null,
    error: "Database request timed out"
}

You'd pretty much have a try catch block catching any expected exceptions which can then be transformed into an appropriate JSON response outlining the problem which can then be handled by the client. 您几乎会有一个try catch块来捕获任何预期的异常,然后可以将其转换为适当的JSON响应,概述该问题,然后由客户端进行处理。

If the request simply has an error before even reaching the servlet, the API used to make the request probably exposes an onError callback of some sort which can then be used to notify the client of the error. 如果请求甚至在到达servlet之前就只是有一个错误,则用于发出请求的API可能会公开某种onError回调,然后可将其用于通知客户端该错误。

Start with HTTP response status codes. 从HTTP响应状态代码开始。 For example, if you have DB connectivity issues, then return 500 Internal Server Error. 例如,如果您有数据库连接问题,则返回500 Internal Server Error。

This approach might not provide your client with all the information they need. 这种方法可能无法为您的客户提供他们需要的所有信息。 So, in addition, you can provide details as JSON in the response body. 因此,此外,您可以在响应正文中将详细信息作为JSON提供。 Structure this JSON however you want. 根据需要构造此JSON。 If you want the client app to go to a specific URL after receiving your response, then consider providing that URL in the response. 如果希望客户端应用在收到响应后转到特定的URL,请考虑在响应中提供该URL。

One final point: it would be bad form to combine 200 OK with an error in the response body. 最后一点:将200 OK与响应正文中的错误结合起来将是不好的形式。 I've seen this done before, and it's annoying. 我以前看过这件事,这很烦人。

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

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