In my package I perform a check on a user id:
//check
if(!$this->checkId($id)) //error
If this fails I need to throw an error as the method in my package will fail to work and I need to inform the user.
Please note, this is not a eloquent query so I do not need any find or fail methods.
How can I do this in laravel?
I agree with the previous answer, but I would throw an exception from checkId() method - since either check passes or fails (and throws exception).
class CheckIdException extends Exception
{
}
class WhateverClass
{
public function checkId($id)
{
// do the check
$passes = ....
if (! $passes) {
throw new CheckIdException('CheckId() failed');
}
return true;
}
}
// somewhere in the app code
try {
$this->checkId($id);
} catch (CheckIdException $e) {
return Response::json(['error' => 'checkId', 'message' => 'meaningul error description']);
} catch (Exception $e) {
return Response::json(['error' => 'UnknownError', 'message' => $e->getMessage()]);
}
// yay, ID check passes! Continue!
...so just throw an error?
if(!$this->checkId($id)) //error
{
App::abort(500, 'CheckId() failed');
}
or
if(!$this->checkId($id)) //error
{
throw new Exception("CheckId() failed");
}
Related
On laravel 9 site in my custom class method raise custom error with error message
<?php
class CustomClass
{
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Exceptions\HttpResponseException;
...
public function method(): array
{
if(false) {
throw new HttpResponseException(response()->json([
'message' => $validated['message'],
], 422));
}
but I failed to catch this error in control where methods raised :
{
try {
$imageUploadSuccess = $customClass->method(
...
); // This method can raise different types of exception
}
catch (ModelNotFoundException $e) { // exception with invalid id is catched - THIS PART WORKS OK
return response()->json([
'message' => 'Item not found',
], 404);
}
catch (HttpResponseException $e) { // I catch HttpResponseException with is generated in method
// None of these 2 methods returns custom error message in method :
\Log::info($e->getMessage()); // empty value - I NEED TO GET THIS MESSAGE TEXT
\Log::info($e->getCode()); // 0 value
\Log::info($e->getTraceAsString());
return response()->json([
'message' => $e->getMessage(),
], 422);
}
return response()->json(['success' => true], 400);
Have I to use other class, not HttpResponseException ? I which way ?
Thanks in advance!
You’re not throwing your custom exception so it’s never raised so it won’t be catchable.
Replace where you throw the HttpResponseException with your Exception.
<?php
class CustomClass
{
public function method()
{
$isValid = false;
if(!$isValid) {
throw new SomeCustomException(response()->json([
'message' => $validated['message'],
], 422));
}
}
}
You would then use do something like:
$customClass = new CustomClass();
$customClass->method();
Note how I have defined a variable $isValid and how I check it for a true or false value in the if statement. This is because if checks for truthy values and false will never be `true so your custom exception would never be thrown.
if (false) {
// code in here will never execute
}
I created an exception called invalid balance, and I'm using like the following. My output result status is 500. How can I change this status to 400?
try {
$balance = Wallet::findOrFail()->docs()
->sum('amount');
if ($balance == 0) {
throw new InvalidBalance();
}
} catch (QueryException $e) {
$message = Str::contains($e->getMessage(), 'Deadlock') ?
'Server is busy' : $e->getMessage();
throw new HttpException(400, $message);
} catch (\Exception $e) {
throw $e;
}
You can use the abort helper.
if ($balance === 0)
{
abort(400, 'Bad Request.');
}
Or within the InvalidBalance class do the abort there.
You can use response() method and pass the http status code as the second parameter as in laravel helpers functions
return response()->json(['message'=>'your message'], 400);
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.
}
Guzzle http is truncating exceptions with more than 120 characters, but I need to log the full exception message. How can I do this?
I am using laravel 4.2.22.
try {
// whatever
} catch (\GuzzleHttp\Exception\RequestException $ex) {
return $ex->getResponse()->getBody()->getContents();
// you can even json_decode the response like json_decode($ex->getResponse()->getBody()->getContents(), true)
}
It is the same for Laravel 5 and 4
try {
$response = $client->post($path, $params);
} catch (\GuzzleHttp\Exception\RequestException $ex) {
\Log::debug('error');
\Log::debug((string) $ex->getResponse()->getBody());
throw $ex;
}
if you just go to $ex->getMessage(), you will get
(truncated...)
at the end.
Might be better solution:
try {
// do request here like:
// return $client->post($path, $params);
} catch (\GuzzleHttp\Exception\ServerException $ex) {
$exFactoryWithFullBody = new class('', $ex->getRequest()) extends \GuzzleHttp\Exception\RequestException {
public static function getResponseBodySummary(ResponseInterface $response)
{
return $response->getBody()->getContents();
}
};
throw $exFactoryWithFullBody->create($ex->getRequest(), $ex->getResponse());
}
I want to set different view blade template for different exception and also pass the errors on the following page. I tried out the following code but it's not working. It always goes to the else portion of the code and run the parent::render($request, $e);code.
public function render($request, Exception $e)
{
if ($this->isHttpException($e))
{
if($e instanceof InvalidArgumentException)
{
return response()->view('front.missing', [], 404);
}elseif($e instanceof ErrorException){
return response()->view('front.missing2', [], 404);
}
return $this->renderHttpException($e);
}else{
if($e instanceof InvalidArgumentException)
{
return response()->view('errors.204', []);
}
return parent::render($request, $e);
}
}
Where is the problem here and what I will do now?
InvalidArgumentException is not a child class of HttpException, so it always goes to the else portion.