user9633176
user9633176

Reputation:

How to redirect users to another page with session messages while error occuring

I want to redirect users to admin page when any error occuring about the thujohn/twitter package. It throws Runtimeexception..

So I add couple code handler.php

public function render($request, Exception $exception)
{

    if ($exception instanceof \RuntimeException) {
        return redirect()->route('admin.panel')
            ->with('message', 'Please try again later..')
            ->with('message_type','warning');

    } else {
        return parent::render($request, $exception);
    }
}

But when I say that, it redirects user at all exceptions even at 404 errors or Trying to get property of non-object erros.. How can I fix this ? I want to redirect user for just relevant error

Or is there any way to do redirect user with condition like below.

if($exception->code == 436){
  // it says member has protected access. I can't use it code property outside of the exception class
      return redirect()->route('admin.panel')
            ->with('message', 'Specific error message')
            ->with('message_type','warning');

 }

Upvotes: 0

Views: 81

Answers (1)

Adam Kozlowski
Adam Kozlowski

Reputation: 5906

First of all go to Exceptions/Handler.php.

Here you can develope your exeptions.

This is my example on QueryException (you need to find by dd() your exception and its code):

use Illuminate\Database\QueryException; // REMEMBER TO USE PROPER INSTANCE


        public function render($request, Exception $exception)
                {
                  //dd($exception); <-- here you can catch and check type od exception
                    switch(true) {

                        case $exception instanceof QueryException:

                            if($exception->getCode() == '23000') {
                                return redirect(
                                    route('get.dashboard.index')
                                )->with('warning', 'No record in database);
                            }
                            break; 
                   }
         }

Upvotes: 1

Related Questions