JsonApiClient / json_api_client

Build client libraries compliant with specification defined by jsonapi.org
MIT License
362 stars 186 forks source link

How to keep error title *and* detail in the error message? #368

Open zackxu1 opened 4 years ago

zackxu1 commented 4 years ago

Hello.

Due to this line in the error_collector.rb, we're not able to retrieve the error detail if a request (for example save) fails.

Could this be fixed or is there an easy way to override this logic?

Thank you.

zackxu1 commented 4 years ago

I see that we have access to last_result_set.errors on the resource object, so we can access the error detail field, which the provided errors method has omitted. It would be nice to make this configurable and avoid custom code to access the detail field. Also, if we use last_result_set.errors directly in application code, is it guaranteed to work in the future? If there's a better solution, please provide details. Thank you

senid231 commented 4 years ago

currently you can monkey patch JsonApiClient::ErrorCollector::Error to make it work but it will affect all resources

class JsonApiClient::ErrorCollector::Error
  def error_key
     super # you can override error key too
  end

  def error_msg
    title
  end

In future releases I think we can expose it to JsonApiClient::Resource

something like

class Person < JsonApiClient::Resource
  def generate_error(error_object)
    [error_key, error_msg]
  end
end
samlachance commented 2 years ago

I also ran into this issue and am getting by with the monkeypatch solution.

For reference, the api that I'm interacting with uses Graphiti: https://www.graphiti.dev/

For each validation error, the title is Validation Error and the actual message is contained in the detail key.

While they may not be following the json api spec correctly, your proposed future release change would be appreciated to handle cases like this.