æè¿ Padrino ã¨ããã¦ã§ãã¢ããªã±ã¼ã·ã§ã³ãã¬ã¼ã ã¯ã¼ã¯ã触ã£ã¦ã¾ãã
Padrino ã§ã¯ã³ã³ããã¼ã©ã¼ã®ã¢ã¯ã·ã§ã³ãå®è¡ããåå¾ã«å¦çãå®è¡ãããã¨ãã§ãã¾ãã
before do åå¦ç end after do å¾å¦ç end get :hoge do å¦ç end
ã¾ããã¨ã©ã¼æã«è¡¨ç¤ºããããã¼ã¸ãæå®ãããã¨ãã§ãã¾ãã
error 403 do render 'errors/403' end error 404 do render 'errors/404' end error 500 do render 'errors/500' end
ã§ãããã¨ã©ã¼æã« after
ãå®è¡ãããªãå ´åããã£ãã®ã§èª¿ã¹ã¦ã¿ã¾ããã
ãã®1. ã³ã³ããã¼ã©ã¼ãè¦ã¤ãããªãå ´å
ã³ã³ããã¼ã©ã¼ãè¦ã¤ãããªãå ´åã¯ã404 Not Found ã¨ã©ã¼ã«ãªãã®ã§ãããerror 404
ã¯å®è¡ããã¾ããã
ã¾ã after
ãå®è¡ããã¾ããã
ããã°ã©ã å
㧠halt 404
ãè¡ã£ãå ´å㯠error 404
ã after
ãå®è¡ããã¾ãã
error 404
ã®ä»£ããã« not_found
ã使ãã°ã³ã³ããã¼ã©ã¼ãè¦ã¤ãããªãã£ãå ´åã«ãå®è¡ããã¾ãããafter
ã¯å®è¡ããã¾ããã
after
ã§è¡ãããå¾å¦çã not_found
ã®ä¸ã«ãæ¸ãã¦ããã°ããã®ã§ãããããããã¨ä»åº¦ã¯ halt 404
ãè¡ã£ãå ´åã«å¾å¦çãäºåå®è¡ããã¦ãã¾ãã¾ãã
not_found
ã®å¾ã« error 404
ãå®ç¾©ããã¨ãhalt 404
ã§ã¯ not_found
ã¯å®è¡ãããªãã®ã§æ¬¡ã®ããã«è¨è¿°ãããã¨ãã§ãã¾ãã
after do å¾å¦ç end # ã³ã³ããã¼ã©ã¼ãè¦ã¤ãããªãå ´åã # after ã¯å®è¡ãããªãã not_found do å¾å¦ç render 'errors/404' end # ããã°ã©ã ä¸ã§ halt 404 ããå ´åã # after ã¯å®è¡ãããã error 404 do render 'errors/404' end
ãªã not_found
ã®åã« error 404
ãæ¸ã㨠error 404
ã¯å®è¡ããã¾ããã
è¨è¿°ããé çªã«ä¾åããã®ã¯ãããããã®ã§ not_found
ã使ããªãæ¹ãããããããã¾ããã
error Sinatra::NotFound
ã¯ã³ã³ããã¼ã©ã¼ãè¦ã¤ãããªãå ´åã«ã ãå®è¡ãããhalt 404
æã«ã¯å®è¡ãããªãã®ã§ãè¨è¿°ããé çªã«ã¯ä¾åãã¾ããã
after do å¾å¦ç end # ããã°ã©ã ä¸ã§ halt 404 ããå ´åã # after ã¯å®è¡ãããã error 404 do render 'errors/404' end # ã³ã³ããã¼ã©ã¼ãè¦ã¤ãããªãå ´åã # after ã¯å®è¡ãããªãã error Sinatra::NotFound do å¾å¦ç render 'errors/404' end
ãã®2. ããã°ã©ã ä¸ã§ä¾å¤ãçºçããå ´å
ããã°ã©ã ä¸ã§ä¾å¤ãçºçããå ´åã«ã after
ã¯å®è¡ããã¾ããã
development ç°å¢ã§ã¯ after
ã¯å®è¡ãããã®ã§ããããã㯠development ç°å¢ã§ã¯ :show_exceptions
ã true ã«ãªã£ã¦ããããã§ãã
production ç°å¢ã¨åãåä½ããããããã«æ¬¡ã®ããã«è¨å®ããã¨ãdevelopment ç°å¢ã§ã after
ãå®è¡ãããªããã¨ã確èªã§ãã¾ãã
disable :show_exceptions
ä¾å¤çºçæ㯠500 ã¨ã©ã¼ã«ãªãã®ã§ error 500
ãæ¸ãã¦ããã°ãããå®è¡ããã¾ãã
not_found
ã®æã¨åæ§ãããã°ã©ã å
㧠halt 500
ãã㨠after
㨠error 500
ã®ä¸¡æ¹ã§å¾å¦çãå®è¡ããã¦ãã¾ãã¾ãããããã°ã©ã ä¸ãããããã halt 500
ãè¡ããã¨ã¯ãªãã¨æãã®ã§ãåé¡ãªãã§ãããã
after do å¾å¦ç end # halt 500 ã®å ´å & ä¾å¤ãçºçããå ´åã error 500 do å¾å¦ç render 'errors/500' end
error
ã«æ°åã§ã¯ãªãä¾å¤ã¯ã©ã¹ãæå®ããã¨ããã®ä¾å¤ãçºçããæã ãå®è¡ã§ããã®ã§ã次ã®ããã«ãè¨è¿°ã§ãã¾ãã
ãã®å ´å㯠halt 500
ã§ã¯å®è¡ããã¾ããã
after do å¾å¦ç end # ä¾å¤ãçºçããå ´åã # after ã¯å®è¡ãããªãã error Exception do å¾å¦ç render 'errors/500' end
ãªãã error Exception
ã nil ãè¿ãå ´å㯠after
ãå®è¡ãããã®ã§ãå¾å¦çãå®è¡ãããã ãã§ããã°æ¬¡ã®ããã«è¨è¿°ãããã¨ãã§ãã¾ãã
ãã ããã®å ´å㯠500 ã®ã¨ã©ã¼ãã¼ã¸ãè¿ããã¨ã¯ã§ãã¾ããã
after do å¾å¦ç end # ä¾å¤ãçºçããå ´åã # after ãå®è¡ãããã error Exception do nil end
ã¾ã¨ã
- ä¾å¤çºçæã¯
after
ã¯å¼ã°ããªã not_found
ã¯halt 404
ã¨ã³ã³ããã¼ã©ã¼ãè¦ã¤ãããªãå ´åã®ä¸¡æ¹ã§å¼ã°ããerror ä¾å¤ã¯ã©ã¹
ã§ä¾å¤çºçæã®å¦çãè¨è¿°ã§ãã