arosemena's avatar

How the hell do you debug properly in laravel

I mean if the only thing i get from the logs is

local.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Method Illuminate\View\View::__toString() must not throw an exception' in /home/app/laravel/app/controllers/AdminItemController.php:0
Stack trace:
#0 [internal function]: Illuminate\Exception\Handler->handleShutdown()
#1 {main} [] []

how am i supposed to know which view threw the exception and what cause the actual exception in the view, it's getting really frustrating when errors like this happen and the log tells you nothing

0 likes
19 replies
ATOM-Group's avatar

I absolutely loathe errors and exceptions in Laravel because of this. All of the lazy loaded function calls and events and closures etc make it virtually impossible to get accurate, meaningful errors that pinpoint the real issue.

This is something I would somehow like Taylor to spend an entire minor version addressing, somehow.

I also feel like stack traces don't really show you the full execution path. I find it hard to believe that when an error occurs, only 10 or so functions/methods are actually called (and yes, I would like to see the full trace, no matter how insignificant the method might be).

9 likes
Devon's avatar

One approach is to use TDD (Test Driven Development), there are tons of videos on building tests on Laracasts, it can help isolate where errors occur...

Another approach is to simply dd('Got here!') throughout the execution of your code. If you get the exception, the dd() is after it occurs. If you get a screen that says 'Got here!', the exception is after the dd(). This is a very raw approach, but it does work in nearly all circumstances.

Part of it comes down to trial and error and simply knowing your code.

What did you recently add that caused it to begin showing that error?

1 like
arosemena's avatar

i solved it but had to dig through every one of the 10 views and check everything manually, i had the name of one variable wrong in the blade object, it is really annoying that laravel couldn't just say that instead of sending me in a manual check

1 like
Devon's avatar

Yea, I was thinking it was a typo, kinda hard to pin-point without seeing the code though.

Glad you got it fixed! :)

slovenianGooner's avatar

This is one of the most annoying errors, yes. If at least a view, that throws the error would be specified or something similar, then the debugging would be much easier.

bashy's avatar

If you're working on one section of code, it works before saving and doesn't afterwards... :P Probably working on too many files at a time before testing.

2 likes
ccasselman's avatar

"Knowing your code" and "what did you do last" doesn't scale for large teams. I'd love some improvements in this area as well.

11 likes
olyckne's avatar

I guess xdebug maybe might help in these kind of situations? I tend to forget to learn and use it, but I think there is at least one video about it here.

acasar's avatar

Use this:

return View::make('my.view')->render();

By explicitly calling render(), you will get much better error messages.

12 likes
bashy's avatar

"Knowing your code" and "what did you do last" doesn't scale for large teams. I'd love some improvements in this area as well.

Sure but when would two or more people be working on the same file? Hopefully you don't develop on the same files as everyone else...

1 like
arosemena's avatar

Interesting @anzze i always used (string)View to cast it to string before injecting it to json, i'll try yours from now on

rspahni's avatar

I'm using [PhpStorm, ] Xdebug + breakpoints as the slicker variant of dd'ing ("Got here!"). I.e. I follow the call stack with as many Step Overs as possible and as few Step Intos as needed. When I run into the given issue, I'm too far through the stack and repeat by way of putting breakpoints ever "closer" to the issue and removing those before. After a few passes I'll have a convenient breakpoint a couple of steps away from the issue and can inspect variables etc. right before it happens. This, to me, has been the only way to e.g. debug Service Provider register/boot methods, which can throw badly misleading exceptions.

1 like
MarkNZed's avatar

Hi, this is incredibly painful! Even with Xdebug and PhpStorm. I've hacked PhpEngine.php to give something much more useful in this case:

--- vendor/laravel//framework/src/Illuminate/View/Engines/PhpEngine.php 2014-10-16 10:05:50.000000000 +0200
+++ PhpEngine.php 2014-10-16 10:06:41.000000000 +0200
@@ -38,6 +38,10 @@
                }
                catch (\Exception $e)
                {
+ // Add some half decent error reporting..
+ error_log("Exception ".$e->getMessage());
+ error_log(" File ".$e->getFile()." Line ".$e->getLine());
+ error_log(" Trace ".$e->getTraceAsString()." Line ".$e->getLine());
                        $this->handleViewException($e, $obLevel);
                }

