Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7559195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:34:28+00:00 2026-05-30T12:34:28+00:00

For some reason, in a Sinatra after filter I can’t seem to access the

  • 0

For some reason, in a Sinatra “after” filter I can’t seem to access the current status code

require 'rubygems'
require 'sinatra'

after do
  puts "After hook with code: #{response.status}"
end

get '/hi' do
  halt(401, "wtf?")
end

When running curl 127.0.0.1:4567/hi, it results in:

After hook for code: 200
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:34:29+00:00Added an answer on May 30, 2026 at 12:34 pm

    It’s basically a function of how the methods are implemented in Sinatra. The methods we need to pay attention to are call!, invoke and dispatch!, all methods in Sinatra::Base (as of v1.3.2).

    call! is the top level method, and in there, it calls the following line of code:

    invoke { dispatch! }
    

    Now, invoke looks like this:

    def invoke
      res = catch(:halt) { yield }
      res = [res] if Fixnum === res or String === res
      if Array === res and Fixnum === res.first
        status(res.shift)
        body(res.pop)
        headers(*res)
      elsif res.respond_to? :each
        body res
      end
    end
    

    It actually sets the response code on the basis of the thing you throw with :halt. And dispatch! looks like:

    def dispatch!
      static! if settings.static? && (request.get? || request.head?)
      filter! :before
      route!
    rescue ::Exception => boom
      handle_exception!(boom)
    ensure
      filter! :after unless env['sinatra.static_file']
    end
    

    See that ensure block? That gets run as the :halt symbol that has been thrown sails up the stack trace. Crucially, this is before the status setting code is run.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason beyond me I can't access the mysql server on a machine.
For some reason this PHP code below will not work, I can not figure
For some reason I can't seem to get this particular web page's contents via
For some reason, my paintComponent(Graphics g) method is being called infinitely. I can't seem
For some reason after I installed Boot Camp, my os x terminal started to
For some reason, heroku tries to require dm-sqlite-adapter, even though it should use Postgres
For some reason, my script isn't writing out the text after I remove the
For some reason, I can't update the second label. I tested it by asking
For some reason if I try to display data with the following code, I
For some reason, I can not get this dialog to show up on the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.