Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trigger_error vs. throwing exceptions

A similar question was asked here, but as the answers didn't answer my question, I'm asking:

I've almost never used trigger_error, always thrown exceptions instead, since in my mind errors are legacy. But I've changed my mind, I think they can co-exist. There are cases when triggering errors make more sense.

I'm updating this library, this question concerns the send method, but is general enough. This is my reasoning:

  • If an API key constant is not set, that is not a catchable error. That is a programming error, and should be treated as such.

  • If an email address is invalid, that should be catchable. This is most likely a user error.

Am I loco? Is this unnecessary and annoying, or does it make sense?

like image 724
Znarkus Avatar asked Oct 21 '10 21:10

Znarkus


People also ask

What is the use of Trigger_error ()?

The trigger_error() function creates a user-level error message. The trigger_error() function can be used with the built-in error handler, or with a user-defined function set by the set_error_handler() function.

What is trigger error?

Used to trigger a user error condition, it can be used in conjunction with the built-in error handler, or with a user defined function that has been set as the new error handler (set_error_handler()). This function is useful when you need to generate a particular response to an exception at runtime.

Is throwing an exception bad?

The throws declaration is part of the method contract. You should always be as precise as possible when defining contracts. Saying throws Exception is therefore a bad idea. It's bad for the same reason it is bad practice to say a method returns an Object when it is guaranteed to return a String .


1 Answers

I agree with your distinction, as to when to throw and when to trigger. For me, trigger_error is also something you want to make a note off, but it's not important to the current request. E.g. for debugging purposes.

Since all my PHP errors (note: not exceptions, but warnings, notices, fatals, etc.) are logged in production, I think trigger_error is a convenient way to get stuff into said log.

Here is an example:

I'm using a HTTP client to access an API we integrate. Of course the library I use is object-oriented PHP and therefor makes heavy use of exceptions. I'm doing various things here and I hope this example makes sense:

  1. The HTTP client library throws an exception when the actual request failed -- e.g. due to a connection issue, such as a timeout, etc.. Of course I catch this error, but I don't elevate it to the user. My wrapper returns false and this equals to, "Temporary issue." in the frontend.
  2. In my catch() block I use trigger_error() to log debug information about the actual connection error. Since I got error_log = syslog in my php.ini all this information is send to syslog and eventually to my log master.
like image 138
Till Avatar answered Sep 25 '22 00:09

Till