Laravel - catch the custom abort message - laravel

I'm using the abort function to send a custom exception message from my service layer.
if($max_users < $client->users()->count()){
return abort(401, "Max User Limit Exceeded");
}
But how do I catch this message in my controller, which is in a different application
try{
$clientRequest = $client->request(
'POST',
"api/saveClientDetails",
[
'headers' => [
'accept' => 'application/json',
'authorization' => 'Bearer ' . $user['tokens']->access_token
],
'form_params' => $data,
]
);
} catch ( \GuzzleHttp\Exception\ClientException $clientException ){
switch($clientException->getCode()){
case 401:
\Log::info($clientException->getCode());
\Log::info($clientException->getMessage());
abort(401);
break;
default:
abort(500);
break;
}
}
The above code prints the following for the message:
But it prints
Client error: `POST http://project-service.dev/api/saveClientDetails` resulted in a `401 Unauthorized` response:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="robots" content="noindex,nofollow (truncated...)

Well you are trying to catch a Guzzle Exception instead when you should be catching a Symfony HttpException. Maybe try something like this:
catch(\Symfony\Component\HttpKernel\Exception\HttpException $e)
{
\Log::info($e->getMessage());
}
Based on your comment, I tried the following:
public function test()
{
try
{
$this->doAbort();
}
catch (\Symfony\Component\HttpKernel\Exception\HttpException $e)
{
dd($e->getStatusCode(), $e->getMessage());
}
}
public function doAbort()
{
abort(401, 'custom error message');
}
and the output is:
401
"custom error message"
Based on your comment, here's how it works for me.
Route::get('api', function() {
return response()->json([
'success' => false,
'message' => 'An error occured'
], 401);
});
Route::get('test', function() {
$client = new \GuzzleHttp\Client();
try
{
$client->request('GET', 'http://app.local/api');
}
catch (\Exception $e)
{
$response = $e->getResponse();
dd($response->getStatusCode(), (string) $response->getBody());
}
});
This outputs the status code and the correct error message. If you use abort it will still return the full HTML response. A better approach would be to return a well-formatted JSON response.
Let me know if it works for you now :)

Related

How to catch server's error in inertiajs request?

In laravel 8 / inertiajs 0.10 / vue 3 I want to catch some error which happens on server, like :
this.form.post(this.route('ads.store'), {
preserveScroll: true,
onSuccess: (p) => { // On succdess I see this message
console.log('onSuccess p::')
console.log(p)
Swal.fire(
'New Ad',
'Your post has successfully been published!',
'success'
)
this.form.description = null
},
onError: (p) => { // That is not triggered!
console.log('onError p::')
console.log(p)
}
})
In control :
public function store( AdFormRequest $request) {
$data = $request->all();
$data['status']= 'A';
$ad = Ad::create($data);
return $request->wantsJson()
? new JsonResponse($ad, 200)
: back()->with('status', 'Error saving add');
}
So if one of required fields is empty I got laravel error popup window...
How to catch it and to show it in Swal.fire ?
MODIFIED # 1:
Searching in net I found onError property, but making :
this.deleteForm.delete(this.route('ads.destroy', this.nextAd), {
preserveScroll: true,
onError: (p) => { // THIS PART IS NOT CALLED ON ERROR
console.log('onError p::')
console.log(p)
Swal.fire(
'Delete Ad',
'Error deleting ad!',
'error'
)
},
onSuccess:()=>{
Swal.fire( // THIS PART IS CALLED ON SUCCESS
'Delete Ad',
'Your post has successfully been Delete!',
'success'
)
}
})
and in control :
public function destroy(Request $request, Ad $ad) {
try {
DB::beginTransaction();
$ad->deleTTTte();
DB::commit();
} catch (QueryException $e) {
DB::rollBack(); // I SEE THIS MESSAGE IN LOG FILE ON ERROR
\Log::info( '-1 AdController store $e->getMessage() ::' . print_r( $e->getMessage(), true ) );
return $request->wantsJson()
? new JsonResponse($ad, 500 /*HTTP_RESPONSE_INTERNAL_SERVER_ERROR*/ )
: back()->with('status', 'Error adding ad : ' . $e->getMessage());
return;
}
return $request->wantsJson()
? new JsonResponse($ad, HTTP_RESPONSE_OK)
: back()->with('status', 'Ad saved succesully');
}
Which way is correct?
Thanks!
The onError() callback is only called when there is a specific flag in the session called errors. So, in you server's catch block you need to flash the errors in the user's session and redirect to your calling page:
try {
// errors throw here
} catch {
return back()->with('errors', 'Error adding ad');
}

