2

I'd like my Laravel app to output assert errors in the browser during development. Although I have put some asserts in my Laravel code that should definitely fail, I never see any errors outputted in the browser?

Is there a assert reporting configuration in Laravel that can force these errors to display?

In production, I would like the assert failures to call a callback method and email me the error.

StackOverflowNewbie
  • 39,403
  • 111
  • 277
  • 441
  • 1
    this [`assert()`](http://php.net/manual/en/function.assert.php)? are you sure that `php.ini` already configured? – Bagus Tesa Oct 03 '18 at 01:37
  • are you using asserts in testing env? – Fatemeh Majd Oct 03 '18 at 08:14
  • @BagusTesa - yes, assert() – StackOverflowNewbie Oct 03 '18 at 09:30
  • @FatemehMajd - not in a testing environment. – StackOverflowNewbie Oct 03 '18 at 09:31
  • why would you use assert in a non-testing environment? asserts, of course, do not work in non-testing env. there are surely other ways to do assert something without actually using an assert function. what do you want to accomplish? – Fatemeh Majd Oct 03 '18 at 12:25
  • asserts work fine in a non-testing environment. For example, I can get it to display assert errors on my localhost when doing plain PHP. I tend to assert a lot of my assumptions while developing code -- and when one of my assumptions is incorrect, I want to be warned. – StackOverflowNewbie Oct 04 '18 at 00:19
  • 3
    I wrote up an answer but then came across [what is probably a duplicate](https://stackoverflow.com/questions/40129963/assert-not-working-in-php-so-simple-what-am-i-doing-wrong)? In short: check [`zend.assertions`](https://www.php.net/manual/en/ini.core.php#ini.zend.assertions). – Don't Panic Jan 05 '20 at 09:39
  • Debugging output sometimes doesn't make it to the browser, particularly in things like Ajax calls. What I generally like to do is append debugging output to a log file which I can inspect at some time after a run. It also has the advantage that you can leave the assertions in the Production code, as it won't interrupt the end-user, but the debugging log file remains available to maintenance developers. – UncaAlby Jan 10 '20 at 23:08

1 Answers1

5

zend.assertions are likely in production mode. In other words, assertions are disabled and ignored.

When set to -1, assertion code will not be generated, making the assertions zero-cost. 1

You can view this configuration setting from the command line:

$ php -i | grep 'zend.assertions' # zend.assertions => -1 => -1

If you are using homestead, see this post to change the configuration.

Marc Barbeau
  • 826
  • 10
  • 21