首先你需要找到一些逻辑you can identify that this error is coming from Controller A or B
,你需要一些自定义逻辑,
要覆盖,您可以在plugin.php
inboot
方法中使用此代码
<?php namespace HardikSatasiya\DemoTest;
use System\Classes\PluginBase;
use Event; // <- don't forget to add this one
class Plugin extends PluginBase
{
public function boot() {
Event::listen('exception.beforeRender',
function ($exception, $httpCode, $request) {
// you need to write some logic here so you can identify
// where from error is raising ( Controller A or B)
// and show proper message
dd($exception);
// if error is not relavent to you just PASS it
// return null to pass on this listener
// it will call default system exception handler
return null;
}, 1); //<- set priority to 1, As default handler has 0 so
}
.... some code
第二种情况,如果您能够自己引发和异常,您可以使用这个更好,因为它显示了异常从哪里引发
https://octobercms.com/docs/services/error-log#exception-handling
// App::error(function(YOUR_EXCEPTION_CLASS $exception) {
App::error(function(InvalidUserException $exception) {
// you can return whatever you want
return 'Sorry! Something is wrong with this account!';
});
您还可以将其与第一个解决方案结合使用,您可以将$exception
对象与您抛出的对象进行比较controller
,get idea its from controller A or B
然后显示正确的消息
例如:throw from controller
use Cms\Classes\CmsException;
throw new CmsException('some message');
将由put this also in plugin boot method
use App;
use Cms\Classes\CmsException;
App::error(function(CmsException $exception) {
return 'I will Handle CmsException ONLY !';
});
如果您还有任何疑问,请发表评论。