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

Adding custom errors to a model based on server response #484

Open
samstarling opened this issue Feb 2, 2018 · 2 comments
Open

Adding custom errors to a model based on server response #484

samstarling opened this issue Feb 2, 2018 · 2 comments

Comments

@samstarling
Copy link

Hi @zacharywelch @edtjones – I've written some middleware to try and handle custom errors, but I can't seem to get it to work, because I'm not sure what the format of env[:body][:errors] should be:

class MyErrorMiddleware < Her::Middleware::DefaultParseJSON
  def on_complete(env)
    if env[:status] == 400
      super
      env[:body][:errors] = ["Hello"] # in reality, parse errors
    else
      super
    end
  end
end

The API returns something like this:

{
  errorCode: 123
  errorText: "INVALID_OPERATION"
}

When I use the above middleware, I don't get any errors in the ActiveModel::Errors object on my model.errors, but I do see the response JSON showing up in it's @base variable when I call .errors.inspect. Am I doing something wrong?

@zacharywelch
Copy link
Collaborator

You could try following #101 and see if that helps. The method's been renamed as store_response_errors

@samstarling
Copy link
Author

@zacharywelch Thanks – I'll have a read. Is it possible to use store_response_errors with validates? I have some basic "presence" validations on my model, but I also want to be able to handle errors from the API in a custom manner.

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