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

Retrieving original data on error response #195

Open
tjjjwxzq opened this issue Jul 4, 2016 · 3 comments
Open

Retrieving original data on error response #195

tjjjwxzq opened this issue Jul 4, 2016 · 3 comments

Comments

@tjjjwxzq
Copy link

tjjjwxzq commented Jul 4, 2016

I'm having trouble dealing with error responses on update.

First off, it seems that the documentation is erroneous in suggesting that an JsonApiClient::ErrorCollector object will be returned, because I'm getting an ActiveModel::Errors object instead. This isn't much of an issue, just slightly strange. Also it seems that when the error response is parsed, only the source and title fields are parsed. However the spec allows for more fields in an error object (eg. details) and I think it be good to allow customizing this behavior.

Now the main issue: on unsuccessful update of a resource, the server should respond with an errors object. However this ends up overwriting the original result set which contains included data and the like. This is troublesome as we can no longer reference associations, but this is usually needed (eg. for re-rendering the edit page for a resource, which may reference associated resources).

Currently I'm having to overwrite the update method in my BaseResource to save a copy of the last result set before making the API call, and setting the last result set to be the one before the update call, once errors have been set.

class BaseResource < JsonApiClient::Resource

  # overwrite update to clone the original
  # result set before making the API call,
  # otherwise the original data will be
  # overwritten on an error response
  def update(attrs = {})
    original_result_set = last_result_set.dup
    success = super
    unless success
      self.last_result_set = original_result_set
      false
    end
  end

Is there any other way to go about it?

@adis-io
Copy link
Contributor

adis-io commented Oct 3, 2016

I also noticed that when using custom_endpoint it returns JsonApiClient::ErrorCollector

@egilburg
Copy link

Yes please! Or provide a clean way to swap json_api_client's error handler for our own.

We wrote a JSON endpoint compliant with json-api spec that returns "error" objects on both validation errors and generic client/server errors (bad route, unauthenticated, server side exceptions, etc). On the client side, we want to generically collect and log all errors based on their original http status as well as custom meta information. The behavior of raising without providing original error payload gets in the way.

@gaorlov
Copy link
Collaborator

gaorlov commented Mar 20, 2019

Is this still an issue?

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

4 participants