在Laravel 5中获取错误严重性

时间:2016-11-14 14:08:58

标签: php laravel error-handling exception-handling

如何在处理异常时找到错误的严重性? 每当出现错误时我都会添加电子邮件通知,但出于某种原因,我也会收到有关验证失败的通知。

Error Number: 1318
Incorrect number of arguments for PROCEDURE mydb.SPInsertEventTran; expected 9, got 0
call SPInsertEventTran()
Filename: F:\wamp\www\eventmanagementsystem\system\database\DB_driver.php
Line Number: 331

2 个答案:

答案 0 :(得分:3)

report课程的App\Exceptions\Handler函数中,您可以执行以下操作:

if ($this->shouldReport($e)) {
    // send email
}

这不会报告$dontReport变量中列出的异常。

所以你的最终代码看起来像是:

public function report(\Exception $e)
{
    if ($this->shouldReport($e)) {
        $top = $e->getMessage().' on line '.$e->getLine();
        $body = $e->getTraceAsString();

        Mail::queue('emails.general', compact('top','body'), function($message) {
            $message->from('abc@abc.com','abc');
            $message->to('abc@gmail.com','abc')
                ->subject('An error on Abc');
        });
    }

    parent::report($e);
}

如果你想发送完整的异常堆栈跟踪,那么你可以使用这个github repo。

答案 1 :(得分:0)

app/Exceptions/Handler.php中,您可以告知Laravel您希望报告哪些例外情况。

应该有一个名为$dontReport的属性,您可以添加ValidationException以阻止其报告,从而停止发送电子邮件。

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
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 = [
        AuthorizationException::class,
        HttpException::class,
        ModelNotFoundException::class,
        ValidationException::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)
    {
        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)
    {
        return parent::render($request, $e);
    }
}

这是我的Handler.php文件。您可以看到ValidationException::class属于$dontReport属性,但未报告。

或者,您可以修改代码,专门忽略ValidationException

if ($e instanceof \Exception && !($e instanceof ValidationException)) {
    // Your email code...