From 4887f0f9b5b9dc58e9b8af0a871a81ee85974694 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Mon, 4 Apr 2016 10:55:46 +0200 Subject: [PATCH] Add better messages for sometimes obscure exceptions * add better messages for sometimes obscure exceptions * fix formatting --- lib/public/appframework/db/mapper.php | 29 +++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/public/appframework/db/mapper.php b/lib/public/appframework/db/mapper.php index 3bc9fbcefc..2e97b06802 100644 --- a/lib/public/appframework/db/mapper.php +++ b/lib/public/appframework/db/mapper.php @@ -282,18 +282,43 @@ abstract class Mapper { if($row === false || $row === null){ $stmt->closeCursor(); - throw new DoesNotExistException('No matching entry found'); + $msg = $this->buildDebugMessage( + 'Did expect one result but found none when executing', $sql, $params, $limit, $offset + ); + throw new DoesNotExistException($msg); } $row2 = $stmt->fetch(); $stmt->closeCursor(); //MDB2 returns null, PDO and doctrine false when no row is available if( ! ($row2 === false || $row2 === null )) { - throw new MultipleObjectsReturnedException('More than one result'); + $msg = $this->buildDebugMessage( + 'Did not expect more than one result when executing', $sql, $params, $limit, $offset + ); + throw new MultipleObjectsReturnedException($msg); } else { return $row; } } + /** + * Builds an error message by prepending the $msg to an error message which + * has the parameters + * @see findEntity + * @param string $sql the sql query + * @param array $params the parameters of the sql query + * @param int $limit the maximum number of rows + * @param int $offset from which row we want to start + * @return string formatted error message string + * @since 9.1.0 + */ + private function buildDebugMessage($msg, $sql, array $params=[], $limit=null, $offset=null) { + return $msg . + ': query "' . $sql . '"; ' . + 'parameters ' . print_r($params, true) . '; ' . + 'limit "' . $limit . '"; '. + 'offset "' . $offset . '"'; + } + /** * Creates an entity from a row. Automatically determines the entity class