在从REST API返回错误时,我正在寻找有关良好实践的指导.我正在开发一个新的API,所以我现在可以采取任何方向.我的内容类型目前是XML,但我计划将来支持JSON.
我现在正在添加一些错误情况,例如客户端尝试添加新资源但已超出其存储配额.我已经使用HTTP状态代码处理某些错误情况(401用于身份验证,403用于授权,404用于普通错误请求URI).我查看了有福的HTTP错误代码,但400-417范围似乎没有报告特定于应用程序的错误.所以起初我很想用200 OK和特定的XML有效载荷返回我的应用程序错误(即付给我们更多,你将得到你需要的存储空间!)但是我停下来思考它并且似乎是肥皂(/耸耸肩恐怖).此外,感觉就像我将错误响应分成不同的情况,因为有些是http状态代码驱动而其他是内容驱动.
那么行业建议是什么?好的做法(请解释原因!)以及从客户端pov中,REST API中的哪种错误处理使客户端代码的生活更轻松?
我使用以下jQuery JSONP请求通过URL检查资源的状态.当资源不可用或服务器关闭时,我的ajaxFail()功能会更新显示.
function fetchServerStatus(service, host)
{
var port = serverStatus[service].port;
$.ajax({
url: "http://" + host + ":" + port + "/admin/server/status",
dataType: "jsonp",
timeout: 1000,
error: ajaxFail,
fail: ajaxFail,
success: ajaxDone,
done: ajaxDone,
complete: ajaxAlways,
always: ajaxAlways
});
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,尽管声明了处理程序,fail并且error没有将任何内容记录到控制台,jQuery总是在控制台中记录失败的请求.如果资源长时间不可用,则会构建一个巨大的控制台日志,最终导致浏览器进程崩溃(在我的情况下为Chrome).
有没有办法阻止它这样做?
我根据本教程制作了一个联系表单:http: //blog.teamtreehouse.com/create-ajax-contact-form
我在我的服务器上使用PHP版本5.3.10-1ubuntu3.4,我遇到http_response_code();了上述链接中的示例教程使用的问题.我http_response_code();只读过PHP 5.4.所以相反,我已经恢复使用header();.
我的表单工作得很好,当我提交时它显示成功消息,而不是我使用时的错误,http_response_code();但我的PHP不是很好,我想知道我所做的是否可以接受或者我应该是以不同的方式做到这一点?如果是这样,请更正我的代码.
这是我的mailer.php文件的内容,你可以看到我已经注释掉http_response_code();并且正在使用它header();.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form fields and remove whitespace.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$phone = trim($_POST["phone"]);
$company = trim($_POST["company"]);
$minbudget = trim($_POST["minbudget"]);
$maxbudget = trim($_POST["maxbudget"]);
$message = trim($_POST["message"]);
$deadline = trim($_POST["deadline"]);
$referred = trim($_POST["referred"]);
// Check that data was sent to the mailer.
if ( empty($name) …Run Code Online (Sandbox Code Playgroud) 在 jenkins 上成功构建后,我正在尝试将我的 Maven 项目上传到 Nexus。这是我的两种配置:
这里出现错误
Failed to deploy artifacts:
Could not transfer artifact de.xxx:NettyCore:pom:1.0-20170508.212447-1 from/to cloud-repo (http://xxx:xxx/nexus/content/repositories/cloud-repo):
Failed to transfer file: http://xxx:xxx/nexus/content/repositories/cloud-repo/de/xxx/NettyCore/1.0-SNAPSHOT/NettyCore-1.0-20170508.212447-1.pom.
Return code is: 400, ReasonPhrase:Bad Request.
ERROR: Uploading file pom.xml failed.
Finished: FAILURE
Run Code Online (Sandbox Code Playgroud) 我正在尝试实现数据验证,其中规则是使用 Google Sheets API 的范围之一。
在 sheet1 中,我有一个主列表,其中一列需要位于其中一个值中。可能的下拉值位于称为下拉列表的单独工作表中。
我的 one_of_range 条件值的错误是什么?
dropdown_action = {
'setDataValidation':{
'range':{
'startRowIndex':1,
'startColumnIndex':4,
'endColumnIndex':5
},
'rule':{
'condition':{
'type':'ONE_OF_RANGE',
'values': [
{ "userEnteredValue" : "dropdown!A1:B2"
}
],
},
'inputMessage' : 'Choose one from dropdown',
'strict':True,
'showCustomUi': True
}
}
}
request = [dropdown_action]
batchUpdateRequest = {'requests': request}
SHEETS.spreadsheets().batchUpdate(spreadsheetId = id,
body = batchUpdateRequest).execute()
Run Code Online (Sandbox Code Playgroud)
但是,我遇到了http错误。如果我选择列表之一而不是 one_of_range,我就能让它工作。但我更喜欢使用 one_of_range 以便我可以在同一个电子表格中维护可能的值。
请求https://sheets.googleapis.com/v4/spreadsheets/id:batchUpdate?alt=json时出现 HttpError 400返回“无效请求 [1].setDataValidation: Invalid ConditionValue.userEnteredValue: dropdown!A1:B2">
我在inner_hits使用 Python 弹性搜索访问数据时遇到了问题。我越来越
RequestError(400,'search_phase_execution_exception', 'failed to create query'
当我尝试使用inner_hits{}.
我的弹性搜索版本 6.5.4,python 版本 3.7.2。
from elasticsearch import Elasticsearch
es = Elasticsearch()
mapping = '''{
"mappings": {
"tablets": {
"properties": {
"Names": {
"type": "nested"
"properties":{
"ID": {"type" : "long"},
"Combination": {"type" : "text"},
"Synonyms": {"type" : "text"}
}
}
}
}
}
}'''
es.indices.create(index="2", ignore=400, body=mapping)
tablets = {
"Names":[
{
"ID" : 1,
"Combination": "Paracetamol",
"Synonyms": "Crocin"
},{
"ID" : 2,
"Combination": "Pantaprazole",
"Synonyms": "Pantap"
}]} …Run Code Online (Sandbox Code Playgroud)