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

add possibility to override status handling #324

Merged
merged 1 commit into from
Jan 24, 2019

Conversation

senid231
Copy link
Member

add status_handlers to JsonApiClient::Resource.connection_options

@senid231 senid231 self-assigned this Jan 11, 2019
@gaorlov
Copy link
Collaborator

gaorlov commented Jan 23, 2019

I love this idea! Thanks a ton for taking the time. I have some hesitation around the throw interface, however. I see that custom_handlers is intended to act as an array; how would I define more than
one? Would it look something like

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 throw as a sign of success; is this common practice?

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 throw(:handle) paradigm is common enough that people will have no troubles with it I'm fine with that too.

@gaorlov gaorlov closed this Jan 23, 2019
@gaorlov gaorlov reopened this Jan 23, 2019
@gaorlov
Copy link
Collaborator

gaorlov commented Jan 23, 2019

sorry about the close/reopen. i am bad at laptop mouse use : (

@senid231
Copy link
Member Author

@gaorlov good suggestions. Thank you for the review.

I've reworked the PR. Please, take a look.

Copy link
Collaborator

@gaorlov gaorlov left a 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!

lib/json_api_client/middleware/status.rb Outdated Show resolved Hide resolved
@senid231 senid231 merged commit 4ca6148 into JsonApiClient:master Jan 24, 2019
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

Successfully merging this pull request may close these issues.

2 participants