0

I noticed that PDO displays warnings to the end client. For example, if an invalid enum value is being set, it shows a warning:

SQLSTATE[01000]: Warning: 1265 Data truncated for column 'type' at row 1

There is no reason for end user to see this. I'd much rather just display 500 status code, although it is users fault so 400 would be more appropriate.

These are my current pdo options:

public static array $options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];

and i am using

error_reporting(E_ALL);
ini_set('display_errors', 0);

at the top of the script.

Any idea why this is happening?

Minimal code with intentional incorrect values when display_errors is set to 1:

$stmt = $this->pdo->prepare("INSERT INTO MYTABLE (`type`) VALUES (?)");
$stmt->execute([$json->type]);

Full message without stack trace

SQLSTATE[01000]: Warning: 1265 Data truncated for column 'type' at row 1
Fatal error: Uncaught PDOException: SQLSTATE[01000]: Warning: 1265 Data truncated for column 'type' at row 1 in path/api/v1/index.php:35

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
sanjihan
  • 5,592
  • 11
  • 54
  • 119
  • 1
    Does this answer your question? [How to prevent showing PDO error?](https://stackoverflow.com/questions/5817705/how-to-prevent-showing-pdo-error) – Mark Feb 11 '20 at 09:59
  • Are you sure that "ini_set('display_errors', 0);" actually affects the configuration? On some servers, it would be ignored. – Ofir Baruch Feb 11 '20 at 09:59
  • @MarkOverton I have already set `PDO::ATTR_ERRMODE` to `PDO::ERRMODE_EXCEPTION`. Warning is still displayed. – sanjihan Feb 11 '20 at 10:01
  • @OfirBaruch Yes, if I set it to 1, than the message also contains the fatal error. – sanjihan Feb 11 '20 at 10:02
  • Can you provide the **full** error message? As well as a code fragment that produces this error – Your Common Sense Feb 11 '20 at 10:02
  • As the message error says, you need to Increase the length of your column to fit the length of the data you are trying to insert – ANIK ISLAM SHOJIB Feb 11 '20 at 10:07
  • It doesn't seem to me similar to this [bug](https://stackoverflow.com/questions/48682621/pdoerrmode-exception-doesnt-suppress-warning). More like some handler is catching the error, echoing the message and re-throwing the exception. Can you reproduce it in a standalone script? – Your Common Sense Feb 11 '20 at 10:38
  • @YourCommonSense aah you are right indeed. Somewhere in the middleware stack I indeed echo the error and rethrow it. – sanjihan Feb 11 '20 at 11:10

0 Answers0