mercredi 5 juin 2019

Error at render Handler.php called from controller, error catching exceptions in general

i´m newbie to laravel. I'm trying to handle exceptions in laravel at Handler.php, throwing the exception from the controller. I get different errors trying this, from "Call to undefined function App\Http\Controllers\render()" which i solved adding the Request $request at the index method (don´t know if that´s even possible and right to do). Also tried a lot of different ways, my code is commented in some of the important parts, also, im trying to make an error on the code to check if my error handling works, that´s where my headache begins and nothing about the error handling works. I was trying to make a centralized error handling class at Handler.php, so the catch at my method could be easier. I need really some help to figure out my mistakes please. Thanks! :)

Ivé already tried correcting the errors that pop up, such as the render one, the request one and they just keep going or my page stays at blank withouth nothing. Also i had an error that said the redirecting was bad (i think i made an infinite loop of errors redirecting). I have some tries commented on the code also.

Index method at controller: public function index(Request $request)//I ADDED THIS REQUEST WITHOUT DOING ANYTHING ELSE IN ANY PART OF THE CODE, IS IT BAD? {

    try{

        asd;//on purpose error to check catch

        \Session::put('errorOrigin', " mostrando los clientes");

        //Gets all clients with their phone and emails (even unactive phone and emails, those al filtered
        //at the view))
        $clients = $this->model::all();

        //return dd($clients[0]);
        for($x = 0; $x <= (count($clients)-1); $x++) {

            if($clients[$x]->type == 1) {//physical client, fill model attributes
                $phisClient = Physical_client::where('client_id', $clients[$x]->id)->first();
                $clients[$x]->lastname = $phisClient->lastname;
                $clients[$x]->second_lastname = $phisClient->second_lastname;   
                $clients[$x]->client_table_id = $phisClient->client_id; 
            }

            if($clients[$x]->type == 2) {//juridical client, fill model attributes
                $jurClient = Juridical_client::where('client_id', $clients[$x]->id)->first();
                $clients[$x]->client_table_id = $jurClient->client_id;  
            }
            $clients[$x]->phones = $clients[$x]->phones()->where('active_flag', 1)->get();
            $clients[$x]->emails = $clients[$x]->emails()->where('active_flag', 1)->get();
        }

        $user_type = Auth::user()->user_type_id;
        if($user_type == 1){//admin user
            return view('admin.clients.index', compact('clients'));
        }
    }catch(\Exception $e){
        //throw
        /*$handler =  app(\App\Exceptions\Handler::class);
        $handler->report($e);
        $handler->render($request, $e);*/

        report($e);
        render($e);
    }
}

render method at Handler.php (App/Exceptions)

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

    //this return is just some trying
    //return parent::render($request, $exception);
    //return dd($exception);
    //return parent::render($request, $exception);


    if($exception instanceof \ErrorException) {
        Session::flash('message_type', 'negative');
        Session::flash('message_icon', 'hide');
        Session::flash('message_header', 'Success');
        Session::flash('error', '¡Ha ocurrido un error ' . $errorOrigin . "!" 
        .' Si este persiste contacte al administrador del sistema');
        return redirect()->back();

    }elseif($exception instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
        Session::flash('message_type', 'negative');
        Session::flash('message_icon', 'hide');
        Session::flash('message_header', 'Success');
        Session::flash('error', '¡La página a la que ha intentado
        acceder no existe o no es accesible en este momento!');
        return back();

    }elseif($exception instanceof \Symfony\Component\Debug\Exception\FatalThrowableError) {
        Session::flash('message_type', 'negative');
        Session::flash('message_icon', 'hide');
        Session::flash('message_header', 'Success');
        Session::flash('error', '¡Ha ocurrido un error!');
        return back();
    }elseif($exception instanceof \Illuminate\Auth\AuthenticationException) {
        return Redirect::back()->withErrors($validator)->withInput();

    }elseif($exception instanceof \UnexpectedValueException) {
        Session::flash('message_type', 'negative');
        Session::flash('message_icon', 'hide');
        Session::flash('message_header', 'Success');
        Session::flash('error', '¡Ha ocurrido un error con un valor no válido al ' . $errorOrigin . "!" 
        .' Si este persiste contacte al administrador del sistema');
        return back();

    }elseif($exception instanceof \Illuminate\Database\QueryException) {
        Session::flash('message_type', 'negative');
        Session::flash('message_icon', 'hide');
        Session::flash('message_header', 'Success');
        Session::flash('error', '¡Ha ocurrido un error en la consulta a la base de datos!'
        .' Si este persiste contacte al administrador del sistema');
        return back();

    }else{/*Original error handling*/
        return parent::render($request, $exception);
    }
}

Actual error: Call to undefined function App\Http\Controllers\render()

Already made some tries calling the class directly, but it fails the page then just stays in BLANK and does nothing, not even errors.

At render() i've made a lot of tries too.



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2WrSjIS
via IFTTT

Aucun commentaire:

Enregistrer un commentaire