Laravel custom exception

14,714

Solution 1

Answering your questions:

  1. To pass this exception to view, you can implement render method what you already started to do. You can just do:

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

so now you can just create error_handler.blade.php view and you will have access in there to $exception variable, so you can use in there {{ $exception->getMessage}} and so on

  1. You didn't define what exactly you want to achieve, however it should work without any problem:

    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'
              ]);
    
    }
    

Of course instead of using $exception->getCode() for code you can put anything you want, this is just an example that you can also use code and message that you have in your exception assuming you set some custom when you throw exception for example:

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

Solution 2

There isn't realy reason to thrown an exception if your purpose is to show the message of that exception within the view which is rendered by the controller. And It isn't a best way to manage exception, because by default all exception which are throw are handle and catch within the App\Exceptions\Handler class.

I think you know exactly when that type of CustomExption you have create will be thrown, but instead of throwing that error just traite that error which need an exception in different way without exception. For that you can create an Array in which old code, status, message, data and pass it to the view method;

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

You have you error data in your view

Share:
14,714

Related videos on Youtube

Rahul
Author by

Rahul

Updated on June 04, 2022

Comments

  • Rahul
    Rahul almost 2 years

    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);
            }
        }
    
    }
    
  • Rahul
    Rahul over 5 years
    I already know that, it would have been great if you have answered my question with some proper solution or example.
  • iWizard
    iWizard over 3 years
    that is reallly great solution to use error code instead of creating new exception classes