Merge lp:~leonardr/lazr.restful/consistent-error-exposing into lp:lazr.restful
Status: | Merged |
---|---|
Approved by: | Tim Penhey |
Approved revision: | 183 |
Merged at revision: | 176 |
Proposed branch: | lp:~leonardr/lazr.restful/consistent-error-exposing |
Merge into: | lp:lazr.restful |
Diff against target: |
403 lines (+126/-86) 7 files modified
src/lazr/restful/_resource.py (+10/-12) src/lazr/restful/configure.zcml (+3/-11) src/lazr/restful/declarations.py (+16/-4) src/lazr/restful/docs/webservice.txt (+5/-4) src/lazr/restful/error.py (+18/-22) src/lazr/restful/interfaces/_rest.py (+2/-12) src/lazr/restful/tests/test_error.py (+72/-21) |
To merge this branch: | bzr merge lp:~leonardr/lazr.restful/consistent-error-exposing |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Tim Penhey (community) | Approve | ||
Review via email: mp+51946@code.launchpad.net |
Description of the change
Currently it's very difficult to get lazr.restful to associate a certain HTTP response code with a given exception. There are two techniques for associating a response code with an exception _class_, but it turns out these techniques actually do nothing whatsoever. To get this to work, you have to call expose() on an exception _instance_. And that only works when you the HTTP response code you want to use is 400.
This branch gets to the bottom of the problem and fixes it. Now you can use expose() to associate an exception instance with _any_ HTTP response code. (This fixes bug 631711.) You can use the @error_status decorator to set a response code for all instances of a given class. Every single exception will have its associated status code set, if it has an associated response code. Exceptions that correspond to 4xx response codes will be handled by lazr.restful; all other exceptions will be re-raised so they can be handled by the publisher (eg. retrying transient database errors or doing OOPS processing).
Here's one change we might not like:
- >>> put_request. traverse( app)() traverse( app)()
- Traceback (most recent call last):
- ...
- Unauthorized: (<Recipe object...>, 'dish', ...)
+ >>> print put_request.
+ (<Recipe object...>, 'dish', ...)
The publisher puts the name of the exception class before the string. lazr.restful doesn't do this. This wasn't a problem before because lazr.restful only handled 400 errors. Now it's handling all 4xx errors.
Possible solutions:
1. lazr.restful re-raises all but 400 errors.
2. lazr.restful re-raises *all* errors, after setting the status (effectively abdicating any responsibility for the content of the response)
3. lazr.restful sticks the name of the exception class before the string.
4. Leave it as is. 401 means "Unauthorized", so there's no loss of information as far as the client is concerned.