Skip to content

task: allow handler subclasses to call Grape's error! method #49

Closed
@GrantBirki

Description

@GrantBirki

This task tracks work to allow handler subclasses to access Ruby Grape's error! method. Being able to access this method from subclasses would be extremely useful for this project. For example:

# frozen_string_literal: true

class BoomtownWithError < Hooks::Plugins::Handlers::Base
  def call(payload:, headers:, env:, config:)

    if payload["boom"] == true
      log.error("boomtown error triggered by payload: #{payload.inspect} - request_id: #{env["hooks.request_id"]}")

      # TODO: Get Grape's `error!` method to work with this
      error!({
        error: "boomtown_with_error",
        message: "the payload triggered a boomtown error",
        request_id: env["hooks.request_id"]
      }, 500)
    end

    return { status: "ok" }
  end
end

This would allow subclasses to panic out of certain operations in any way they see fit, rather than just raising something like StandardError and having it be morphed into a 500 with a generic server_error message.

Right now, there are a few files that have stubs in place with TODO comments to finish implementation of this:

  • spec/acceptance/plugins/handlers/boomtown_with_error.rb is a handler in the acceptance test suite that calls error! when the payload has a certain attribute set.
  • spec/acceptance/acceptance_tests.rb has an acceptance test describe block called boomtown_with_error which contains the tests required. The commented out test needs to be fixed as it currently displays # 1) Hooks endpoints boomtown_with_error sends a POST request to the /webhooks/boomtown_with_error endpoint and it explodes # Failure/Error: expect(response.body).to include(expected_body_content) if expected_body_content #expected "{\"error\":\"server_error\",\"message\":\"undefined method 'error!' for an instance of BoomtownWithE...thread_pool.rb:167:in 'block in #Puma::ThreadPool#spawn_thread'\",\"handler\":\"BoomtownWithError\"}" to include "the payload triggered a boomtown error" # ./spec/acceptance/acceptance_tests.rb:28:in 'RSpec::ExampleGroups::Hooks#expect_response' # ./spec/acceptance/acceptance_tests.rb:501:in 'block (4 levels) in <top (required)>' but this is due to the error! method not be available in handler subclasses.

So what needs to be done? The handler subclass needs a way to be able to call Ruby Grape's error! method so that handler classes can instantly quit processing an HTTP request and return whatever error they need to. The Ruby Grape error! method supports a few forms so we should continue to honor that as well:

  • error!({ error: "boomtown_with_error", message: "the payload triggered a boomtown error", request_id: env["hooks.request_id"] }, 500)
  • error!('401 Unauthorized', 401)
  • etc

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions