mozilla/socorro — pull request #1865 by peterbe (2014-02-06 22:58:32+00:00)
Title: Bug 969062 return more descriptive 4xx errors in json

@AdrianGaudebert r?

cc @rhelmer @twobraids

First of all, the middleware now returns errors in JSON. This is best described this way:

```
$ curl -v "http://localhost:8883/crashes/paireduuid/uuid/xxx/hang_id/xxx/" | json_print
* Adding handle: conn: 0x7f9a1a802000
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7f9a1a802000) send_pipe: 1, recv_pipe: 0
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* About to connect() to localhost port 8883 (#0)
* Trying ::1...
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8883 (#0)
> GET /crashes/paireduuid/uuid/xxx/hang_id/xxx/ HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:8883
> Accept: */*
>
< HTTP/1.1 400 Bad Request
< Content-Type: application/json; charset=UTF-8
< Transfer-Encoding: chunked
< Date: Thu, 06 Feb 2014 22:54:05 GMT
* Server localhost is not blacklisted
< Server: localhost
<
{ [data not shown]
100 99 0 99 0 0 30564 0 --:--:-- --:--:-- --:--:-- 33000
* Connection #0 to host localhost left intact
{
"error": {
"message": "Bad value for parameter(s) 'Date could not be converted extract from xxx'"
}
}
```

See, it returns a proper `400` code. And it returns it as `application/json; charset=UTF-8`.
And the response body is a valid piece of JSON.

With that in place, if you use the public API and cause the same error the error message is "carried" there too. See:

```
$ curl -v "http://socorro/api/CrashPairsByCrashId/?hang_id=xxx&uuid=xxx" | json_print
* Adding handle: conn: 0x7ff023804000
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7ff023804000) send_pipe: 1, recv_pipe: 0
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* About to connect() to socorro port 80 (#0)
* Trying 127.0.0.1...
* Connected to socorro (127.0.0.1) port 80 (#0)
> GET /api/CrashPairsByCrashId/?hang_id=xxx&uuid=xxx HTTP/1.1
> User-Agent: curl/7.30.0
> Host: socorro
> Accept: */*
>
< HTTP/1.1 400 BAD REQUEST
* Server nginx/1.4.1 is not blacklisted
< Server: nginx/1.4.1
< Date: Thu, 06 Feb 2014 22:56:18 GMT
< Content-Type: application/json; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: X-Mobile, User-Agent, Cookie
< Access-Control-Allow-Origin: *
< X-Frame-Options: DENY
< Set-Cookie: anoncsrf=X6tRCd1xwTJpEmptkEUxgh4zuQpfJEfR; expires=Fri, 07-Feb-2014 00:56:18 GMT; httponly; Max-Age=7200; Path=/
<
{ [data not shown]
100 99 0 99 0 0 244 0 --:--:-- --:--:-- --:--:-- 244
* Connection #0 to host socorro left intact
{
"error": {
"message": "Bad value for parameter(s) 'Date could not be converted extract from xxx'"
}
}
```

This is only applicable to any of the 4xx errors. If you get any 500 error you get it back in `text/plain` and any response body given by the middleware is discarded as the error is re-represented in the webapp.