使用octoberCMS

时间:2016-09-26 10:52:27

标签: php error-handling octobercms

我有一些自定义路线

Route::get('/files/', [
            'as' => 'read',
            'uses' => 'Myname\MyPlugin\Http\Controllers\FilesController@read'
        ]);

在我班级的某个地方,我有一个验证路径的功能

private function getPath()
{
    $path = Input::get('path');

    if (!$path)
    {
    throw new MyException('parameter is missing. path required', 400);
    }

    return base_path().'/'.$path;
}

我已经使用JSOM设置了自定义错误处理程序,但它是OctoberCMS的错误处理程序,它以HTML格式呈现错误。

您是否知道用自定义替换OctoberCMS的默认错误处理程序的方法? 感谢

2 个答案:

答案 0 :(得分:0)

刚刚在文档中找到了anwser:https://octobercms.com/docs/services/error-log#exception-handling

10月提供App:错误来管理插件中的异常。

([\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})

不要伪造为您的插件创建自定义异常。如果您使用通用例外,则会捕获所有异常。

答案 1 :(得分:0)

由于此问题在Google上的可见性,因此我在此处为自定义错误处理程序发布了替代解决方案。

由于Github上的以下问题,我在使用App:error时遇到了问题:https://github.com/octobercms/october/issues/3416


不使用十月的App::error进行自定义错误处理,请尝试以下操作:

  1. 创建一个自10月的错误处理程序继承的自定义错误处理程序。例如,在plugins/{AUTHOR}/{PLUGIN}/classes/CustomHandler.php中创建以下类(假设您正在为OctoberCMS开发插件)。覆盖处理程序的render方法。

    <?php
    
    namespace {AUTHOR}\{PLUGIN}\Classes; //<-- CHANGE ME OBVIOUSLY FOR YOUR PLUGIN
    
    use October\Rain\Foundation\Exception\Handler;
    use Illuminate\Support\Facades\Event;
    use Illuminate\Support\Facades\Response;
    use Exception;
    use Illuminate\Database\Eloquent\ModelNotFoundException; // As an example exception you want to handle ...
    
    /* Custom error handler which replaces the default error handler for OctoberCMS. */
    class CustomHandler extends Handler
    {
        /**
        * Render an exception into an HTTP response.
        *
        * @param  \Illuminate\Http\Request  $request
        * @param  \Exception  $exception
        * @return \Illuminate\Http\Response
        */
        public function render($request, Exception $exception)
        {
            /* Custom JSON response for ModelNotFoundException exceptions. */
            if($exception instanceof ModelNotFoundException){
                return Response::json(['error' => 'A record was not found for the resource that was requested.'], 404);
            }
    
            /* The rest of this code is just the 'default' code from OctoberCMS' error handler. */
            /* The only change is the code above this comment where I handle a specific exception in a unique way.*/
            /* i.e. I decided to return JSON for the error rather than an HTML error page (in debug mode). */
            if (!class_exists('Event')) {
                return parent::render($request, $exception);
            }
    
            $statusCode = $this->getStatusCode($exception);
            $response = $this->callCustomHandlers($exception);
    
            if (!is_null($response)) {
                return Response::make($response, $statusCode);
            }
    
            if ($event = Event::fire('exception.beforeRender', [$exception, $statusCode, $request], true)) {
                return Response::make($event, $statusCode);
            }
    
            return parent::render($request, $exception);
        }
    }
    
    1. 然后,在插件注册文件Plugin.php中,添加具有以下代码的boot方法:

    
    <?php namespace {AUTHOR}\{PLUGIN};
    
    use System\Classes\PluginBase;
    use {AUTHOR}\{PLUGIN}\Classes\CustomHandler; //<-- IMPORTANT
    use Illuminate\Contracts\Debug\ExceptionHandler;  //<-- IMPORTANT
    
    class Plugin extends PluginBase
    {
       /**
       * @var array Plugin dependencies
       */
       public $require = [];
    
    
       public function registerComponents()
       {
       }
    
       public function registerSettings()
       {
       }
    
       public function boot(){
    
    
           /* Replace the default error handler of OctoberCMS to return JSON format responses instead. */
           /* Also, this is used in order to control the responses for certain types of errors/exceptions. */
           /* We are going about it this way because App::error (as mentioned in the documentation for OctoberCMS), */
           /* was not returning the response properly presumably because of a bug. Argh... */
           $this->app->bind(
               ExceptionHandler::class,
               CustomHandler::class
           );
    
    
       }
    
    
    }
    
    

由于新的CustomHandler.php已添加到classes目录中,因此新类应该已经被选中。无需composer dump-autoload -o或更改composer.json

就是这样。发生ModelNotFoundException时,提供给Web浏览器的响应将是您编写的JSON(来自我们创建的自定义错误处理程序)。

相关链接: