Sha256: 80940cb42359a06e5cb5abc7ea5006808dbabcf82fe11aebe357f5bfb3ca780c

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

require 'httparty'

module ExceptionNotifier
  class MattermostNotifier < BaseNotifier
    def call(exception, opts = {})
      options = opts.merge(base_options)
      @exception = exception

      @formatter = Formatter.new(exception, options)

      @gitlab_url = options[:git_url]

      payload = {
        text: message_text.compact.join("\n"),
        username: options[:username] || 'Exception Notifier'
      }

      payload[:icon_url] = options[:avatar] if options[:avatar]
      payload[:channel] = options[:channel] if options[:channel]

      httparty_options = options.except(
        :avatar, :channel, :username, :git_url, :webhook_url,
        :env, :accumulated_errors_count, :app_name
      )

      httparty_options[:body] = payload.to_json
      httparty_options[:headers] ||= {}
      httparty_options[:headers]['Content-Type'] = 'application/json'

      HTTParty.post(options[:webhook_url], httparty_options)
    end

    private

    attr_reader :formatter

    def message_text
      text = [
        '@channel',
        "### #{formatter.title}",
        formatter.subtitle,
        "*#{@exception.message}*"
      ]

      if (request = formatter.request_message.presence)
        text << '### Request'
        text << request
      end

      if (backtrace = formatter.backtrace_message.presence)
        text << '### Backtrace'
        text << backtrace
      end

      text << message_issue_link if @gitlab_url

      text
    end

    def message_issue_link
      link = [@gitlab_url, formatter.app_name, 'issues', 'new'].join('/')
      params = {
        'issue[title]' => ['[BUG] Error 500 :',
                           formatter.controller_and_action || '',
                           "(#{@exception.class})",
                           @exception.message].compact.join(' ')
      }.to_query

      "[Create an issue](#{link}/?#{params})"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
exception_notification-4.4.0 lib/exception_notifier/mattermost_notifier.rb