0

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

 }
  • Possible duplicate of [Call to a member function on a non-object](https://stackoverflow.com/questions/54566/call-to-a-member-function-on-a-non-object) – CBroe Apr 27 '18 at 08:30
  • _“Trying to get property of non-object erros.. How can I fix this ?”_ - you should start by researching what the error actually means, if you’re unaware of that. See duplicate for an explanation. (Plus, only vaguely mentioning that you get an error is not actually helpful in most cases. Please always quote error messages verbatim, including what file/line they refer to. In general, please go read [ask].) – CBroe Apr 27 '18 at 08:32
  • No one didin't read the question.. –  Apr 27 '18 at 08:41
  • @CBroe this is not interest with the question, not even a little –  Apr 27 '18 at 08:43
  • Use dd on the exception to check details about the error you want and filter according to them – gbalduzzi Apr 27 '18 at 08:56
  • If you say you get “Trying to get property of non-object” errors and don’t know how to fix them, then this is very much relevant. – CBroe Apr 27 '18 at 09:53
  • @CBroe problem is not about fixing Trying to get property of non-object error. It's just an example like 404 error. Problem is redirecting user when errors occur. Please fully read the question before the flag someone –  Apr 27 '18 at 10:49

1 Answers1

1

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; 
                   }
         }
Adam Kozlowski
  • 5,606
  • 2
  • 32
  • 51