Lumen QueryException write to log - laravel

I'm using Lumen 5.3.* and I need to make QueryException write errors on the storage/logs/lumen.log.
Other validations are writing to the log file except for QueryException.
try {
// throw here exception here
} catch (QueryException $e) {
// do what ever
} catch (CustomException $e) {
// do what ever
}
I have my CustomException extended to \Exception and use Handler class to to do report() as to why I am able to write to the log file. But the QueryException does not. (QueryException is a lumen vendor file)
I know it is possible to just to \Log() upon catch on the QueryException but I want to make things cleaner by not putting it this way.
Is there another way to do this aside from doing what was stated above?
Thanks in advance!

Related

how to make multiple delete or update or insert in one transaction in yii2 using active records

is it the right way in my code to do multiple statement in one transaction using active records?
cause I don't know how to make it like that
please advice, thanks
$transaction = Yii::$app->db->beginTransaction();
try {
//.... active record operations
$transaction->commit();
} catch (\Exception $e) {
$transaction->rollBack();
throw $e;
} catch (\Throwable $e) {
$transaction->rollBack();
throw $e;
}
Note from Yii2 doc: in the above code we have two catch-blocks for compatibility with PHP 5.x and PHP 7.x. \Exception implements the \Throwable interface since PHP 7.0, so you can skip the part with \Exception if your app uses only PHP 7.0 and higher.
References:
https://www.yiiframework.com/doc/api/2.0/yii-db-transaction
https://www.yiiframework.com/doc/guide/2.0/en/db-active-record#transactional-operations

Laravel: Way to diagnose "uncaught reflection: class log does not exist" without guessing?

As we all painfully know, this message is generated when an error occurs before Laravel has had a chance to instantiate a "Log" class instance to handle it. And... it therefore seems to completely conceal just what the underlying error is!
In my case the php artisan command won't run either.
Is there any way to find out what's wrong without "blind guessing?"
When you want to handle exceptions, a good way to catch it would be to implement a way to trapping exception out of controllers or services. You can do it in the "render" method of the App\Exceptions\Handler class. In this "render" method, you can write a block of "if" code to show the messages generated by an exception when Laravel throws an exception. For example:
public function render($request, Exception $exception)
{
if($exception) {
// do something
return response()->json(['error' => $exception->getMessage(),
$exception->getTraceAsString()], 500);
}
// Or if you created an exception specialization
if ($exception instanceof MyCustomException) {
return response()->view('errors.custom', [], 500);
}
return parent::render($request, $exception);
}
This was indeed caused by a syntax error, and I'm really surprised that Laravel had even managed to "get started" by that point in time. I literally found it by looking with git at a list of files that had recently changed.

Laravel redirect ALL types of errors to custom view

I'm looking for a way to catch all possible errors and redirect all types of errors to 1 page.
My current code in /Exceptions/Handler.php:
if ($this->isHttpException($exception)) {
$statusCode = $exception->getStatusCode();
switch ($statusCode) {
case '404':
return response()->view('layouts/404');
}
}
Problem is that ErrorException (E_NOTICE) types (which are caused by possible bugs in the code) aren't redirected to the 404 page. These errors end up on the 'Woops something went wrong' page.
I basically am trying to make every type of error end up on my custom error page.
All attempts end up on white pages.
What am I not seeing?
Well, to do what you're asking for you should go to App\Exceptions\Handler.php and there, you need to modify the render method:
public function render($request, Exception $exception)
{
abort(404);
}
But I insist, this is a terrible idea, you should catch the Exceptions that your app may get and show the error message to the user in a clean way, for example:
try{
//... your fancy code here
}catch(Exception $e){
// return with the message error $e->getMessage()
}

How to redirect another page instead of redirecting to abort 403 error page in Laravel 5.4

My question is whenever there is a 403 error I should redirect to my own custom page or show the flash message(on the same page) something like that.How can I achieve this in Laravel?.
You can use app/Exceptions/Handler.php for this
public function render($request, Exception $e)
{
if ($e->getStatusCode() == 403) {
return redirect('yourpage'); // this will be on a 403 exception
}
return parent::render($request, $e); // all the other exceptions
}
You can create a view for specific HTTP error codes. If you set up a Blade template at resources/views/errors/403.blade.php, it will get used for all 403 error responses.
Source
Alternatively you can set up a custom exception handler for 403 responses if you need something more involved. I found a good example of this here.
you can make you own page in inside the view .resources/views/errors/yourblade.blade.php
after that just return your page to that page.
You can use app/Exceptions/Handler.php for this as Thomas Moors stated above or you can use a try catch for basic error handling.
try {
do Something //
}
catch (Exception $e) {
//log error in log file or dv
return redirect->('home');
}

Throwing errors in Laravel

In my package, before I perform a query on my database I check if some of the params provided are valid:
//check
if(!$this->checkId($id)) //error
//do the query
$user = User::find($id);
What's the best way to handle this in a laravel package?
Throw an error? If so, which kind? Do an app abort?
Using findOrFail()
There is a pretty good method that Laravel provides just for this case called findOrFail().
What you can do is use that and then catch the exception.
try {
$user = User::findOrFail($queryParam);
} catch(ModelNotFoundException $e) {
// Do things that should happen here if your query parameter does not exist.
}
If you do not wish to catch it here, you can setup a listener for this exception. You can place it anywhere you wish as long as it's being autoloaded.
use Illuminate\Database\Eloquent\ModelNotFoundException;
App::error(function(ModelNotFoundException $e)
{
return Response::make('Not Found', 404);
});
Throwing an exception
If all you want to do is throw an error if a query param isn't available, you can throw any of the exceptions found here... https://github.com/symfony/symfony/tree/master/src/Symfony/Component/HttpKernel/Exception
If none suit you, you can create your own and just extend Symfony\Component\HttpKernel\Exception\HttpException and throw new YourCustomException($statusCode, $message);. Just follow what they've done in the other exception classes and it should be fairly easy.
Then you can modify the the "exception listener" to catch YourCustomException.
Here is a tutorial I found... http://fideloper.com/laravel4-error-handling
I would use Event Listeners for the most control.
Create an events.php in your app directory and include it in app/start/global.php
Or, you can register anywhere in your package.
Create a listener:
Event::listen('paramCheck.noId', function($id)
{
// do some logic, log something, return something, throw an error...
});
Then fire the event from whenever you need to:
if(!$this->checkId($id))
$returnFromEvent = Event::fire('paramCheck.noId', array($id));

Resources