Skip to content

Commit 621bde4

Browse files
committed
Tested new feature, use Exception class name if no error message has been provided
1 parent 4488828 commit 621bde4

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/ApiProblem/ApiProblem.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ public static function fromException(Exception $exception, $title = '', $type =
135135
$type = self::RFC2616;
136136
}
137137

138-
$detail = empty($exception->getMessage()) ? get_class($exception) : $exception->getMessage();
138+
$detail = $exception->getMessage();
139+
if (empty($detail)) {
140+
$className = explode('\\', get_class($exception));
141+
$detail = array_pop($className);
142+
}
139143

140144
return new self($code, $detail, $title, $type, $additionalDetails);
141145
}

tests/ApiProblem/ApiProblemTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,15 @@ public function testItCanConstructFromException()
2727
$this->assertEquals('Not Found', $problem->title());
2828
$this->assertEquals(ApiProblem::RFC2616, $problem->type());
2929
}
30+
31+
public function testItCanUseExceptionName()
32+
{
33+
$exception = new \PDOException();
34+
$problem = ApiProblem::fromException($exception);
35+
36+
$this->assertEquals(500, $problem->status());
37+
$this->assertEquals('PDOException', $problem->detail());
38+
$this->assertEquals('Internal Server Error', $problem->title());
39+
$this->assertEquals('http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html', $problem->type());
40+
}
3041
}

0 commit comments

Comments
 (0)