Override Sinatra default NotFound error page

后端 未结 3 1281
后悔当初
后悔当初 2021-02-05 20:17

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

3条回答
  •  情深已故
    2021-02-05 21:02

    If you don't use error handling in your route, you can utilize the built in error route like this (taken and modified from the Sinatra: Up and Running book)

    require 'sinatra'
    
    configure do
      set :show_exceptions, false
    end
    
    get '/div_by_zero' do
      0 / 0
      "You won't see me."
    end
    
    not_found do
      request.path
    end
    
    error do
      "Error is: " + params['captures'].first.inspect
    end
    

    There is a parameter captures that holds your error. You can access it via params['captures']. It is an array, and in my tests it would contain a single element, which was the error itself (not a string).

    Here is information on the request object.

提交回复
热议问题