Skip to content

Include exception message and backtrace in response#524

Merged
lgebhardt merged 1 commit intoJSONAPI-Resources:masterfrom
iamvery:exception-response
Nov 20, 2015
Merged

Include exception message and backtrace in response#524
lgebhardt merged 1 commit intoJSONAPI-Resources:masterfrom
iamvery:exception-response

Conversation

@iamvery
Copy link
Copy Markdown
Contributor

@iamvery iamvery commented Nov 5, 2015

Following #502, this is a simple potential solution to communicating exception information in non-prod.

It bothers me a bit to submit a change that doesn't have a test, but wasn't clear where such a test would exist as the response of internal server errors doesn't appear to be tested. To that end, this is something of a development support feature and presumably as long as it doesn't break production functionality it should be safe to ship.

TIL

One thing I did realize while I was working on this is that the backtrace produced by the error is logged to log/test.log. So one could conceivably monitor this file if they encountered an unexpected error in test. I must have failed to realize this before simple because it's not my typical workflow. Maybe that's not true for others?


So, what do you think? Is this useful?

@lgebhardt
Copy link
Copy Markdown
Contributor

This does seem useful. However it doesn't look like the spec allows for the exception and backtrace keys as valid members of the error object. @dgeb can you provide some guidance? We could certainly stuff them into the meta key.

@iamvery
Copy link
Copy Markdown
Contributor Author

iamvery commented Nov 17, 2015

Ah good call. The meta key would probably suffice.

Interestingly since I created this PR I've been tailing log/test.log somewhat happily... Worth fixing up?

@lgebhardt
Copy link
Copy Markdown
Contributor

@iamvery I see it as worthwhile. It's sometimes slightly difficult to tail a log on a qa server.

@iamvery
Copy link
Copy Markdown
Contributor Author

iamvery commented Nov 18, 2015

👍 I'll try to get an update in soon. Thanks for the feedback!

@dgeb
Copy link
Copy Markdown
Contributor

dgeb commented Nov 18, 2015

Just chiming in here to support using meta. The spec doesn't allow custom members in the top-level of an error object.

@iamvery
Copy link
Copy Markdown
Contributor Author

iamvery commented Nov 18, 2015

❤️

@iamvery
Copy link
Copy Markdown
Contributor Author

iamvery commented Nov 19, 2015

I just pushed an update that moves this information to meta. Let me know if you'd like anything else done.

@lgebhardt
Copy link
Copy Markdown
Contributor

@iamvery Thanks for patching this up. But I have a few suggestions. I want to avoid having the meta fields output if they are nil. I also think by passing the meta option into Error we can reuse it for other exceptions easily.

No need to do this now, but I'm thinking I may want to create a configuration option to control whether these are output. I haven't decided if an option is overkill at this point.

* When in a non-prod environment it is useful to include more
  information about the error for the purpose of debugging. Imagine you
  are test-driving an acceptance test and you hit a 500 error. There
  would be no way to tell what caused the problem unless you tailed
  log/test.log. Similar to Rails' special error pages in non-prod
  environments, this gives you a little more context in the response.
* It is important, however, that this information not be included in
  production. Doing so might leak internal details about your app a
  create a security concern.
@iamvery
Copy link
Copy Markdown
Contributor Author

iamvery commented Nov 20, 2015

Thanks for the feedback! The PR has been updated. I'm thinking that meta being nil in the case of prod will omit it from serialization due to https://github.com/cerebris/jsonapi-resources/blob/master/lib/jsonapi/resource_serializer.rb#L134. Is that right?

@lgebhardt
Copy link
Copy Markdown
Contributor

Thanks @iamvery. The errors do not run through that code. They are actually just output as a hash in https://github.com/cerebris/jsonapi-resources/blob/master/lib/jsonapi/response_document.rb#L102 so the nil parameters come through as nils. So by adding meta to the base Error object you are outputting meta to every error, which is what I wanted.

lgebhardt added a commit that referenced this pull request Nov 20, 2015
Include exception message and backtrace in response
@lgebhardt lgebhardt merged commit a88b7e7 into JSONAPI-Resources:master Nov 20, 2015
@xdmx
Copy link
Copy Markdown

xdmx commented Nov 21, 2015

Shouldn't it be Rails.env.production? instead of Rails.env.prod?

@iamvery
Copy link
Copy Markdown
Contributor Author

iamvery commented Nov 21, 2015

Hah! Good catch. Tests, amitite? I'll send a patch unless someone else gets to it 😆

@iamvery iamvery deleted the exception-response branch November 21, 2015 16:27
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