Ruby Integration

Integrate error tracking into your Ruby applications

Ruby on Rails Integration

1. Install Dependencies

# Gemfile
gem 'httparty'

2. Create Error Logger

# lib/error_logger.rb
require 'httparty'

class ErrorLogger
  API_KEY = ENV['ERROR_LOG_API_KEY'] || 'YOUR_API_KEY'
  SECURITY_KEY = ENV['ERROR_LOG_SECURITY_KEY'] || 'YOUR_SECURITY_KEY'
  WEBHOOK_URL = "https://your-domain.com/api/ingest/#{API_KEY}"

  def self.log_error(exception, context = {})
    payload = {
      message: exception.message,
      stackTrace: exception.backtrace.join("\n")
    }.merge(context)

    HTTParty.post(
      WEBHOOK_URL,
      body: payload.to_json,
      headers: {
        'Content-Type' => 'application/json',
        'X-Security-Key' => SECURITY_KEY
      }
    )
  rescue => e
    # Silently fail
    Rails.logger.error("Failed to log error: #{e.message}")
  end
end

3. Configure Exception Handling

# config/application.rb
module YourApp
  class Application < Rails::Application
    config.exceptions_app = ->(env) do
      ErrorsController.action(:show).call(env)
    end
  end
end

# app/controllers/errors_controller.rb
class ErrorsController < ApplicationController
  def show
    exception = env["action_dispatch.exception"]
    
    if exception
      ErrorLogger.log_error(exception, {
        url: request.original_url,
        method: request.method,
        params: params.to_unsafe_h
      })
    end

    render json: { error: 'Internal server error' }, status: 500
  end
end

4. Rescue From in Controllers

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  rescue_from StandardError, with: :handle_error

  private

  def handle_error(exception)
    ErrorLogger.log_error(exception, {
      url: request.original_url,
      user_id: current_user&.id
    })

    render json: { error: 'Something went wrong' }, status: 500
  end
end

Sinatra Integration

require 'sinatra'
require_relative 'error_logger'

# Global error handler
error do
  e = env['sinatra.error']
  ErrorLogger.log_error(e, {
    url: request.url,
    method: request.request_method
  })
  
  status 500
  json error: 'Internal server error'
end

get '/users/:id' do
  begin
    user = User.find(params[:id])
    json user: user
  rescue => e
    ErrorLogger.log_error(e, { user_id: params[:id] })
    status 500
    json error: 'Failed to fetch user'
  end
end

Environment Configuration

# .env
ERROR_LOG_API_KEY=your_api_key_here
ERROR_LOG_SECURITY_KEY=your_security_key_here

Manual Error Logging

class UsersController < ApplicationController
  def create
    begin
      @user = User.create!(user_params)
      render json: @user, status: :created
    rescue ActiveRecord::RecordInvalid => e
      ErrorLogger.log_error(e, {
        action: 'user_creation',
        params: user_params
      })
      render json: { error: e.message }, status: :unprocessable_entity
    end
  end

  private

  def user_params
    params.require(:user).permit(:name, :email)
  end
end

Tip

Use the dotenv gem to manage your environment variables in development!