Changing the default PDO error mode

  109015
March 14, 2020 15:40 php.lists@allenjb.me.uk (AllenJB)
Hi all,

A regular problem that new users run into is error handling with PDO. 
The current default error mode for PDO is "silent". This leads to the 
situation where code doesn't work and returns no obvious errors unless 
PDO specific error handling code is explicitly added.

I would like to propose the default error mode be changed from PHP 8 as 
I believe this would make PDO error handling much more intuitive for new 
developers, preventing the common "my code isn't working and I don't 
know why" scenario that occurs.

While a BC break, I believe it's the type of change developers expect in 
a major version and I don't believe that explicitly setting the error 
mode for code bases that rely on the silent error mode is onerous - many 
code bases are already explicitly setting the error mode already.

Given the way error handling has been / is heading in PHP, I would 
suggest the default error mode should be exceptions, but I can forsee 
arguments for using warnings instead ("less of a BC break" since code 
would still continue unless a custom error handler causes an abort, so 
closer to the behavior of "silent").

What are peoples views on making this change?

What do you think the new default be (and why)?

(I am aware this change will likely require an RFC)

AllenJB
  109021
March 14, 2020 21:10 larry@garfieldtech.com ("Larry Garfield")
On Sat, Mar 14, 2020, at 10:40 AM, AllenJB wrote:
> Hi all, > > A regular problem that new users run into is error handling with PDO. > The current default error mode for PDO is "silent". This leads to the > situation where code doesn't work and returns no obvious errors unless > PDO specific error handling code is explicitly added. > > I would like to propose the default error mode be changed from PHP 8 as > I believe this would make PDO error handling much more intuitive for new > developers, preventing the common "my code isn't working and I don't > know why" scenario that occurs. > > While a BC break, I believe it's the type of change developers expect in > a major version and I don't believe that explicitly setting the error > mode for code bases that rely on the silent error mode is onerous - many > code bases are already explicitly setting the error mode already. > > Given the way error handling has been / is heading in PHP, I would > suggest the default error mode should be exceptions, but I can forsee > arguments for using warnings instead ("less of a BC break" since code > would still continue unless a custom error handler causes an abort, so > closer to the behavior of "silent"). > > What are peoples views on making this change? > > What do you think the new default be (and why)? > > (I am aware this change will likely require an RFC) > > AllenJB
I see only two options: 1) Do nothing. 2) Move to Exceptions. I've never used PDO and not set it to Exceptions, honestly. Adding more global error conditions to code is a terrible idea, so let's not do warnings. If your query breaks, you really shouldn't continue because your data is busted so your following code will fail ungracefully at best, or corrupt data at worst. Changing a default like this seems a reasonably safe thing to do in a major, as long as it's still just as easy to switch back to silent mode for those who prefer to be explicit. --Larry Garfield
  109022
March 14, 2020 21:20 george.banyard@gmail.com ("G. P. B.")
On Sat, 14 Mar 2020 at 22:10, Larry Garfield <larry@garfieldtech.com> wrote:

> On Sat, Mar 14, 2020, at 10:40 AM, AllenJB wrote: > > Hi all, > > > > A regular problem that new users run into is error handling with PDO. > > The current default error mode for PDO is "silent". This leads to the > > situation where code doesn't work and returns no obvious errors unless > > PDO specific error handling code is explicitly added. > > > > I would like to propose the default error mode be changed from PHP 8 as > > I believe this would make PDO error handling much more intuitive for new > > developers, preventing the common "my code isn't working and I don't > > know why" scenario that occurs. > > > > While a BC break, I believe it's the type of change developers expect in > > a major version and I don't believe that explicitly setting the error > > mode for code bases that rely on the silent error mode is onerous - many > > code bases are already explicitly setting the error mode already. > > > > Given the way error handling has been / is heading in PHP, I would > > suggest the default error mode should be exceptions, but I can forsee > > arguments for using warnings instead ("less of a BC break" since code > > would still continue unless a custom error handler causes an abort, so > > closer to the behavior of "silent"). > > > > What are peoples views on making this change? > > > > What do you think the new default be (and why)? > > > > (I am aware this change will likely require an RFC) > > > > AllenJB > > I see only two options: > > 1) Do nothing. > > 2) Move to Exceptions. > > I've never used PDO and not set it to Exceptions, honestly. Adding more > global error conditions to code is a terrible idea, so let's not do > warnings. If your query breaks, you really shouldn't continue because your > data is busted so your following code will fail ungracefully at best, or > corrupt data at worst. > > Changing a default like this seems a reasonably safe thing to do in a > major, as long as it's still just as easy to switch back to silent mode for > those who prefer to be explicit. > > --Larry Garfield > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php
Agreed this should be an all or nothing change. Best regards George P. Banyard
  109139
