【问题标题】:Laravel custom exceptionLaravel 自定义异常
【发布时间】:2019-02-25 01:55:08
【问题描述】:

在发布这个问题之前,我已经在互联网上搜索了合适的答案,但没有得到任何答案。这些是我的以下问题:

1) 如何在 laravel 控制器中不通过 try catch 就抛出异常,并在被调用控制器的视图中获取异常。 示例:TestController.php

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

如何在仪表板视图中获取异常消息

2) 如何设置异常信息的格式,假设我想返回格式为

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

我创建了一个自定义异常,但不知道如何充分利用它

<?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);
        }
    }

}

【问题讨论】:

    标签: php laravel exception laravel-5 laravel-exceptions


    【解决方案1】:

    默认异常处理程序会拦截所有未捕获的异常。如果你想让它表现不同,你只需要修改它:https://laravel.com/docs/5.7/errors#the-exception-handler

    【讨论】:

    • 我已经知道了,如果您能用一些适当的解决方案或示例回答我的问题,那就太好了。
    【解决方案2】:

    回答您的问题:

    1. 要将此异常传递给视图,您可以实现您已经开始做的render 方法。你可以这样做:

      if(!$request->ajax()){
          view('error_handler', compact('exception'))
      } else {
         return response()->json($response);
      }
      

    所以现在你可以创建error_handler.blade.php 视图,你可以在那里访问$exception 变量,所以你可以在那里使用{{ $exception-&gt;getMessage}} 等等

    1. 您没有明确定义您想要达到的目标,但是它应该可以毫无问题地工作:

      public function render($request,$exception) {
      
          if(!$request->ajax()){
              view('error_handler', compact('exception'))
          }
      
          return response()->json([
                    'code' => $exception->getCode(),
                    'status' => 'error',
                    'message' => $exception->getMessage(),
                    'data' => 'sample data'
                ]);
      
      }
      

    当然,除了使用$exception-&gt;getCode() 代替code,你可以放任何你想要的东西,这只是一个例子,假设你在抛出异常时设置了一些自定义,你也可以使用你在异常中的代码和消息例如:

    throw new CustomException('This is custom message', 123);
    

    【讨论】:

    • 这是使用错误代码而不是创建新异常类的非常好的解决方案
    【解决方案3】:

    如果您的目的是在控制器呈现的视图中显示该异常的消息,则没有理由抛出异常。而且这不是管理异常的最佳方式,因为默认情况下,所有抛出的异常都是 App\Exceptions\Handler 类中的句柄和捕获。

    我想你知道你创建的那种类型的 CustomExption 什么时候会被抛出,但不是抛出那个错误,只是描述那个需要以不同方式无异常的异常的错误。为此,您可以创建一个数组,其中包含旧代码、状态、消息、数据并将其传递给视图方法;

    class CustomController extends Controller
    {
        public function samemethod(){
             // some code that can generate an error
    
             // construct the error data
             $customErrorData = [
                 'code' => 0000,
                 'status' => "some status",
                 'message' => "Some error message",
                 'data' => [...]
             ];
    
             // After error data creation you can pass it to the view
             return View::make('customview', compact('customErrorData'));
        }
    }
    

    您的视图中有错误数据

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多