Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

override not_found sinatra application

Tags:

ruby

sinatra

sinatra (= 1.4.5)

I want to render a custom 404 message for specific routes, in this case, the text 'Not found'

class App < Sinatra::Application 

  not_found do
    'You suck'
  end

  get '/404page' do
    halt 404, 'Not found'
  end 
end

I have found that no matter what I do, the text from the not_found block is invoked any time response status is set to 404, when all I really want is the text from the halt.

May be a duplicate of Override Sinatra default NotFound error page but I have not been able to find the soution.

like image 267
lfender6445 Avatar asked Feb 21 '26 14:02

lfender6445


1 Answers

Overriding error Sinatra::NotFound seems to be a better choice to handle 404 when a specific route is not found.

error Sinatra::NotFound do
  'You suck'
end

get '/404page' do
  halt 404, 'Not found'
end
like image 101
garbagecollector Avatar answered Feb 24 '26 16:02

garbagecollector



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!