Skip to content

JSONAPI Content-Type in error responses #17

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

Open
stevenharman opened this issue Mar 22, 2018 · 4 comments
Open

JSONAPI Content-Type in error responses #17

stevenharman opened this issue Mar 22, 2018 · 4 comments

Comments

@stevenharman
Copy link

stevenharman commented Mar 22, 2018

I'm wondering if we should be setting the Content-Type response header for JsonapiErrorable#handle_exception and JsonapiErrorable::Validatable#render_errors_for. In our app we've overwritten those two methods in our base API controller to look something like this:

  # Override JsonapiErrorable#handle_exception to ensure correct content type
  def handle_exception(*)
    self.content_type ||= Mime[:jsonapi]
    super
  end

  private

  # Override JsonapiErrorable::Validatable to ensure correct content type
  def render_errors_for(*)
    self.content_type ||= Mime[:jsonapi]
    super
  end

Here we're relying on the Mime::Type being registered - which we're getting by way of jsonapi-rails, which registers it in their Railtie. https://github.com/jsonapi-rb/jsonapi-rails/blob/14a9421658495fe916ec1886834b6adfd764b211/lib/jsonapi/rails/railtie.rb#L34-L36

But this gem could do something similar. Is there a reason not to?

@richmolj
Copy link
Contributor

Hey @stevenharman sorry for not seeing this issue earlier. It seems like we would want to do something like this globally, not just for errors, right?

@richmolj
Copy link
Contributor

Ah! Sorry I think you are saying - jsonapi-rails is handling this for normal responses, but since we avoid that gem for errors we should do it here too. Is that right? If so I agree, let's do it.

@stevenharman
Copy link
Author

@richmolj Yes, the later. jsonapi-rails is setting the Content-Type for most responses, so we get that "for free." But since we're not using that gem for error responses, the response header is not being set.

@richmolj
Copy link
Contributor

We should take your fixes and put them into this lib then, I agree. Do you want to PR for that?

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

No branches or pull requests

2 participants