0

我需要根据发生错误的位置有不同的自定义错误页面

如果发生错误

控制器 A => 显示自定义错误 A

同样,如果发生错误

控制器 B => 显示自定义错误 B。

这是因为控制器 B 输出 javascript,它需要在出错时输出 javascript。

自定义错误页面设置在 中/modules/system/classes/ErrorHandler.php,如何在自己的插件中编写自己的自定义错误模板选择逻辑。如何/modules/system/classes/ErrorHandler.php在我的插件中覆盖

4

1 回答 1

0

首先你需要找到一些逻辑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对象与您抛出的对象进行比较controllerget 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 !';
});

如果您还有任何疑问,请发表评论。

于 2018-03-22T08:10:04.227 回答