March 19, 2020 09:31 php@beccati.com (Matteo Beccati)
Hi,

On 14/03/2020 22:10, Larry Garfield wrote:
>> What do you think the new default be (and why)? >> >> (I am aware this change will likely require an RFC) >> >> AllenJB > > I see only two options: > > 1) Do nothing. > > 2) Move to Exceptions. > > I've never used PDO and not set it to Exceptions, honestly. Adding more global error conditions to code is a terrible idea, so let's not do warnings. If your query breaks, you really shouldn't continue because your data is busted so your following code will fail ungracefully at best, or corrupt data at worst. > > Changing a default like this seems a reasonably safe thing to do in a major, as long as it's still just as easy to switch back to silent mode for those who prefer to be explicit.
I agree defaulting to PDO::ERRMODE_EXCEPTION is a sensible change for PHP8. Cheers -- Matteo Beccati Development & Consulting - http://www.beccati.com/
  109144
March 19, 2020 12:25 cschneid@cschneid.com (Christian Schneider)
Am 19.03.2020 um 10:31 schrieb Matteo Beccati <php@beccati.com>:
> On 14/03/2020 22:10, Larry Garfield wrote: >>> What do you think the new default be (and why)? >>> >>> (I am aware this change will likely require an RFC) >>> >>> AllenJB >> >> I see only two options: >> >> 1) Do nothing. >> >> 2) Move to Exceptions. >> >> I've never used PDO and not set it to Exceptions, honestly. Adding more global error conditions to code is a terrible idea, so let's not do warnings. If your query breaks, you really shouldn't continue because your data is busted so your following code will fail ungracefully at best, or corrupt data at worst. >> >> Changing a default like this seems a reasonably safe thing to do in a major, as long as it's still just as easy to switch back to silent mode for those who prefer to be explicit. > > I agree defaulting to PDO::ERRMODE_EXCEPTION is a sensible change for PHP8.
I think this should go through a deprecation phase first as it is a BC break, no matter how easy to fix you think it is. - Chris
  109145
March 19, 2020 12:52 cmbecker69@gmx.de ("Christoph M. Becker")
On 19.03.2020 at 13:25, Christian Schneider wrote:

> Am 19.03.2020 um 10:31 schrieb Matteo Beccati <php@beccati.com>: > >> On 14/03/2020 22:10, Larry Garfield wrote: >> >>>> What do you think the new default be (and why)? >>>> >>>> (I am aware this change will likely require an RFC) >>>> >>>> AllenJB >>> >>> I see only two options: >>> >>> 1) Do nothing. >>> >>> 2) Move to Exceptions. >>> >>> I've never used PDO and not set it to Exceptions, honestly. Adding more global error conditions to code is a terrible idea, so let's not do warnings. If your query breaks, you really shouldn't continue because your data is busted so your following code will fail ungracefully at best, or corrupt data at worst. >>> >>> Changing a default like this seems a reasonably safe thing to do in a major, as long as it's still just as easy to switch back to silent mode for those who prefer to be explicit. >> >> I agree defaulting to PDO::ERRMODE_EXCEPTION is a sensible change for PHP8. > > I think this should go through a deprecation phase first as it is a BC break, no matter how easy to fix you think it is.
As I understand it, the proposal is to *change* the default setting, not to (eventually) remove any of the currently available modes. Are you suggesting to deprecate using the default; in other words, require users to explicitly set PDO::ATTR_ERRMODE? -- Christoph M. Becker
  109178
March 21, 2020 14:32 php.lists@allenjb.me.uk (AllenJB)
On 19/03/2020 12:25, Christian Schneider wrote:
> Am 19.03.2020 um 10:31 schrieb Matteo Beccati <php@beccati.com>: >> I agree defaulting to PDO::ERRMODE_EXCEPTION is a sensible change for PHP8. > I think this should go through a deprecation phase first as it is a BC break, no matter how easy to fix you think it is. > > - Chris
To clarify: I'm not proposing the silent error mode be removed - it would still exist and developers would be able to switch (back) to it simply by explicitly setting the error mode when creating a PDO object. How do you see that a deprecation would be implemented for this change? A deprecation notice each time a PDO object is created without explicitly setting the error mode? Is this not just as disruptive to existing code as changing the default error handling to warnings? For new users I can see it being more so: Every time a new developer follows an older example they're going to be asking about this deprecation message (or worse, changing error_reporting or using @ to silence it!). Without a deprecation phase, existing examples will continue to work (errors will just be reported differently, but no in an unintuitive way in my opinion) AllenJB