Skip to content

Commit 23fe3c9

Browse files
committed
improve errors
1 parent 841474b commit 23fe3c9

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

api.include.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4546,7 +4546,7 @@ public function success($result): ResponseInterface
45464546

45474547
public function exception($exception): ResponseInterface
45484548
{
4549-
$document = ErrorDocument::fromException($exception);
4549+
$document = ErrorDocument::fromException($exception, $this->debug);
45504550
$response = ResponseFactory::fromObject($document->getStatus(), $document);
45514551
if ($this->debug) {
45524552
$response = ResponseUtils::addExceptionHeaders($response, $exception);
@@ -4562,7 +4562,7 @@ public function multi($results): ResponseInterface
45624562
foreach ($results as $i => $result) {
45634563
if ($result instanceof \Throwable) {
45644564
$documents[$i] = null;
4565-
$errors[$i] = ErrorDocument::fromException($result);
4565+
$errors[$i] = ErrorDocument::fromException($result, $this->debug);
45664566
$success = false;
45674567
} else {
45684568
$documents[$i] = $result;
@@ -9894,7 +9894,7 @@ public function jsonSerialize()
98949894
return array_filter($this->serialize(), function($v) {return $v!==null;});
98959895
}
98969896

9897-
public static function fromException(\Throwable $exception)
9897+
public static function fromException(\Throwable $exception, bool $debug)
98989898
{
98999899
$document = new ErrorDocument(new ErrorCode(ErrorCode::ERROR_NOT_FOUND), $exception->getMessage(), null);
99009900
if ($exception instanceof \PDOException) {
@@ -9909,7 +9909,8 @@ public static function fromException(\Throwable $exception)
99099909
} elseif (strpos(strtolower($exception->getMessage()), 'constraint') !== false) {
99109910
$document = new ErrorDocument(new ErrorCode(ErrorCode::DATA_INTEGRITY_VIOLATION), '', null);
99119911
} else {
9912-
$document = new ErrorDocument(new ErrorCode(ErrorCode::ERROR_NOT_FOUND), '', null);
9912+
$message = $debug?$exception->getMessage():'PDOException occurred (enable debug mode)';
9913+
$document = new ErrorDocument(new ErrorCode(ErrorCode::ERROR_NOT_FOUND), $message, null);
99139914
}
99149915
}
99159916
return $document;

api.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4546,7 +4546,7 @@ public function success($result): ResponseInterface
45464546

45474547
public function exception($exception): ResponseInterface
45484548
{
4549-
$document = ErrorDocument::fromException($exception);
4549+
$document = ErrorDocument::fromException($exception, $this->debug);
45504550
$response = ResponseFactory::fromObject($document->getStatus(), $document);
45514551
if ($this->debug) {
45524552
$response = ResponseUtils::addExceptionHeaders($response, $exception);
@@ -4562,7 +4562,7 @@ public function multi($results): ResponseInterface
45624562
foreach ($results as $i => $result) {
45634563
if ($result instanceof \Throwable) {
45644564
$documents[$i] = null;
4565-
$errors[$i] = ErrorDocument::fromException($result);
4565+
$errors[$i] = ErrorDocument::fromException($result, $this->debug);
45664566
$success = false;
45674567
} else {
45684568
$documents[$i] = $result;
@@ -9894,7 +9894,7 @@ public function jsonSerialize()
98949894
return array_filter($this->serialize(), function($v) {return $v!==null;});
98959895
}
98969896

9897-
public static function fromException(\Throwable $exception)
9897+
public static function fromException(\Throwable $exception, bool $debug)
98989898
{
98999899
$document = new ErrorDocument(new ErrorCode(ErrorCode::ERROR_NOT_FOUND), $exception->getMessage(), null);
99009900
if ($exception instanceof \PDOException) {
@@ -9909,7 +9909,8 @@ public static function fromException(\Throwable $exception)
99099909
} elseif (strpos(strtolower($exception->getMessage()), 'constraint') !== false) {
99109910
$document = new ErrorDocument(new ErrorCode(ErrorCode::DATA_INTEGRITY_VIOLATION), '', null);
99119911
} else {
9912-
$document = new ErrorDocument(new ErrorCode(ErrorCode::ERROR_NOT_FOUND), '', null);
9912+
$message = $debug?$exception->getMessage():'PDOException occurred (enable debug mode)';
9913+
$document = new ErrorDocument(new ErrorCode(ErrorCode::ERROR_NOT_FOUND), $message, null);
99139914
}
99149915
}
99159916
return $document;

src/Tqdev/PhpCrudApi/Controller/JsonResponder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function success($result): ResponseInterface
3030

3131
public function exception($exception): ResponseInterface
3232
{
33-
$document = ErrorDocument::fromException($exception);
33+
$document = ErrorDocument::fromException($exception, $this->debug);
3434
$response = ResponseFactory::fromObject($document->getStatus(), $document);
3535
if ($this->debug) {
3636
$response = ResponseUtils::addExceptionHeaders($response, $exception);
@@ -46,7 +46,7 @@ public function multi($results): ResponseInterface
4646
foreach ($results as $i => $result) {
4747
if ($result instanceof \Throwable) {
4848
$documents[$i] = null;
49-
$errors[$i] = ErrorDocument::fromException($result);
49+
$errors[$i] = ErrorDocument::fromException($result, $this->debug);
5050
$success = false;
5151
} else {
5252
$documents[$i] = $result;

src/Tqdev/PhpCrudApi/Record/Document/ErrorDocument.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function jsonSerialize()
4646
return array_filter($this->serialize(), function($v) {return $v!==null;});
4747
}
4848

49-
public static function fromException(\Throwable $exception)
49+
public static function fromException(\Throwable $exception, bool $debug)
5050
{
5151
$document = new ErrorDocument(new ErrorCode(ErrorCode::ERROR_NOT_FOUND), $exception->getMessage(), null);
5252
if ($exception instanceof \PDOException) {
@@ -61,7 +61,8 @@ public static function fromException(\Throwable $exception)
6161
} elseif (strpos(strtolower($exception->getMessage()), 'constraint') !== false) {
6262
$document = new ErrorDocument(new ErrorCode(ErrorCode::DATA_INTEGRITY_VIOLATION), '', null);
6363
} else {
64-
$document = new ErrorDocument(new ErrorCode(ErrorCode::ERROR_NOT_FOUND), '', null);
64+
$message = $debug?$exception->getMessage():'PDOException occurred (enable debug mode)';
65+
$document = new ErrorDocument(new ErrorCode(ErrorCode::ERROR_NOT_FOUND), $message, null);
6566
}
6667
}
6768
return $document;

0 commit comments

Comments
 (0)