Skip to content

Commit

Permalink
Tested new feature, use Exception class name if no error message has …
Browse files Browse the repository at this point in the history
…been provided
  • Loading branch information
nilportugues committed Jun 21, 2016
1 parent 4488828 commit 621bde4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/ApiProblem/ApiProblem.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ public static function fromException(Exception $exception, $title = '', $type =
$type = self::RFC2616;
}

$detail = empty($exception->getMessage()) ? get_class($exception) : $exception->getMessage();
$detail = $exception->getMessage();
if (empty($detail)) {
$className = explode('\\', get_class($exception));
$detail = array_pop($className);
}

return new self($code, $detail, $title, $type, $additionalDetails);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/ApiProblem/ApiProblemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@ public function testItCanConstructFromException()
$this->assertEquals('Not Found', $problem->title());
$this->assertEquals(ApiProblem::RFC2616, $problem->type());
}

public function testItCanUseExceptionName()
{
$exception = new \PDOException();
$problem = ApiProblem::fromException($exception);

$this->assertEquals(500, $problem->status());
$this->assertEquals('PDOException', $problem->detail());
$this->assertEquals('Internal Server Error', $problem->title());
$this->assertEquals('http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html', $problem->type());
}
}

0 comments on commit 621bde4

Please sign in to comment.