0

I'm new to the whole concept of exception or error handling in laravel. Basically I have form request which, when rules aren't met, shows me 422 error in console, I want to convert that into error message in blade, but I fail.

I already tried this in Exception/Handle.php

public function render($request, Exception $exception) { return redirect()->back()->with('error', 'Invalid data'); // return parent::render($request, $exception); } 

My error message in blade should work with this:

@if(Session::has('error')) <div class="alert alert-error"> {{ Session::get('error') }} @php Session::forget('error'); @endphp </div> @endif 

I know that I should check error codes or instances before returning, but I just want to get this working. I can't return anything (no response at all). One think which works is return parent::render($request, $exception); and it thows error message in console

So more info

I don't know if it helps, but the request is made from Vue application to controller, which takes FormRequest as a parameter.

Also, I can't check error code, I tried

 public function render($request, Exception $exception) { if ($exception->getCode() === 422) { return response([], 356); //should throw 356 error } return parent::render($request, $exception); // instead this return fires and gives me 422 error } 

Vue component

my template:

<template> <form class="form" @submit.prevent="submit()"> <input type="text" class="form-control-plaintext textField" placeholder="Post a comment" required v-model="textField"> <button :disabled="disabled" class="btn btn-success submit" type="submit">Post</button> <button :disabled="disabled" class="btn btn-dark submit" type="reset">Cancel</button> </form> </template> 

my axios method:

 axios .post('/comments', { textField: this.textField, id: this.id, routeName: this.routename, }) .then(response => { this.name = ''; this.callReload(this.id); this.textField = ''; }) .catch(function (error) { console.log(error) }); 
4
  • Does this answer your question? Laravel Redirect Back with() Message Commented Nov 9, 2019 at 20:45
  • I tried every solution in your provided answer without luck, the problem probably is not about syntax, but approach, I can log to console any error code like so return response([], 389);, but can't redirect or make a response in json format Commented Nov 9, 2019 at 21:35
  • Can you provide the code for your vue component. Commented Nov 9, 2019 at 22:32
  • Of course, component does the job without errors if rules of form request are met Commented Nov 9, 2019 at 22:39

1 Answer 1

1

You probably want to change the render function, which I assume is from App\Exceptions\Handler.php to return JSON for exceptions where the request was made via JSON.

public function render($request, $exception) { if ($request->isJson()) { return response()->json(['error' => 'Error Detail'], 422); } } 

You will also want to check the exception type rather than simply returning a 422 status code for any JSON exception.

Sign up to request clarification or add additional context in comments.

2 Comments

But I actually had to do return response()->json(['error' => 'Error Detail'], 200); in order to access message, 422 status just throws an error.
Also I intended to display error message in blade, that's why redirect's didn't work, I actually have to handle them in Vue

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.