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

  • SEARCH
  • Home
  • 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 7086227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:31:24+00:00 2026-05-28T07:31:24+00:00

My use case is that I would like to do error handling in sinatra.

  • 0

My use case is that I would like to do error handling in sinatra. For this I am setting up the error handler as follows

error 0..600 do
  @@logger.error("error reason #{env['sinatra.error']}")
end

The sinatra.error variable gets set fine if the error was caused by explicitly raising an exception

get '/' do
  raise "Fail the request"
end 

But if halt is used to terminate the request then sinatra.error does not get set. Looking into sinatra code this appears to be as expected because throwing :halt causes the control flow to go all the way up to invoke and thus bypassing the setting of sinatra.error variable.

My question is how to use the error handler along with the halt so that I can get the cause of the error in the error handler.

  • 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-28T07:31:25+00:00Added an answer on May 28, 2026 at 7:31 am

    I think the behavior you’re seeing stems from the intended purpose of halt. When you call it, you aren’t necessarily signaling in error; you just want execution to stop immediately, which can be particularly useful in a filter. If you check Sinatra’s README, it says that you use halt to “immediately stop a request within a filter or route use”. Granted, you will usually do it because of an error.

    It is also interesting to notice that the error handler you defined gets called not only when errors occur, but also when regular requests are served, including ones with status 200. And in those cases, env[sinatra.error] won’t be set either.

    What you can do in your error handler is to check for an exception and, if it’s not available, check the response code. For example (note that this is a classical application):

    error 0..600 do
      boom = @env['sinatra.error']
      status = response.status
      case
      when boom != nil
        puts 'exception: ' + boom
      when status != 200
        puts 'error: ' + status
      end
    end
    

    One consequence is that, in this handler, normal requests are indistinguishable from those interrupted by halt, because both generate a 200 status code. However, if you are using halt to report errors, then you should be using an error code like 500 anyway.

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

Sidebar

Related Questions

That is, I'd like to have a tuple of values. The use case on
I have a block of HTML that I would like to use as the
I would like to ensure that a method (actually a constructor in my case)
The use case is that I use one development machine for different source trees.
What is CLR hosting? What is the use case for that?
On a Use Case diagram can you show things that an actor cannot do,
Here's the use case: A user clicks on a link that opens a window
I have the following use case: There's a class called Template and with that
Here is a test description, testing the Create New Widget use-case. Confirm that you
Are there rules of thumb that help determine which to use in what case?

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.