-
Notifications
You must be signed in to change notification settings - Fork 463
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
Apiepie::ParamErrors Inconsistent interface #890
Comments
I am thinking this might have happened due to a refactor. probably best to update the documentation, unless we're really missing out on something... |
Might be related to my change with returning multiple errors, so either we need to reconsider that change or update the documentation. |
@davidwessman Can you update the documentation ? |
EDIT Now I realized the point about the different interfaces of Previous answer@mathieujobin I reviewed the README and find that it has been updated to this syntax: ```ruby # ParamError is superclass of ParamMissing, ParamInvalid rescue_from Apipie::ParamError do |e| render text: e.message, status: :unprocessable_entity end ```using @wozza35 Where did you find the This is my suggestion to handle all the errors, but I am not sure if it should be in the README or not. rescue_from Apipie::ParamError do |e|
errors = if e.is_a?(Apipie::ParamMultipleMissing)
e.params.to_h {|p| [p.name, "has an error"]}
else
{e.param => "has an error"}
end
render json: errors, status: :unprocessable_entity
end |
So it seems like these errors have always used different interfaces for the params.
apipie-rails/lib/apipie/validator.rb Lines 75 to 77 in 7cc859e
apipie-rails/lib/apipie/validator.rb Lines 41 to 49 in 7cc859e
apipie-rails/lib/apipie/validator.rb Lines 359 to 368 in 7cc859e
So it seems like it would make sense the change the |
Inconsistent interface isn't ideal, I convey, but considering the little audience of this gem. I'm not sure its worth fixing. I guess if enough people vote that its worth it. otherwise, I think this is really minor. or at least, I don't see what exact problem is causes. |
Hello, Thanks for your work. I've recently run into this issue. For me the problem it causes is the following: I'm working on an API that accepts nested params and rescue I agree this is a minor issue and I do have a workaround (implementing custom wrapper validators to throw errors with the SuggestionHere is my suggestion to implement the change in a (more or less) non-breaking way:
I'd be happy to submit a PR if one of the proposed options is acceptable. |
@PanosCodes Do you have an opinion on the above? |
@baarde I think it makes sense, but maybe it would be easier to see some code examples and some tests? Would be great if you could draft something up 🙂 |
It is suggested in the documentation to rescue both
ParamMissing
andParamInvalid
exceptions by rescuing from theApipie::ParamError
superclass. However, the interface of these two sub-classes is different.For example, I'm trying to return a custom error JSON message in the case of a param error:
ParamMissing#param
returns an instance ofApipie::ParamDescription
, whereasParamInvalid#param
returns a symbol. So, If the parameter is not an Array:If the parameter is missing:
I realize I can rescue the error classes separately or make use of meta programming, but it seems like this might be unintentional behaviour?
The text was updated successfully, but these errors were encountered: