mehandih / grails-jaxrs

Automatically exported from code.google.com/p/grails-jaxrs
0 stars 0 forks source link

Error Not Found Message for GET /api/person/:id is in XML Format - Should be JSON #71

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1.  create a GET request for domain object with an id that is not in the 
database
2.  ensure 'application/json' is in the Accept header
3.  Notice XML tag in output ``<error>Person with id 10 not found</error>''

What is the expected output? What do you see instead?
Expected an error in JSON format - for POST/GET

What version of the product are you using? On what operating system?
plugin version 0.6 on Grails 2.1.2 on CentOS Linux

Please provide any additional information below.

# note the standard GET returns JSON but should the error be in same format ?
$ curl -v -k -H "Content-Type: application/json" -H "Accept: application/json" 
-X POST http://cobain:8080/hello/api/person -d 
'{"class":"Person","firstName":"Fabien","lastName":"Barel"}'
* About to connect() to cobain port 8080 (#0)
*   Trying 192.168.1.103... connected
* Connected to cobain (192.168.1.103) port 8080 (#0)
> POST /hello/api/person HTTP/1.1
> User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 
OpenSSL/0.9.8r zlib/1.2.3
> Host: cobain:8080
> Content-Type: application/json
> Accept: application/json
> Content-Length: 58
> 
< HTTP/1.1 201 Created
< Server: Apache-Coyote/1.1
< Location: http://cobain:8080/hello/api/person/1
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Sun, 27 Jan 2013 23:17:03 GMT
< 
* Connection #0 to host cobain left intact
* Closing connection #0
{"class":"hello.Person","id":1,"firstName":"Fabien","lastName":"Barel","middleIn
itial":null}austinmini-2:outbox saleram$ 
austinmini-2:outbox saleram$ 
austinmini-2:outbox saleram$ curl -v -k -H "Content-Type: application/json" -H 
"Accept: application/json" -X GET http://cobain:8080/hello/api/person/1
* About to connect() to cobain port 8080 (#0)
*   Trying 192.168.1.103... connected
* Connected to cobain (192.168.1.103) port 8080 (#0)
> GET /hello/api/person/1 HTTP/1.1
> User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 
OpenSSL/0.9.8r zlib/1.2.3
> Host: cobain:8080
> Content-Type: application/json
> Accept: application/json
> 
< HTTP/1.1 200 OK
< Server: Apache-Coyote/1.1
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Sun, 27 Jan 2013 23:17:12 GMT
< 
* Connection #0 to host cobain left intact
* Closing connection #0
{"class":"hello.Person","id":1,"firstName":"Fabien","lastName":"Barel","middleIn
itial":null}austinmini-2:outbox saleram$ 
austinmini-2:outbox saleram$ 
austinmini-2:outbox saleram$ curl -v -k -H "Content-Type: application/json" -H 
"Accept: application/json" -X GET http://cobain:8080/hello/api/person/10
* About to connect() to cobain port 8080 (#0)
*   Trying 192.168.1.103... connected
* Connected to cobain (192.168.1.103) port 8080 (#0)
> GET /hello/api/person/10 HTTP/1.1
> User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 
OpenSSL/0.9.8r zlib/1.2.3
> Host: cobain:8080
> Content-Type: application/json
> Accept: application/json
> 
< HTTP/1.1 404 Not Found
< Server: Apache-Coyote/1.1
< Content-Type: application/xml
< Transfer-Encoding: chunked
< Date: Sun, 27 Jan 2013 23:17:27 GMT
< 
* Connection #0 to host cobain left intact
* Closing connection #0
<error>Person with id 10 not found</error>

Original issue reported on code.google.com by alphat...@gmail.com on 27 Jan 2013 at 11:28

GoogleCodeExporter commented 8 years ago
Right, this is still hard-coded, see 
https://github.com/krasserm/grails-jaxrs/blob/master/src/groovy/org/grails/jaxrs
/provider/DomainObjectNotFoundException.groovy#L49. Will try to fix that soon.

Thanks for reporting.

Original comment by krass...@googlemail.com on 28 Jan 2013 at 6:25

GoogleCodeExporter commented 8 years ago
Bliss!

Thanks very much - I would love to contrib a fix if you wouldn't mind.

Worked w Groovy and Grails since mid 2009 version 1.1

On Monday, January 28, 2013, wrote:

Original comment by alphat...@gmail.com on 28 Jan 2013 at 7:31

GoogleCodeExporter commented 8 years ago
Could the JSON version be something like this :

{
  "message": "Domain object %s not found",
  "error": "not_found",
  "status": 404}

Original comment by alphat...@gmail.com on 29 Jan 2013 at 7:23

GoogleCodeExporter commented 8 years ago
It would be great if you can fix this and contribute. Thanks for offering help, 
highly appreciate it. Regarding the JSON, I think

{
  "message": "Domain object %s not found",
  "status": 404
}

is enough because 404 implies "error":"not_found". WDYT?

Original comment by krass...@googlemail.com on 29 Jan 2013 at 9:08

GoogleCodeExporter commented 8 years ago
That works for me.  Can the code (catching Domain at this layer simply
check the 'Accept' header and return the appropriately formatted message,
that is "<error>"  or JSON error object ?

Another (related) issue is if the :id value is non-numeric, we get the
standard Tomcat 404.html.  I believe this is a Grails-ism.  For example :

$ curl -v -k -H "Content-Type: application/json" -H "Accept:
application/json" -X GET http://cobain:8080/hello/api/person/*10x*
* About to connect() to cobain port 8080 (#0)
*   Trying 192.168.1.103... connected
* Connected to cobain (192.168.1.103) port 8080 (#0)

OpenSSL/0.9.8r zlib/1.2.3

< HTTP/1.1 404 Not Found
< Server: Apache-Coyote/1.1
< Content-Type: text/html;charset=utf-8
< Content-Length: 967
< Date: Sun, 27 Jan 2013 23:40:57 GMT
<
* Connection #0 to host cobain left intact
* Closing connection #0
<html><head><title>Apache Tomcat/7.0.30 - Error report</title><style><!--H1
{font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-s
ize:22px;}
H2
{font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-s
ize:16px;}
H3
{font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-s
ize:14px;}
BODY
{font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B
{font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;}
P
{font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px
;}A
{color : black;}A.name {color : black;}HR {color : #525D76;}--></style>
</head><body><h1>HTTP Status 404 - Not Found</h1><HR size="1"
noshade="noshade"><p>type Status report</p><p>message <u>Not
Found</u></p><p>description <u>The requested resource is not
available.</u></p><HR size="1" noshade="noshade"><h3>Apache
Tomcat/7.0.30</h3></body></html>austinmini-2:outbox saleram$

Original comment by alphat...@gmail.com on 29 Jan 2013 at 9:18

GoogleCodeExporter commented 8 years ago
I think checking the Accept header is the way to go. 

Original comment by krass...@googlemail.com on 29 Jan 2013 at 9:24