Hi Christoph, it worked for me when putting the rest_errors.xqm in the webapp folder and rewrriting your import in this way:
import module namespace err="error" at "rest_errors.xqm";
Trying 127.0.0.1... * Connected to localhost (127.0.0.1) port 9984 (#0)
GET /softwareberatung/rev/2017/12 HTTP/1.1 Host: localhost:9984 User-Agent: curl/7.47.0 Accept: */*
< HTTP/1.1 404 Not Found < Set-Cookie: JSESSIONID=6ylp1ounz561183u198394hjs;Path=/ < Expires: Thu, 01 Jan 1970 00:00:00 GMT < Content-Type: text/plain;charset=UTF-8 < Content-Length: 25 < Server: Jetty(8.1.18.v20150929) < * Connection #0 to host localhost left intact Revenue 12 does not exist
Working with the repo is something that I never got right but probably I should just carefully re-read the docs at [1]. Regards, Marco.
[1] http://docs.basex.org/wiki/Repository
On 05/03/2017 13:45, Christoph Gaukel wrote:
Hi Marco,
thank you for your reply. It works fine, when I put the declaration direct into my rest-module:
module namespace page = 'rev';
declare %rest:error("*:notfound") %rest:error-param("description", "{$description}") function page:not-found-error($description as xs:string) { rest:response <http:response status="404"> <http:header name="Content-type" value="text/plain"/> </http:response> </rest:response>, $description };
declare %rest:path("softwareberatung/rev/{$year}/{$id}") %rest:GET %output:method("json") function page:get-revenue( $year, $id ) { error(xs:QName('err:notfound'), 'Revenue ' || $id || ' does not exist') };
but not if I put this code into module rest_errors.xqm:
module namespace err = "error"; declare namespace rest = "http://exquery.org/ns/restxq"; declare namespace http = "http://expath.org/ns/http-client";
declare %rest:error("*:notfound") %rest:error-param("description", "{$description}") function err:not-found-error($description as xs:string) { rest:response <http:response status="404"> <http:header name="Content-type" value="text/plain"/> </http:response> </rest:response>, $description };
and import the module:
module namespace page = 'rev'; import module namespace err="error" at "../repo/rest_errors.xqm";
declare %rest:path("softwareberatung/rev/{$year}/{$id}") %rest:GET %output:method("json") function page:get-revenue( $year, $id ) { error(xs:QName('err:notfound'), 'Revenue ' || $id || ' does not exist') };
This returns status code 400 instead of 404!
Regards
Christoph
Am 05.03.2017 um 12:31 schrieb Marco Lettere:
Hi Chrostoph, the best way I've found out to organize error responses in a RestXQ context is to exploit BaseX built-in error interceptors. [1] What we usually do is write a module with interceptors (basically annotated functions ) like the following:
module namespace e = "urn:myerr";
declare %rest:error("*:notfound") %rest:error-param("description", "{$description}") function e:login-error($description as xs:string) { rest:response <http:response status="404"> <http:header name="Content-type" value="text/plain"/> </http:response> </rest:response>, $description
};
Here we just use rest:response to hand-craft the response we need when an exception with the annotated code (*:notfound) is generated (and not catched before). In your case rewriting your throwing like the following would then be catched by the above interceptor.
fn:error(xs:QName('err:notfound'), 'Invoice ' || $id || ' does not exist').
You can intercept with the proper annotations all information you usually have in an exception handler (line number, value, stack trace, ...) and this can be exploited, for example, in order to make the status of the response dynamic. You could pass it into the error interceptor with the value field for instance.
Hope you can move ahead with this info. Regards, Marco.
[1] http://docs.basex.org/wiki/RESTXQ#Error_Handling
On 05/03/2017 07:33, Christoph Gaukel wrote:
Hi,
i tried to return http-status 404, but my service returns status 400:
This is the code in my XQuery:
fn:error(xs:QName('error'), 'Invoice ' || $id || ' does not exist', 404)
This is what curl prints:
- Trying 127.0.0.1...
- Connected to localhost (127.0.0.1) port 8984 (#0)
GET /softwareberatung/revenues/2015/15-123 HTTP/1.1 Host: localhost:8984 User-Agent: curl/7.47.0 Accept: */*
< HTTP/1.1 400 Bad Request < Set-Cookie: JSESSIONID=cfedc7x61od51p8o40uxm8dfe;Path=/ < Expires: Thu, 01 Jan 1970 00:00:00 GMT < Content-Type: text/plain;charset=UTF-8 < Content-Length: 97 < Server: Jetty(8.1.18.v20150929) < Stopped at /home/christoph/basex/restxq/Revenue.xqm, 64/19:
- Connection #0 to host localhost left intact
[error] Invoice 15-123 does not exist
Regards
Christoph Gaukel
basex-talk@mailman.uni-konstanz.de