Search code examples
rubysinatra

Override Sinatra default NotFound error page


Is there a way to override the sinatra default NotFound error page ("Sinatra doesnt know this ditty")? I want sinatra to show only a plain string as "Method not found" when it does not found the proper route, but when I raise an 404 error from inside a route I want it to show the passed-in error message.

Implementing the not_found block like this:

 not_found do
    'Method not found.' 
  end

works, but its not a valid option since I want to be able to throw my own NotFound error messages from routes like this:

 get '/' do
    begin
      # some processing that can raise an exception if resource not found
    rescue => e
      error 404, e.message.to_json
    end
  end

But as expected not_found block overrides my error message.


Solution

  • Nevermind, found that all routes are matched in order, so after all routes I put get/post/put/delete '*' do ; end and that solves my problem.