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

Add public API to set last error #352

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ryoqun
Copy link

@ryoqun ryoqun commented Aug 10, 2016

Add public API to set last error.

We're mainly using rails as a API backend server. So, it's a bit pointless to return an error page response instead of normal JSON responses for API endpoints.

However we want to use better_errors when an exception is raised via http://localhost:3000/__better_errors

For that purpose, we only want to pass the error to BetterError from our rails application for later investigation like this:

class Api::CommonController < ActiveController::Base
  rescue_from StandardError, with: :fallback_error_handler

  ...

  def fallback_error_handler
    ...
    if Rails.env.development?
      render status: 500, json: {success: false, error: error.message, show_better_errors: true}
      BetterErrors.set_last_error(error)
    end
    ...
  end

  ...
end

@ryoqun ryoqun changed the title Last error api Add public API to set last error Aug 10, 2016
@ryoqun
Copy link
Author

ryoqun commented Aug 14, 2016

@charliesome Hi, if this API addition is deemed good, I'll finish up this pull request (adding specs, docs, etc). Could you take a look at this?

@RobinDaugherty
Copy link
Member

RobinDaugherty commented Jul 30, 2017

@ryoqun this looks like it can be a useful change, and I'm interested in completing it if possible.

I've found that when developing Javascript, the XHR responses from Better Errors are useful to developers looking at the Network tab of the developer tools/inspector. However, the fact that the body is not valid JSON causes an exception when the body is parsed. If developers are doing a good job with the frontend code, they're adding exception-handling that will react correctly when the response has a 500 status or is not valid JSON.

It might be useful for Better Errors to respond with valid JSON when it was requested, but I don't think it would be good for the front end to rely on a specific, structured "error" response from the back end: in the real world, sometimes the back end is actually broken.

Would it be helpful for Better Errors to respond with a JSON body instead of only text?

@RobinDaugherty RobinDaugherty added this to the v3.0 milestone Jul 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants