Resources

Summary

# config/environments/development.rb
config.consider_all_requests_local = false

# config/application.rb
config.exceptions_app = self.routes

# config/routes.rb
match "/404", to: "errors#not_found", via: :all
match "/500", to: "errors#internal_server_error", via: :all

# errors_controller.rb
class ErrorsController < ApplicationController
  # layout 'errors'

  def not_found
  end

  def internal_server_error
    begin
      exception = request.env['action_dispatch.exception']
      message = exception.message.to_s
      source_extract = exception.source_extract.join("\n")
      backtrace = exception.backtrace[0..9].join("\n")
      SlackNotifyJob.perform_later(message, source_extract, backtrace)
    ensure
      # head :internal_server_error
      render status: 500
    end
  end
end

# Gemfile
gem "slack-notifier"

# Terminal
rail g job SlackNotify

# jobs/slack_notify_job.rb
class SlackNotifyJob < ApplicationJob
  queue_as :default

  def perform(error_message, source_extract, backtrace)
    error = "Error: 500 - Internal Server Error"
    message = ""
    message << "*#{error}*\n"
    message << "*Date:* #{Time.now}\n"
    message << "*Error:* ```#{error_message}``` \n"
    message << "*Source:* ```#{source_extract}``` \n"
    message << "*Backtrace*: ```#{backtrace}``` \n"
    notifier = Slack::Notifier.new Rails.application.secrets.slack_url
    notifier.ping message, username: '091Error', channel: '#general'
  end
end