Sha256: 4ea408959fd89009a840596a0d50a18b0c0a2ef7c16b45171b6722fa834a2ff7

Contents?: true

Size: 795 Bytes

Versions: 12

Compression:

Stored size: 795 Bytes

Contents

# frozen_string_literal: true

class ErrorHandlingWithChainsController < ApplicationController

  # Example where the query chain is resolved
  # in the view (during render 'show')
  def fetch_in_view
    @records = DummyRecord
      .rescue(DHC::Error, ->(error) { handle_error(error) })
      .where(color: 'blue')
    render 'show'
    render_error if @error
  end

  # Example where the query chain is resolved
  # before the view is rendered
  def fetch_in_controller
    @records = DummyRecord
      .rescue(DHC::Error, ->(error) { handle_error(error) })
      .where(color: 'blue').fetch
    render 'show'
    render_error if @error
  end

  private

  def handle_error(error)
    @error = error
    nil
  end

  def render_error
    self.response_body = nil
    render 'error'
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
dhs-1.6.0 spec/dummy/app/controllers/error_handling_with_chains_controller.rb
dhs-1.5.0 spec/dummy/app/controllers/error_handling_with_chains_controller.rb
dhs-1.4.2 spec/dummy/app/controllers/error_handling_with_chains_controller.rb
dhs-1.4.1 spec/dummy/app/controllers/error_handling_with_chains_controller.rb
dhs-1.4.0 spec/dummy/app/controllers/error_handling_with_chains_controller.rb
dhs-1.3.0 spec/dummy/app/controllers/error_handling_with_chains_controller.rb
dhs-1.2.0 spec/dummy/app/controllers/error_handling_with_chains_controller.rb
dhs-1.1.0 spec/dummy/app/controllers/error_handling_with_chains_controller.rb
dhs-1.0.3 spec/dummy/app/controllers/error_handling_with_chains_controller.rb
dhs-1.0.2 spec/dummy/app/controllers/error_handling_with_chains_controller.rb
dhs-1.0.1 spec/dummy/app/controllers/error_handling_with_chains_controller.rb
dhs-1.0.0 spec/dummy/app/controllers/error_handling_with_chains_controller.rb