Custom Error Sinatra
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn
Sinatra Halt
more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags sinatra halt with message Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, sinatra error block helping each other. Join them; it only takes a minute: Sign up Setting status code in Sinatra's custom error block up vote 3 down vote favorite I have a custom error message for 400 status code: get '/do'
Sinatra Exceptions
do raise ParamXMissingError unless params['x'] end error ParamXMissingError do haml :custom_error_page end I want ParamXMissingError to be 400, but when I run the above code, and check through Firefox Network tools, it seems Sinatra is actually returning 500, not 400. How do I make it display the custom_error_page.haml and return 400? Preferably, it'd be nice to have the status code and the page handled from inside the error block, not something that I would sprinkle around
Sinatra Error Do
when raising ParamXMissingError. For example, this would be repetitive and not a good solution:halt 400, haml(:custom_error_page) ruby sinatra rack http-status-code-301 share|improve this question asked Jan 15 '14 at 23:20 DumpHole 1,0221924 add a comment| 1 Answer 1 active oldest votes up vote 5 down vote accepted Doh. All I need is to set status 400: error ParamXMissingError do status 400 haml :custom_error_page end share|improve this answer answered Jan 15 '14 at 23:31 DumpHole 1,0221924 This doesn't work for me, when doing an ajax request, I still get 200s :(. Of course I am doing this in a normal post action though. –Thermatix Aug 19 '15 at 10:00 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged ruby sinatra rack http-status-code-301 or ask your own question. asked 2 years ago viewed 2812 times active 2 years ago Related 3Multipart response in Ruby/Rack1How can I test an action that handles exceptions using rack/test on Sinatra?0Error on a Sinatra's middleware1How to use pry to access a Rack middleware instance?0Standardizing api
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn sinatra custom error pages more about Stack Overflow the company Business Learn more about hiring developers or posting
Sinatra Show_exceptions
ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community sinatra return 404 Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Sinatra Catching Exceptions for Custom Error Pages up vote 1 http://stackoverflow.com/questions/21150363/setting-status-code-in-sinatras-custom-error-block down vote favorite I am trying to handle errors in a Modular Sinatra App. We raise various error through out the app and I wrote some thing alike to catch errors thinking it will happen hierarchically. My the file I use to error handle looks like the following. #!/usr/bin/env ruby # @class class RApp # @brief Sinatra main routes overloading for App class class RApp < Sinatra::Base # @fn http://stackoverflow.com/questions/29881225/sinatra-catching-exceptions-for-custom-error-pages not_found do {{{ # @brief Not found error page not_found do render_error 404, _('Not Found'), env['sinatra.error'].message end # }}} # @fn error ServiceNotAvailableError do {{{ # @brief Handle ServiceNotFoundException, commonly associated with communication # errors with external required services like Ambrosia, Saba error ServiceNotAvailableError do render_error 503, _('Service Not Available'), env['sinatra.error'].message end # }}} # @fn error Exception do {{{ # @brief Handle general internal errors error Exception do render_error 500, _('Internal Server Error'), env['sinatra.error'].message end # }}} error DBC::InvalidUUIDError do "Invalid UUID Error" end # @fn def show_error code, title, message, view = nil {{{ # @brief Displays the proper message (html or text) based on if the request is XHR or otherwise def render_error code, title, message, view = nil view = code.to_s if view.nil? if request.xhr? halt code, message else halt code, slim(:"error_pages/#{view}", locals: {title: title, message: message}) end end # }}} # Just for testing get '/errors/:type' do |type| raise Object.const_get(type) end end # of class RApp < Sinatra::Base }}} # vim:ts=2:tw=100:wm=100 I was thinking it will try in the order in the file which seem to be true. How ever the issue is Exception doesn't catch all exceptions. For example I have DBC::InvalidUUIDError which is as such
application-level variables that are modified using one of the set, enable, http://www.sinatrarb.com/configuration.html or disable methods and are available within the request https://github.com/sinatra/sinatra/issues/578 context via the settings object. Applications are free to set custom settings as well as the default, built-in settings provided by the framework. Using set, enable, and disable In its simplest form, the set method takes custom error a setting name and value and creates an attribute on the application. Settings can be accessed within requests via the settings object: set :foo, 'bar' get '/foo' do "foo is set to " + settings.foo end Deferring evaluation When the setting value is a Proc, evaluation is performed when the setting is read so that other settings may be used to calculate the value: set :foo, 'bar' set :baz, Proc.new { "Hello " + foo } get '/baz' do "baz is set to " + settings.baz end The /baz response should come as “baz is set to Hello bar” unless the foo setting is modified. Configuring multiple settings Multiple settings can be set by passing a Hash to set. The previous example could be rewritten with: set :foo => 'bar', :baz => Failed" $stdout.print "----> Failed" end get "/error" do raise "OMG" end I am using sinatra (1.3.3) dannluciano commented Oct 1, 2012 I found this in documentation: "Sinatra installs special not_found and error handlers when running under the development environment." What is you environment? dannluciano commented Oct 1, 2012 Try with: ruby test_error.rb -env production Sinatra member burningTyger commented Oct 1, 2012 I checked and tested some different cases: If you have status code error handlers like error 500 {} and you halt 500 you're fine. If you raise somewhere and you have a plain error handler nothing will happen. Same goes for the custom Errors. They will only raise an exception on a missing constant. blambeau commented Oct 2, 2012 I've observed a similar behavior in one of my apps recently. Error handling is generally fine except in development mode. I don't know if this is really intended, but it is not very intuitive IMHO... dannluciano commented Oct 4, 2012 @kapso then, do you fixed the problem? JonRowe commented Feb 24, 2013 Hi, I looked into this for you with the aim to writing a patch, the issue is that in :development mode raise_errors is set so that Sinatra can do the magic stack trace page, if you want to test the behaviour of your handlers you need to set raise_errors to be false. See the mapped error tests for more information. Sinatra member rkh commented Feb 26, 2013 Error blocks don't trigger in development mode. rkh closed this Feb 26, 2013 blambeau commented Feb 26, 2013 @rkh that's very odd. Sinatra member rkh commented Feb 26, 2013 No, it's intentional. The idea is that error blocks will hide the issue and you usually don't want to do this in development mode. Do a set :show_exceptions, :after_handler if you want to change that behaviour (see README.md). JonRowe commented Feb 26, 2013 Agreed it makes complete sense. blambeau commented Feb 26, 2013 @rkh @JonRowe only when you use exceptions passively for unintended issues IMHO. I tend to use them actively, as a programming mechanism of its own. in that context, it's odd. EDIT: the difference between "ex