Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error payload #186

Merged
merged 27 commits into from
Jul 29, 2016
Merged

Error payload #186

merged 27 commits into from
Jul 29, 2016

Conversation

stefanoborini
Copy link
Contributor

Fixes #175

Missing tests. Will be added.

@codecov-io
Copy link

codecov-io commented Jul 28, 2016

Current coverage is 88.69% (diff: 90.42%)

Merging #186 into master will increase coverage by 0.85%

@@             master       #186   diff @@
==========================================
  Files            36         37     +1   
  Lines          1275       1336    +61   
  Methods           0          0          
  Messages          0          0          
  Branches        121        129     +8   
==========================================
+ Hits           1120       1185    +65   
+ Misses          116        112     -4   
  Partials         39         39          

Powered by Codecov. Last update adbc9aa...869230a

@stefanoborini
Copy link
Contributor Author

Note that it's open to discussion if we should deliver a payloaded HTTPError for any exception that may occur. Right now, only REST exceptions produce an error that is potentially payloaded. Any other exception will produce a regular internal server error with no payload.

@@ -37,6 +34,5 @@ Module contents

.. automodule:: remoteappmanager.db
:members:
:special-members: __init__
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes on the doc do not seem relevant to this PR, can you create an issue separately please?

@kitchoi
Copy link
Contributor

kitchoi commented Jul 28, 2016

Alternative to catching RESTException and raising a custom PayloadedHTTPError, RESTBaseHandler.write_error can recognise that the Exception is a RESTException and compose the payload accordingly.

@stefanoborini
Copy link
Contributor Author

@kitchoi I already thought about that and I don't like it, because in write_error you have no knowledge of which methods was actually invoked.

self.clear_header('Content-Type')
self.finish()

def rest_to_http_exception(self, rest_exc):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Class method for PayloadedHTTPError?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. In the future, the conversion will be done by a serializer that may produce xml instead of json. This serializer will be a instance var.

@stefanoborini stefanoborini changed the title Error payload [WIP] Error payload Jul 28, 2016
content_type = "text/plain"
else:
if content_type is not None:
raise ValueError("Content type specified, but no payload")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would anyone say "application/json" and then provide no payload?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not what you would expect, but does it actually cause problem? If not, seem to be an unnecessary potential failure here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not unnecessary. The class constraints are clear. if you specify the payload, you can or cannot specify the content type. If you don't, it's by default text/plain. If you don't specify a payload, there's no reason for specifying the content type, and if you do, you are doing something very wrong, because it means that your payload is None by accident.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it wrong to set the header content-type to something but provide an empty content?
(by the way you could default payload to an empty string, just an idea)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a difference between empty payload an no payload. some http responses don't want payload, and it's invalid if they do provide one (e.g. 204 No content, or 404 Not Found)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least in the context of REST, I mean. 404 can have a payload in non-rest context

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No payload but has content-type header - is it just because it is wrong in terms of definition or does it cause error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it causes an error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you explain how please? I just wanted to understand.

@kitchoi
Copy link
Contributor

kitchoi commented Jul 29, 2016

Just a comment, instead of raising an Error, one could also call send_error and pass keyword arguments to it, the keyword arguments can then be passed to write_error. These two methods are designed to be extendable. So for example, one could instead of raising PayloadedHTTPError, call send_error(status_code, ..., payload='...')

@itziakos
Copy link
Member

👍

@itziakos itziakos merged commit 5106d87 into master Jul 29, 2016
@itziakos itziakos deleted the 175-error-payload branch July 29, 2016 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants