如何以显示内部服务器错误向用户? [英] How to show the internal server errors to the user?

查看:315
本文介绍了如何以显示内部服务器错误向用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个API中工作。我想扔详细的错误信息给用户。现在,我在一个情况来决定什么样的错误code应该发送或如何在应用程序中出现的任何错误解释的内部用户。例如,如果数据库连接失败,我想要什么样的HTTP状态code的发送给用户?

I am working in an API . I want to throw detailed error messages to the user. Now i am in a situation to decide what kind of error code should be sent or how to explain user if any error occurs in the application internally. For example if database connection fails , what kind of http status code i want to send to the user ?

谁能帮助?

推荐答案

这是HTTP状态code一般是指在HTTP请求本身,而不是应用程序处理请求的状态的状态。因此,大多数服务器端错误是由 500内部服务器错误覆盖。有关错误的任何其他信息应在响应正文描述。对于API的响应主体往往会JSON或XML,所以你可以使用这些格式为你的错误。事情是这样的:

An HTTP status code generally refers to the status of the HTTP request itself, not the status of the application handling the request. Therefore, most server-side errors are covered by 500 Internal Server Error. Any additional info about the error should be described in the response body. For APIs, the response body will often be JSON or XML, so you can use those formats for your errors. Something like this:

HTTP/1.1 500 Internal Server Error
[headers]

{"status":"error", "message":"The request failed due to database connectivity."}

有,但是,有两个情况下,我能想到的,您可能希望其他状态code。如果用户已经请求了未实现的API函数,你可能需要一个 501未执行,然后当有一个临时服务中断,您可以使用 503服务不可用

There are, however, two cases I can think of when you might want another status code. If the user has requested an API method that is not implemented, you might want a 501 Not Implemented, and when there is a temporary service outage, you can use 503 Service Unavailable.

有关服务器端的状态codeS 这里。

More info about server-side status codes here.

这篇关于如何以显示内部服务器错误向用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