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

[BUG] Slack Delivery Method Fails with JSON::ParserError on Non-JSON Response #531

Open
3 tasks done
coatezy opened this issue Feb 21, 2025 · 0 comments
Open
3 tasks done

Comments

@coatezy
Copy link

coatezy commented Feb 21, 2025

Bug Report

Describe the Bug:
The Slack delivery method throws an error when attempting to parse a plain text response from the Slack webhook endpoint as JSON.

When setting up a Slack notification, the limited documentation and accepted configuration led me to believe that the delivery method expected the incoming_webhook.url returned during the OAuth handshake.

After further investigation and checking the default URL in the class, I found that it actually calls the https://slack.com/api/chat.postMessage endpoint, which returns a JSON response. After setting the Authorization header and specifying the channel in the JSON body, I was able to deliver a message. However:

  • It would be helpful if we could optionally use the webhook URL, as this approach requires less configuration.
  • Alternatively, the documentation should clearly state that this delivery method is designed to work with the chat.postMessage endpoint.
Failed enqueuing Noticed::EventJob to Inline(default): JSON::ParserError (unexpected character: 'ok')
↳ (pry):4:in `<main>'
JSON::ParserError: unexpected character: 'ok'
from /Users/tomcoates/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/json-2.10.1/lib/json/common.rb:245:in `parse'

To Reproduce:

  1. Configure notifier:
# frozen_string_literal: true

class TestNotifier < ApplicationNotifier
  deliver_by :slack do |config|
    config.url = -> { slack_webhook_url }
    config.json = -> do
      {
        text: "Hello, world"
      }
    end
  end

  private

  notification_methods do
    def slack_webhook_url
      # Set your webhook URL
      "https://hooks.slack.com/services/your/webhook/url"
    end
  end
end
  1. Deliver notification:
TestNotifier.deliver(User.first)

Expected Behavior:
Requests should not raise an error unless config.raise_if_not_ok = true is set. The response should be correctly processed based on its content type.

Actual Behavior:
A JSON::ParserError is raised when the response is not JSON.

Screenshots (if applicable):
Image

Environment:

  • Noticed gem version: 2.5.1, 2.6.0
  • Ruby version: 3.3.4
  • Rails version: 8.0.1
  • Operating System: macOS

Possible Fix:

  • Only call JSON.parse if the response has Content-Type: application/json.
  • Improve documentation to clarify that the delivery method is intended for chat.postMessage.

Checklist:

  • I have searched for similar issues and couldn't find any.
  • I have checked the documentation for relevant information.
  • I have included all the required information.
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

1 participant