How to change the response messages in Tymon JWT package laravel

I want to change response messages in the Tymon JWT package. For example, while fetching the data with Invalid token I am getting this response
"message": "Invalid token.",
"exception": "Tymon\\JWTAuth\\Exceptions\\TokenInvalidException",
I need o change this from above response to below response
"errors": "Invalid token.",
"exception": "Tymon\\JWTAuth\\Exceptions\\TokenInvalidException",
controller code
try {
$assign = AssignmentResource::collection(DB::table('assignments')->whereIn('assignments.academic_id',$ids)
->whereIn('assignments.batch',$batch)
->whereIn('assignments.course',$classid)->get());
} catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {
return response()->json(['success' => false,'errors' => $e,'status' => 404] );
} catch (Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {
return response()->json(['success' => false,'errors' =>$e,'status' => 404] );
} catch (Tymon\JWTAuth\Exceptions\JWTException $e) {
return response()->json(['success' => false,'errors' =>$e,'status' => 404] );
}
thank you in advance
You can customize the laravel Exceptions.
inside app/Exceptions/Handler.php you can customize your message.
public function render($request, Exception $exception)
{
if ($request->is('api/*') || $request->expectsJson() || $request->is('webhook/*')) {
if ($exception instanceof Tymon\JWTAuth\Exceptions\TokenInvalidExceptio) {
return [
'errors' => $exception->getMessage(),
'exception' => 'your message'
];
}
}
}
If you see Exception Handling page of Tymon JWT Auth, then it is coming soon:
One way you can achieve this is like using try..catch:
try {
// Your code here.
} catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {
// return your response.
} catch (Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {
// return your response.
} catch (Tymon\JWTAuth\Exceptions\JWTException $e) {
// return your response.
}

Laravel-Vue: How can I use try-catch block to catch validation exception in Validation Requests?

I'm using vue.js. I'm hitting my server with axios like:
try{
const resp = await axios.post('storeProduct', data,
{
headers : header(state)
});
console.log(resp);
}catch(error){
console.log("you are at error");
console.log(error);
}
Here, I'm console logging the error where I get error 422 but I want to get the message as well. If I use try catch in a simple validation it works. But cant make it working with the Validation Request object.
In my Controller: 'ProductRequest' is the validation object which has validation rules. It gives me the errors but can't catch in the try-block of axios in vue.
public function storeProduct(ProductRequest $request){
try{
return $controller->saveProducts($request);
}catch(\Exception $e){
return $e;
}
}
ProductRequest.php
public function rules()
{
try{
return validation_value('add_products');
}catch(\Exception $e){
return $e;
}
}
Is there anyway that I could return the error message from here and catch it in my 'vue axios try/catch block'
Try it like this :
axios.post('storeProduct', data,
{
headers : header(state)
})
.then(response => console.log(response.data))
.catch(error => {
if (error.response) {
console.log("you are at error");
console.log(error.response);
}
});

Laravel API, how to properly handle errors

Anyone know what is the best way to handle errors in Laravel, there is any rules or something to follow ?
Currently i'm doing this :
public function store(Request $request)
{
$plate = Plate::create($request->all());
if ($plate) {
return $this->response($this->plateTransformer->transform($plate));
} else {
// Error handling ?
// Error 400 bad request
$this->setStatusCode(400);
return $this->responseWithError("Store failed.");
}
}
And the setStatusCode and responseWithError come from the father of my controller :
public function setStatusCode($statusCode)
{
$this->statusCode = $statusCode;
return $this;
}
public function responseWithError ($message )
{
return $this->response([
'error' => [
'message' => $message,
'status_code' => $this->getStatusCode()
]
]);
}
But is this a good way to handle the API errors, i see some different way to handle errors on the web, what is the best ?
Thanks.
Try this, i have used it in my project (app/Exceptions/Handler.php)
public function render($request, Exception $exception)
{
if ($request->wantsJson()) { //add Accept: application/json in request
return $this->handleApiException($request, $exception);
} else {
$retval = parent::render($request, $exception);
}
return $retval;
}
Now Handle Api exception
private function handleApiException($request, Exception $exception)
{
$exception = $this->prepareException($exception);
if ($exception instanceof \Illuminate\Http\Exception\HttpResponseException) {
$exception = $exception->getResponse();
}
if ($exception instanceof \Illuminate\Auth\AuthenticationException) {
$exception = $this->unauthenticated($request, $exception);
}
if ($exception instanceof \Illuminate\Validation\ValidationException) {
$exception = $this->convertValidationExceptionToResponse($exception, $request);
}
return $this->customApiResponse($exception);
}
After that custom Api handler response
private function customApiResponse($exception)
{
if (method_exists($exception, 'getStatusCode')) {
$statusCode = $exception->getStatusCode();
} else {
$statusCode = 500;
}
$response = [];
switch ($statusCode) {
case 401:
$response['message'] = 'Unauthorized';
break;
case 403:
$response['message'] = 'Forbidden';
break;
case 404:
$response['message'] = 'Not Found';
break;
case 405:
$response['message'] = 'Method Not Allowed';
break;
case 422:
$response['message'] = $exception->original['message'];
$response['errors'] = $exception->original['errors'];
break;
default:
$response['message'] = ($statusCode == 500) ? 'Whoops, looks like something went wrong' : $exception->getMessage();
break;
}
if (config('app.debug')) {
$response['trace'] = $exception->getTrace();
$response['code'] = $exception->getCode();
}
$response['status'] = $statusCode;
return response()->json($response, $statusCode);
}
Always add Accept: application/json in your api or json request.
Laravel is already able to manage json responses by default.
Withouth customizing the render method in app\Handler.php you can simply throw a Symfony\Component\HttpKernel\Exception\HttpException, the default handler will recognize if the request header contains Accept: application/json and will print a json error message accordingly.
If debug mode is enabled it will output the stacktrace in json format too.
Here is a quick example:
<?php
...
use Symfony\Component\HttpKernel\Exception\HttpException;
class ApiController
{
public function myAction(Request $request)
{
try {
// My code...
} catch (\Exception $e) {
throw new HttpException(500, $e->getMessage());
}
return $myObject;
}
}
Here is laravel response with debug off
{
"message": "My custom error"
}
And here is the response with debug on
{
"message": "My custom error",
"exception": "Symfony\\Component\\HttpKernel\\Exception\\HttpException",
"file": "D:\\www\\myproject\\app\\Http\\Controllers\\ApiController.php",
"line": 24,
"trace": [
{
"file": "D:\\www\\myproject\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\ControllerDispatcher.php",
"line": 48,
"function": "myAction",
"class": "App\\Http\\Controllers\\ApiController",
"type": "->"
},
{
"file": "D:\\www\\myproject\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php",
"line": 212,
"function": "dispatch",
"class": "Illuminate\\Routing\\ControllerDispatcher",
"type": "->"
},
...
]
}
Using HttpException the call will return the http status code of your choice (in this case internal server error 500)
In my opinion I'd keep it simple.
Return a response with the HTTP error code and a custom message.
return response()->json(['error' => 'You need to add a card first'], 500);
Or if you want to throw a caught error you could do :
try {
// some code
} catch (Exception $e) {
return response()->json(['error' => $e->getMessage()], 500);
}
You can even use this for sending successful responses:
return response()->json(['activeSubscription' => $this->getActiveSubscription()], 200);
This way no matter which service consumes your API it can expect to receive the same responses for the same requests.
You can also see how flexible you can make it by passing in the HTTP status code.
If you are using Laravel 8+, you can do it simply by adding these lines in Exception/Handler.php on register() method
$this->renderable(function (NotFoundHttpException $e, $request) {
if ($request->is('api/*')) {
return response()->json([
'message' => 'Record not found.'
], 404);
}
});
For me, the best way is to use specific Exception for API response.
If you use Laravel version > 5.5, you can create your own exception with report() and render() methods. Use command:
php artisan make:exception AjaxResponseException
It will create AjaxResponseException.php at: app/Exceptions/
After that fill it with your logic. For example:
/**
* Report the exception.
*
* #return void
*/
public function report()
{
\Debugbar::log($this->message);
}
/**
* Render the exception into an HTTP response.
*
* #param \Illuminate\Http\Request $request
* #return JsonResponse|Response
*/
public function render($request)
{
return response()->json(['error' => $this->message], $this->code);
}
Now, you can use it in your ...Controller with try/catch functionality.
For example in your way:
public function store(Request $request)
{
try{
$plate = Plate::create($request->all());
if ($plate) {
return $this->response($this->plateTransformer->transform($plate));
}
throw new AjaxResponseException("Plate wasn't created!", 404);
}catch (AjaxResponseException $e) {
throw new AjaxResponseException($e->getMessage(), $e->getCode());
}
}
That's enough to make your code more easier for reading, pretty and useful.
Best regards!
For Laravel 8+ in file App\Exceptions\Hander.php inside method register() paste this code:
$this->renderable(function (Throwable $e, $request) {
if ($request->is('api/*')) {
return response()->json([
'message' => $e->getMessage(),
'code' => $e->getCode(),
], 404);
}
});
I think it would be better to modify existing behaviour implemented in app/Exceptions/Handler.php than overriding it.
You can modify JSONResponse returned by parent::render($request, $exception); and add/remove data.
Example implementation:
app/Exceptions/Handler.php
use Illuminate\Support\Arr;
// ... existing code
public function render($request, Exception $exception)
{
if ($request->is('api/*')) {
$jsonResponse = parent::render($request, $exception);
return $this->processApiException($jsonResponse);
}
return parent::render($request, $exception);
}
protected function processApiException($originalResponse)
{
if($originalResponse instanceof JsonResponse){
$data = $originalResponse->getData(true);
$data['status'] = $originalResponse->getStatusCode();
$data['errors'] = [Arr::get($data, 'exception', 'Something went wrong!')];
$data['message'] = Arr::get($data, 'message', '');
$originalResponse->setData($data);
}
return $originalResponse;
}
Well, all answers are ok right now, but also they are using old ways.
After Laravel 8, you can simply change your response in register() method by introducing your exception class as renderable:
<?php
namespace Your\Namespace;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
/**
* Register the exception handling callbacks for the application.
*
* #return void
*/
public function register()
{
$this->renderable(function (NotFoundHttpException $e, $request) {
if ($request->is('api/*')) {
return response()->json([
'message' => 'Record not found.'
], 404);
}
});
}
}
Using some code from #RKJ best answer I have handled the errors in this way:
Open "Illuminate\Foundation\Exceptions\Handler" class and search for a method named "convertExceptionToArray". This method converts the HTTP exception into an array to be shown as a response. In this method, I have just tweaked a small piece of code that will not affect loose coupling.
So replace convertExceptionToArray method with this one
protected function convertExceptionToArray(Exception $e, $response=false)
{
return config('app.debug') ? [
'message' => $e->getMessage(),
'exception' => get_class($e),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => collect($e->getTrace())->map(function ($trace) {
return Arr::except($trace, ['args']);
})->all(),
] : [
'message' => $this->isHttpException($e) ? ($response ? $response['message']: $e->getMessage()) : 'Server Error',
];
}
Now navigate to the App\Exceptions\Handler class and paste the below code just above the render method:
public function convertExceptionToArray(Exception $e, $response=false){
if(!config('app.debug')){
$statusCode=$e->getStatusCode();
switch ($statusCode) {
case 401:
$response['message'] = 'Unauthorized';
break;
case 403:
$response['message'] = 'Forbidden';
break;
case 404:
$response['message'] = 'Resource Not Found';
break;
case 405:
$response['message'] = 'Method Not Allowed';
break;
case 422:
$response['message'] = 'Request unable to be processed';
break;
default:
$response['message'] = ($statusCode == 500) ? 'Whoops, looks like something went wrong' : $e->getMessage();
break;
}
}
return parent::convertExceptionToArray($e,$response);
}
Basically, we overrided convertExceptionToArray method, prepared the response message, and called the parent method by passing the response as an argument.
Note: This solution will not work for Authentication/Validation errors but most of the time these both errors are well managed by Laravel with proper human-readable response messages.
In your handler.php This should work for handling 404 Exception.
public function render($request, Throwable $exception ){
if ($exception instanceof ModelNotFoundException) {
return response()->json([
'error' => 'Data not found'
], 404);
}
return parent::render($request, $exception);
}
You don't have to do anything special. Illuminate\Foundation\Exceptions\Handler handles everything for you. When you pass Accept: Application/json header it will return json error response. if debug mode is on you will get exception class, line number, file, trace if debug is off you will get the error message. You can override convertExceptionToArray. Look at the default implementation.
return config('app.debug') ? [
'message' => $e->getMessage(),
'exception' => get_class($e),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => collect($e->getTrace())->map(function ($trace) {
return Arr::except($trace, ['args']);
})->all(),
] : [
'message' => $this->isHttpException($e) ? $e->getMessage() : 'Server Error',
];
As #shahib-khan said,
this happens in debug mode and is handled by Laravel in production mode.
you can see base method code in
\Illuminate\Foundation\Exceptions\Handler::convertExceptionToArray
protected function convertExceptionToArray(Throwable $e)
{
return config('app.debug') ? [
'message' => $e->getMessage(),
'exception' => get_class($e),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => collect($e->getTrace())->map(fn ($trace) => Arr::except($trace, ['args']))->all(),
] : [
'message' => $this->isHttpException($e) ? $e->getMessage() : 'Server Error',
];
}
Therefore, I overrode the the convertExceptionToArray function in app/Exceptions/Handler
of course, still in debug mode, if the exception is thrown, you can track it in the Laravel.log.
protected function convertExceptionToArray(Throwable $e)
{
return [
'message' => $this->isHttpException($e) ? $e->getMessage() : 'Server Error',
];
}
Add header to your API endpoint. which works for me. it will handle the error request properly.
Accept: application/json

Lumen can't set custom validation rules

I try this code to set custom validation messages, but with no effect -
class TestController extends Controller
{
public function submit(Request $request)
{
$this->validate($request,
[
'items' => 'required'
],
[
'items.required' => 'test test'
]
);
}
}
But on error I got this response -
{
"error": "The given data failed to pass validation."
}
What wrong with this code?
UPD:
Earlier I edit App\Exceptions\Handler to put errors(in API response) in specific format -
{
"error": "123"
}
This code is reason that validation errors not shown -
public function render($request, Exception $e)
{
return response([
'error' => $e->getMessage()
], 500);
}
I update Handler::render method regarding to this purpose
public function render($request, Exception $e) {
$response = parent::render($request, $e);
if (isset($response->exception) and !empty($response->exception)) {
return response(['error' => $response->exception->getMessage()], 500);
} else {
return parent::render($request, $e);
}
}
But I think I need to improve this code.
It seems you mixed validation rules and messages.
The validate method takes 3 parameter: request, rules, messages.
Please try this:
public function submit(Request $request)
{
$rules = [
'items' => 'required',
'otheritems' => 'required',
];
$messages = [
'items.required' => 'Error: Please enter something.',
'otheritems.required' => 'Otheritems are also required',
];
$this->validate($request, $rules, $messages);
}
The latest Lumen version always gives back JSON, see documentation:
The $this->validate helper will always return JSON responses with the relevant error messages when validation fails. If you are not building a stateless API that solely sends JSON responses, you should use the full Laravel framework.
Update regarding error:
The given result by Lumen looks like that.
{"items":["Items are required"],"otheritems":["Otheritems are also required"]}
Each item that failed the validation gets an entry in your response. So your error bag, need to be a JSON array.
Custom exception render method:
public function render($request, Exception $e)
{
$response = parent::render($request, $e);
if ($response->getStatusCode() == 422) {
$renderResult = parent::render($request, $e);
$returnResult['error'] = json_decode($renderResult->content(), true);
$returnResult = json_encode($returnResult);
return new Response($returnResult, $response->getStatusCode());
} else {
return parent::render($request, $e);
}
}

Resources