在Laravel 5中处理来自DomDocument的500内部服务器错误

时间:2015-06-22 16:29:48

标签: php laravel exception-handling laravel-5

我为Laravel编写的库使用DomDocument

我在Controller下使用此库,其名称空间为app/Services/Verify/。当我将一些输入放入表单时,库被初始化并使用。

当库失败时,Laravel将会失败 - 返回以下消息:Whoops, looks like something went wrong.

我使用以下正则表达式验证客户端的网址'our_team_link' => 'required|url|regex:/^http:\/\/www\.ugcleague\.com\/team_page\.cfm\?clan_id=\d+$/'

以下是我上面网址的DomXPath代码。

    // Generate our team's HTML file
    $this->ourTeamHTML = new \DomDocument;
    $this->ourTeamHTML->loadHTMLFile($this->ourTeamURL);

大多数情况下,网络应用程序运行正常。但是,有些情况下,即使他们键入了正则表达式有效的URL,也确实存在不存在的URL(仍然通过正则表达式),并返回如下错误:< / p>

PHP Warning:  DOMDocument::loadHTMLFile(http://www.ugcleague.com/team_page.cfm?clan_id=8831111118): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error
 in /Users/loop/Code/laravel/app/Services/Verify/ScrapeUGC.php on line 49

Warning: DOMDocument::loadHTMLFile(http://www.ugcleague.com/team_page.cfm?clan_id=8831111118): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error
 in /Users/loop/Code/laravel/app/Services/Verify/ScrapeUGC.php on line 49

Laravel将链接到Whoops, looks like something went wrong页面。

这会让用户感到困惑。我希望返回一个更具描述性的错误,也就是说,&#34;这个团队不存在。&#34;

2 个答案:

答案 0 :(得分:0)

您需要使用php函数来禁用libxml错误。

uint

现在,当您完成脚本处理后,您将需要使用...

生成任何错误
libxml_use_internal_errors(true);

那会返回一个数组,所以你现在可以用它来检查......

$errors = libxml_get_errors();

您很可能需要遍历该数组并处理每个错误,以确保它是您正在寻找的特定错误。使用if(count($errors) > 0) { echo 'This team does not exist'; } else { echo 'Successful'; } // Be sure to clear errors libxml_clear_errors(); 时,每次发现无效的HTML / XML时,您都会收到一堆通知/警告。

答案 1 :(得分:0)

您可以在Laravel app / Exceptions / Hnadler.php

中处理此问题

注意:我已经查看了使用PHP中可用的DOMException处理程序的选项,但是您通过I / O警告得到的错误消息并非真实和异常。

这是PHP本机DomException的样子:

/**
 * DOM operations raise exceptions under particular circumstances, i.e.,
 * when an operation is impossible to perform for logical reasons.
 * @link http://php.net/manual/en/class.domexception.php
 */
class DOMException extends Exception  {

    /**
     * @var
     * (PHP 5)<br/>
     * An integer indicating the type of error generated
     * @link http://php.net/manual/en/class.domexception.php#domexception.props.code
     */
    public $code;
}

所以我想出了这个,因为我们不能使用DomException来指示这个错误,因为它不是Exception,你可以在你的app / Exceptions / Handler.php中添加它

 <?php

 namespace App\Exceptions;

 use Exception;
 use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;


 class Handler extends ExceptionHandler
 {
      /**
      * A list of the exception types that should not be reported.
      *
      * @var array
      */
      protected $dontReport = [
         \Symfony\Component\HttpKernel\Exception\HttpException::class,
      ];

     /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $e
     * @return void
     */
     public function report(Exception $e)
     {
         return parent::report($e);
     }

    /**
    * Render an exception into an HTTP response.
    *
    * @param  \Illuminate\Http\Request  $request
    * @param  \Exception  $e
    * @return \Illuminate\Http\Response
    */
    public function render($request, Exception $e)
    {

        $message = $e->getMessage();

        if (str_contains($message, 'DOMDocument::loadHTMLFile(): I/O warning')) {
             return redirect($request->fullUrl())->with('error', "This team does not exist");
        }


       //We could also handle DomException like so, but not Dom warning 
       if ($e instanceof \DomException){
            return redirect($request->fullUrl())->with('error', "Your friendly message here");
        }


     }
}

注意:修改Handler.php时要小心,如果你不知道自己在做什么,可能会开始有空白页而不是Laravel呐喊或错误。如果您不确定,可以在某处进行备份。