有没有办法覆盖 sinatra 默认的 NotFound 错误页面(“Sinatra 不知道这个小曲”)?我希望 sinatra 在未找到正确的路由时仅将纯字符串显示为“未找到方法”,但是当我从路由内部引发 404 错误时,我希望它显示传入的错误消息。
像这样实现 not_found 块:
not_found do
'Method not found.'
end
有效,但它不是一个有效的选项,因为我希望能够从这样的路由中抛出我自己的 NotFound 错误消息:
get '/' do
begin
# some processing that can raise an exception if resource not found
rescue => e
error 404, e.message.to_json
end
end
但正如预期的 not_found 块会覆盖我的错误消息。
也许是比中提出的更优雅的解决方案接受的答案 https://stackoverflow.com/questions/8452253/override-sinatra-default-notfound-error-page#8472353只为拯救Sinatra::NotFound
,而不是使用error(404)
or not_found
styles.
error Sinatra::NotFound do
content_type 'text/plain'
[404, 'Not Found']
end
这可以防止您尚未定义的路由出现“sinatra 不知道这个小曲”默认页面,但不会妨碍显式的路由return [404, 'Something else']
-风格的回应。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)