laravel异常简单处理

来源:互联网 发布:怎么用淘宝微淘 编辑:程序博客网 时间:2024/05/23 20:12
laravel中针对具体的处理逻辑,可能存在的错误。try{} catch(Exception $e) {}捕获处理对应的错误。针对大量出现的可能存在异常,可以使用全局异常捕获,如NotFoundException ,ModelNotFoundException
在\App\Exception\Handle中,对于不需要处理的异常添加到 $dontReport = []。其中report方法一般是对应的分开记录日志处理,render方法是对应的异常http响应处理。根据具体需求配置

如:

class Handler extends ExceptionHandler{    /**     * A list of the exception types that should not be reported.     *     * @var array     */    protected $dontReport = [        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)    {        if ($e instanceof ModelNotFoundException) {            $e = new NotFoundHttpException($e->getMessage(), $e);        }        if ($e instanceof \ErrorException) {            return xxx;        }    }}

可以对应的配置和函数中,根据需求添加对应的内容,和逻辑处理