Hope it is useful to you!

bashy's avatar

May want to add that into "Tips" category rather than bumping a 1 month old thread ;)

MarkNZed's avatar

Hi bashy, I reply on the thread because the page comes up in Google search. Hopefully useful to more people. Regards, Mark.

1 like
gazofnaz's avatar

@anzze Great tip. Here's an example of an error with both methods.

In the first example there is no mention of the actual controller called. In the second there is a clear mention of it.

Without Render. View::make( 'xxx/yyy' )

ErrorException in e0047e27789741709a11bf4e5756331f line 3:
Trying to get property of non-object
1. at HandleExceptions->handleError('8', 'Trying to get property of non-object', '/home/vagrant/pct_l5/storage/framework/views/e0047e27789741709a11bf4e5756331f', '3', array('__path' => '/home/vagrant/pct_l5/storage/framework/views/e0047e27789741709a11bf4e5756331f', '__data' => array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'users' => '31', 'marketingProducts' => '560', 'consultingProducts' => '619', 'ezProducts' => '0', 'currency' => '7', 'statusEnabled' => '1', 'statusDisabled' => '2', 'statusInvited' => '0', 'userList' => object(Collection), 'activityList' => object(Collection), 'currencyList' => array(object(stdClass), object(stdClass), object(stdClass), object(stdClass), object(stdClass), object(stdClass))), 'obLevel' => '1', '__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'users' => '31', 'marketingProducts' => '560', 'consultingProducts' => '619', 'ezProducts' => '0', 'currency' => '7', 'statusEnabled' => '1', 'statusDisabled' => '2', 'statusInvited' => '0', 'userList' => object(Collection), 'activityList' => object(Collection), 'currencyList' => array(object(stdClass), object(stdClass), object(stdClass), object(stdClass), object(stdClass), object(stdClass)))) in e0047e27789741709a11bf4e5756331f line 3
2. at include('/home/vagrant/pct_l5/storage/framework/views/e0047e27789741709a11bf4e5756331f') in PhpEngine.php line 39
3. at PhpEngine->evaluatePath('/home/vagrant/pct_l5/storage/framework/views/e0047e27789741709a11bf4e5756331f', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'users' => '31', 'marketingProducts' => '560', 'consultingProducts' => '619', 'ezProducts' => '0', 'currency' => '7', 'statusEnabled' => '1', 'statusDisabled' => '2', 'statusInvited' => '0', 'userList' => object(Collection), 'activityList' => object(Collection), 'currencyList' => array(object(stdClass), object(stdClass), object(stdClass), object(stdClass), object(stdClass), object(stdClass)))) in CompilerEngine.php line 57
4. at CompilerEngine->get('/home/vagrant/pct_l5/resources/views/login/dashboard.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'users' => '31', 'marketingProducts' => '560', 'consultingProducts' => '619', 'ezProducts' => '0', 'currency' => '7', 'statusEnabled' => '1', 'statusDisabled' => '2', 'statusInvited' => '0', 'userList' => object(Collection), 'activityList' => object(Collection), 'currencyList' => array(object(stdClass), object(stdClass), object(stdClass), object(stdClass), object(stdClass), object(stdClass)))) in View.php line 136
5. at View->getContents() in View.php line 104
6. at View->renderContents() in View.php line 78
7. at View->render() in Response.php line 44
8. at Response->setContent(object(View)) in Response.php line 202
9. at Response->__construct(object(View)) in Router.php line 1198
10. at Router->prepareResponse(object(Request), object(View)) in Router.php line 702
11. at Router->Illuminate\Routing\{closure}(object(Request))
12. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
13. at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
14. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
15. at Pipeline->then(object(Closure)) in Router.php line 703
16. at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 670
17. at Router->dispatchToRoute(object(Request)) in Router.php line 628
18. at Router->dispatch(object(Request)) in Kernel.php line 214
19. at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
20. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
21. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55
22. at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
23. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61
24. at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
25. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
26. at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
27. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40
28. at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
29. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
30. at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
31. at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
32. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
33. at Pipeline->then(object(Closure)) in Kernel.php line 115
34. at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
35. at Kernel->handle(object(Request)) in index.php line 53

With Render. View::make( 'xxx/yyy' )->render()

ErrorException in e0047e27789741709a11bf4e5756331f line 3:
Trying to get property of non-object
in e0047e27789741709a11bf4e5756331f line 3
1. at HandleExceptions->handleError('8', 'Trying to get property of non-object', '/home/vagrant/pct_l5/storage/framework/views/e0047e27789741709a11bf4e5756331f', '3', array('__path' => '/home/vagrant/pct_l5/storage/framework/views/e0047e27789741709a11bf4e5756331f', '__data' => array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'users' => '31', 'marketingProducts' => '560', 'consultingProducts' => '619', 'ezProducts' => '0', 'currency' => '7', 'statusEnabled' => '1', 'statusDisabled' => '2', 'statusInvited' => '0', 'userList' => object(Collection), 'activityList' => object(Collection), 'currencyList' => array(object(stdClass), object(stdClass), object(stdClass), object(stdClass), object(stdClass), object(stdClass))), 'obLevel' => '1', '__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'users' => '31', 'marketingProducts' => '560', 'consultingProducts' => '619', 'ezProducts' => '0', 'currency' => '7', 'statusEnabled' => '1', 'statusDisabled' => '2', 'statusInvited' => '0', 'userList' => object(Collection), 'activityList' => object(Collection), 'currencyList' => array(object(stdClass), object(stdClass), object(stdClass), object(stdClass), object(stdClass), object(stdClass)))) in e0047e27789741709a11bf4e5756331f line 3
2. at include('/home/vagrant/pct_l5/storage/framework/views/e0047e27789741709a11bf4e5756331f') in PhpEngine.php line 39
3. at PhpEngine->evaluatePath('/home/vagrant/pct_l5/storage/framework/views/e0047e27789741709a11bf4e5756331f', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'users' => '31', 'marketingProducts' => '560', 'consultingProducts' => '619', 'ezProducts' => '0', 'currency' => '7', 'statusEnabled' => '1', 'statusDisabled' => '2', 'statusInvited' => '0', 'userList' => object(Collection), 'activityList' => object(Collection), 'currencyList' => array(object(stdClass), object(stdClass), object(stdClass), object(stdClass), object(stdClass), object(stdClass)))) in CompilerEngine.php line 57
4. at CompilerEngine->get('/home/vagrant/pct_l5/resources/views/login/dashboard.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'users' => '31', 'marketingProducts' => '560', 'consultingProducts' => '619', 'ezProducts' => '0', 'currency' => '7', 'statusEnabled' => '1', 'statusDisabled' => '2', 'statusInvited' => '0', 'userList' => object(Collection), 'activityList' => object(Collection), 'currencyList' => array(object(stdClass), object(stdClass), object(stdClass), object(stdClass), object(stdClass), object(stdClass)))) in View.php line 136
5. at View->getContents() in View.php line 104
6. at View->renderContents() in View.php line 78
7. at View->render() in HomeController.php line 100
8. at HomeController->getDashboard()
9. at call_user_func_array(array(object(HomeController), 'getDashboard'), array()) in Controller.php line 246
10. at Controller->callAction('getDashboard', array()) in ControllerDispatcher.php line 162
11. at ControllerDispatcher->call(object(HomeController), object(Route), 'getDashboard') in ControllerDispatcher.php line 107
12. at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
13. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
14. at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
15. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
16. at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 108
17. at ControllerDispatcher->callWithinStack(object(HomeController), object(Route), object(Request), 'getDashboard') in ControllerDispatcher.php line 67
18. at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\HomeController', 'getDashboard') in Route.php line 204
19. at Route->runWithCustomDispatcher(object(Request)) in Route.php line 134
20. at Route->run(object(Request)) in Router.php line 701
21. at Router->Illuminate\Routing\{closure}(object(Request))
22. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
23. at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
24. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
25. at Pipeline->then(object(Closure)) in Router.php line 703
26. at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 670
27. at Router->dispatchToRoute(object(Request)) in Router.php line 628
28. at Router->dispatch(object(Request)) in Kernel.php line 214
29. at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
30. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
31. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55
32. at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
33. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61
34. at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
35. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
36. at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
37. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40
38. at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
39. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
40. at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
41. at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
42. at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
43. at Pipeline->then(object(Closure)) in Kernel.php line 115
44. at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
45. at Kernel->handle(object(Request)) in index.php line 53
2 likes

Please or to participate in this conversation.