Сообщения об ошибках¶
MadBanner возвращает ошибки в виде JSON-документа. Типичное сообщение об ошибке содержит два поля:
error - код ошибки
message - сообщение об ошибке
Пример:
{
"error":"not_authorized",
"message":"can't decode credentials"
}
В случае, если причина ошибки - невалидный JSON документ, в ответе в поле invalid_field будет указано невалидное поле:
{
"invalid_field":"foo",
"error":"invalid_data",
"message":"unexpected field \"foo\""
}
Пример:
$ curl --request GET \
--include \
--url http://api.example.com/users/admin
HTTP/1.1 401 Unauthorized
Server: nginx/1.4.3
Date: Fri, 22 Nov 2013 05:23:42 GMT
Content-Type: application/json
Content-Length: 63
Connection: keep-alive
www-authenticate: Basic realm="MadBanner"
{
"error":"not_authorized",
"message":"can't decode credentials"
}
Ошибки авторизации¶
Сервер возвращает ошибку not_authorized в нескольких случаях.
Если заголовок Authorization
отсутствует или содержит неправильно закодированные данные:
$ curl --request GET \
--include \
--url http://api.example.com/users/admin
HTTP/1.1 401 Unauthorized
Server: nginx/1.4.4
Date: Mon, 25 Nov 2013 09:35:25 GMT
Content-Type: application/json
Content-Length: 63
Connection: keep-alive
www-authenticate: Basic realm="MadBanner"
{
"error":"not_authorized",
"message":"can't decode credentials"
}
Если пользователь от имени которого производится запрос не существует:
$ curl --request GET \
--include \
--user bob:bob \
--url http://api.example.com/users/bob
HTTP/1.1 401 Unauthorized
Server: nginx/1.4.4
Date: Mon, 25 Nov 2013 09:40:11 GMT
Content-Type: application/json
Content-Length: 61
Connection: keep-alive
www-authenticate: Basic realm="MadBanner"
{
"error":"not_authorized",
"message":"user \"bob\" not found"
}
Если указан неверный пароль:
curl --request GET \
--include \
--user admin:error \
--url http://api.example.com/users/admin
HTTP/1.1 401 Unauthorized
Server: nginx/1.4.4
Date: Mon, 25 Nov 2013 09:42:45 GMT
Content-Type: application/json
Content-Length: 53
Connection: keep-alive
www-authenticate: Basic realm="MadBanner"
{
"error":"not_authorized",
"message":"wrong password"
}
Неподдерживаемый Content-Type¶
$ curl --request POST \
--include \
--user admin:admin \
--header 'Content-Type: text/html' \
--data \
'{
"id":"alice",
"password":"wonderland",
"email":"alice@example.com",
"name":"Alice"
}' \
--url http://api.example.com/users
HTTP/1.1 400 Bad Request
Server: nginx/1.4.4
Date: Tue, 26 Nov 2013 09:48:14 GMT
Content-Type: application/json
Content-Length: 87
Connection: keep-alive
{
"error":"unsupported_content_type",
"message":"unsupported content type \"text/html\""
}
cant_decode_json
$ curl --request POST \
--include \
--user admin:admin \
--header 'Content-Type: application/json' \
--data not_json \
--url http://api.example.com/users
HTTP/1.1 400 Bad Request
Server: nginx/1.4.4
Date: Wed, 27 Nov 2013 07:07:24 GMT
Content-Type: application/json
Content-Length: 58
Connection: keep-alive
{
"error":"cant_decode_json",
"message":"can't decode json"
}