dtu72460 2018-09-20 17:36
浏览 250

Laravel自定义异常

Before posting this question I have searched internet for appropriate answers but got none.These are my following questions:

1) How to throw exception without try catch in laravel controller and get exception in the view of the called controller. Example : TestController.php

function index(){
throw new CustomException('Data not found');
return view('dashboard');
}

How to get exception message in dashboard view

2) How to set format for exception message, suppose I want to return format as

$response['code'] = 0;
        $response['status'] = 'error';
        $response['message'] = 'message';
        $response['data'] = '';

I have created a custom exception but don't know how to use it to fully

<?php

namespace App\Exceptions;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Mockery\Exception;

class CustomException extends Exception{

    public $response;


    /**
     * Report the exception.
     *
     * @return void
     */
    public function report()
    {
    }

    /**
     * Render the exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request
     * @return \Illuminate\Http\Response
     */
    public function render($request,$exception){
        ob_clean();
        $response['code'] = 0;
        $response['status'] = 'error';
        $response['message'] = 'Message';
        $response['data'] = '';
        if(!$request->ajax()){
            // non ajax response
        }else{
            return response()->json($response);
        }
    }

}
  • 写回答

3条回答 默认 最新

  • dongyan3853 2018-09-20 17:40
    关注

    All uncaught exceptions are intercepted by default exceptions handler. If you want this to behave differently, you just need to modify it: https://laravel.com/docs/5.7/errors#the-exception-handler

    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数