简体   繁体   English

如何从Play中的Action引发错误或异常?

[英]How to throw errors or exceptions from Action in Play?

I read the official document of PlayFramework about Error handling. 我阅读了有关错误处理的PlayFramework官方文档。 And I learned it is good to use HttpErrorHandler class. 而且我了解到使用HttpErrorHandler类是很好的。 And they say such error handling classes can catch errors or exceptions from Action . 他们说这种错误处理类可以捕获Action错误或异常。 And I saw methods of DefaultHttpErrorHandler class. 我看到了DefaultHttpErrorHandler类的方法。 However, how does the DefaultHttpErrorHandler class distinguish errors or exceptions thrown from Action ? 但是, DefaultHttpErrorHandler类如何区分Action抛出的错误或异常? Some method such as onServerError takes int argument as its parameter, so in this case how does Action throw errors or exceptions? 诸如onServerError某些方法将int参数作为其参数,因此在这种情况下, Action如何引发错误或异常? Does other page show it? 其他页面会显示吗?

Basically, an exception thrown in a Controller will result of a 500 http error code. 基本上,在Controller中引发的异常将导致500 http错误代码。 The onServerError method will catch and handle this exception. onServerError方法将捕获并处理此异常。

On the other side, 400, 401, 403 or 404 http error codes are called "Client error" in Play environnement. 另一方面,在播放环境中,400、401、403或404 http错误代码称为“客户端错误”。 These errors are catch and handle by the onClientError method. 这些错误由onClientError方法捕获并处理。

Thus, if you want to have a dedicated and customized page for 404 http error page ("Page not found"), build the given view and call it inside the onClientError method (you have the statusCode parameter to distinct 404 from 400, 401 or other http status code). 因此,如果要为404 http错误页面(“找不到页面”)创建专用的定制页面,请构建给定视图并在onClientError方法内调用它(您可以使用statusCode参数将404与onClientError或其他http状态代码)。

If you want to have a custom error page, you have to call the dedicated view in onServerError method. 如果要具有自定义错误页面,则必须在onServerError方法中调用专用视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM