2

I have a LDAP server request in my code. I open an connection with ldap_connect and check that returned descriptor is valid, but then I make a search request with ldap_search using the open descriptor. But sometimes due to the network failure or something else, ldap_search fails with warning Search: Can't contact LDAP server.

On my production server I treat all warnings as fatal errors, so the whole page render fails.

Should I just prepend ldap_search with @ and just check the value returned (I do check it now as well) or there's a better way to handle this?

dmzkrsk
  • 2,011
  • 2
  • 20
  • 30

2 Answers2

1

Personally I'd agree with you, just shove a @ there and manually check for error conditions - I do this for mysql_connect and Memcache->connect, and any fsockopen calls I make, as this can also allow for things like "try again a few times before dying".

However, I'm sure most nuts would tell you you should never use @ and you should use custom error handlers instead. So... up to you!

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1

You can turn warnings into Exceptions by setting your own error handler. Just catch all warnings and throw an Exception inside the callback. That way you won't have to deal with the unwanted output, but have a chance to recover (direct the user to a temporary-error-page), which you can't do with fatal errors.

Sometimes an external resource throws a warning, and also lets you check if an error occurred (mysqli for example.) In this case you can ignore the warning with @, but make sure you check if an error occurred.

I would not just suppress a warning, without making sure that it is dealt with in some other way. Users want to know why something went wrong, especially if the situation is only temporary.

As a side note: I think warnings in php are a rediculous concept. Code either fails or it doesn't. If it fails, I want to be able to deal with it, without jumping through hoops. Just throw me an exception, please!

DudeOnRock
  • 3,685
  • 3
  • 27
  • 58