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));
Related
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.
How can I find the culprit of the following error in Laravel?
Trying to get property of a non-object Error
This is most probably coming from calling a method\property on a null object. So to fail early, you should use Model::firstOrFail(); or Model::findOrFail(ID);.
Other than that a null check can do it before you use, but it gets ugly if you have many null checks in your code.
try {} catch (\Exception $e) {} is also a way to catch an exception and handle it manually, but doing this in many places is again a lot of work.
What I do is use a ternary operation on the model before accessing properties of that model like so
$model = Model::find($id); $model ? $name = $model->name : null;
with this you can always be rest assured that a fatal throwable error would not be thrown if the model is not found. This also means you have to check if the variable name is not null before working with it like so if(!is_null($name) { //do your stuff here}
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()
}
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!
Say you have a simple resource route like this:
Route::resource('overview', 'OverviewController');
And hit routes which you know don't exist. For example:
/overview/sdflkjdsflkjsd
/overview/sdflkjdsflkjsd/edit
Which in my case throws Trying to get property of non-object error from my view (as no resource is found)
I looked into adding 'Regular Expression Parameter Constraints' from the docs, but it looks like these are not available for resource routes either (plus don't really fix the problem).
I'm looking for a way to throw a single exception for this kind of thing, which I can then handle once, rather than add logic to each action (or at least the show and edit actions).. if possible.
EDIT After looking around github, I found the exception in the Symphony repo here. Is there a way I can hook into it?
Since you're getting a Trying to get property of non-object error, I assume you're fetching the resource via YourModel::find();
I'd suggest you use YourModel::findOrFail() instead. Then, you'd be getting a specific type of exception called ModelNotFoundException. Just register an error handler for this.
For instance,
App::error(function(ModelNotFoundException $e)
{
return Response::make('Not Found', 404);
});
UPDATE: This would actually go into render() method inside the app/Exceptions/Handler.php file in Laravel 5.1, and of course the code would utilize the passed $e parameter instead.
public function render($request, Exception $e)
{
if ($e instanceof ModelNotFoundException)
{
return \Response::make('Not Found', 404);
}
return parent::render($request, $e);
}