2011-12-12 75 views

回答

6

404可以與not_found方法如例如的幫助下進行處理:

not_found do 
    'Site does not exist.' 
end 

500S可以通過調用錯誤方法用的塊時,例如處理:

error do 
    "Application error. Pls try later." 
end 

的細節錯誤可以通過sinatra.errorrequest.env如下訪問:

error do 
    'An error occured: ' + request.env['sinatra.error'].message 
end 
+0

我該怎麼做,以檢查異常如果它是500,我的意思是什麼錯了?謝謝。 – Jane

+0

@Jane查看我的更新回答 – maprihoda

4

我無法在開發環境中開箱即用 - 爲了使其正常工作,我必須在我的sinatra配置中將show_exceptions設置爲false。

class BaseApp < Sinatra::Base 

    configure { set :show_exceptions, false } 

    error do |err| 
    raise "Error: #{err}" 
    end 

end 

這樣設置後,當設置爲true,允許錯誤頁面,顯示回溯和環境信息時unhanded發生異常,但我可以禁用它只是火自定義錯誤。

+1

這是正確的答案。沒有配置線,它不起作用。 – pmontrasio

+1

實際上,必須禁用「:show_exceptions」和「:raise_errors」(即設置爲false)才能執行並返回通用錯誤塊(即無異常類或狀態碼)。 –

+0

請注意,示例中的'err'是異常的消息,也可以通過'env ['sinatra.error']。message'獲得。 –