要設計些Restful API瞳秽,試圖看看業(yè)界對錯誤返回有沒有標準√湎兀看了幾個:
https://developers.douban.com/wiki/?title=api_v2
https://github.com/FanfouAPI/FanFouAPIDoc/wiki
https://developer.github.com/v3/
http://open.weibo.com/wiki/API%E6%96%87%E6%A1%A3_V2
https://dev.twitter.com/rest/public/search
douban
{
"msg":"uri_not_found",
"code":1001,
"request":"GET \/v2\/photo\/132"
}
fanfou
{
"request":"/account/show.json",
"error":"參數錯誤"
}
{"error":"Sorry, your query is too complex. Please reduce complexity and try again."}
github
{
"message": "Bad credentials",
"documentation_url": "https://developer.github.com/v3"
}
{
"request" : "/statuses/home_timeline.json",
"error_code" : "20502",
"error" : "Need you follow uid."
}
結論:沒有統(tǒng)一標準材原。大家隨便玩兒吧。
大多數使用了error表示錯誤信息谭羔,有的用error_code表示錯誤編號华糖。error_code這個東西,某些程度上可以幫助多語言化瘟裸。但是也會使得api很累贅客叉,而且……編號也很麻煩,編了號還得查表话告。直接用英文的error信息是比較方便的兼搏。多語言時就翻譯一下。
這樣說來沙郭。我選擇twitter的表達方式佛呻。