title: How should I return Sinatra HTTP errors from inside a class where HALT is not available?
body: <p>I have a large backend API for my native app that's built in Sinatra, that also serves some admin web pages. I'm trying to dry up the codebase and refactor code into classes inside the lib directory.</p>
  
  <p>My API clients expect a status and a message, such as 200 OK, or 404 Profile Not Found. I'd usually do this with something like <code>halt 404, 'Profile Not Found'</code>.</p>
  
  <p><strong>What's the easiest way of <code>halt</code>ing with an HTTP status code and a message from inside a class?</strong></p>
  
  <hr>
  
  <p><strong>Old Wet Code</strong></p>
  
  <pre><code>post '/api/process_something'
    halt 403, 'missing profile_id' unless params[:profile_id].present?
    halt 404, 'offer not found' unless params[:offer_id].present?
    do_some_processing
    200
  end
  </code></pre>
  
  <p><strong>New Dry Code</strong></p>
  
  <pre><code>post '/api/process_something'
    offer_manager = OfferManager.new
    offer_manager.process_offer(params: params)
  end
  </code></pre>
  
  <p><strong>offer_manager.rb</strong></p>
  
  <pre><code>class OfferManager
    def process_offer(params:)
      # halt 403, 'missing profile_id' unless params[:profile_id].present?
      # halt 404, 'offer not found' unless params[:offer_id].present?
      # halt doesn't work from in here
      do_some_processing
      200
    end
  end 
  </code></pre>