-
Notifications
You must be signed in to change notification settings - Fork 186
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
add possibility to override status handling #324
add possibility to override status handling #324
Conversation
3901272
to
933088f
Compare
933088f
to
a8eff74
Compare
a8eff74
to
9b63869
Compare
I love this idea! Thanks a ton for taking the time. I have some hesitation around the MyApi::Base.connection_options[:status_handlers] = -> ( code, _env ) do
throw(:handled) if code == 400
end,
-> ( code, _env ) do
# handle other code
end
# or
MyApi::Base.connection_options[:status_handlers] = handle_400, handle_404
# where handle_400 and handle_404 are lambdas But that also means that if there's more than one handler whichever one gets hit first will short circuit the entire sequence, right? Also: I have never seen I don't know if any of this feels like an issue to you, but how would you feel about making this more explicit? Something like # handlers can be a class
class 400Handler
def call( _env )
# do stuff
end
end
# or a lambda/block
404_handler = -> (_env){ raise "lol. not found" }
MyApi::Base.connection_options[:status_handlers] = { 400 => 400Handler,
404 => 404_handler } And the status middleware would look like module JsonApiClient
module Middleware
class Status < Faraday::Middleware
def cutsom_handler_for( code )
@options.fetch( :status_handlers , {} )[ :code ]
end
def custom_handled?( code )
custom_handler_for( code ).present?
end
def handle_status(code, env)
if custom_handled?( code )
return custom_handler_for( code, _env )
end
case code
# etc
end
end
end
end What do you think of that? If this is something that you think can work, I'll be more than happy to take some time to flesh it out and contribute (I don't just want to dump work on you and expect you to take it all on). That said, I agree that this something that we need and if you think that the |
sorry about the close/reopen. i am bad at laptop mouse use : ( |
9b63869
to
e4a13f0
Compare
it allows to override status handling
e4a13f0
to
a2dd3d8
Compare
@gaorlov good suggestions. Thank you for the review. I've reworked the PR. Please, take a look. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I obviously already like these changes haha!
I'm glad my suggestion inspired you to build it out this way. Thanks for taking the time!
add status_handlers to JsonApiClient::Resource.connection_options