Skip to content

Allow throwables to be used #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/ApiProblem/ApiProblem.php
Original file line number Diff line number Diff line change
@@ -127,6 +127,18 @@ public function __construct($status, $detail, $title = '', $type = '', array $ad
*/
public static function fromException(Exception $exception, $title = '', $type = '', array $additionalDetails = [])
{
return self::fromThrowable($exception, $title, $type, $additionalDetails);
}

/**
* @param Throwable $throwable
* @param string $title
* @param string $type
* @param array $additionalDetails
*
* @return ApiProblem
*/
public static function fromThrowable(Throwable $throwable, $title = '', $type = '', array $additionalDetails = []) {
$eCode = $exception->getCode();
$code = (!empty($eCode) && is_int($eCode)) ? $eCode : 500;

34 changes: 34 additions & 0 deletions src/ApiProblem/ApiProblemResponse.php
Original file line number Diff line number Diff line change
@@ -72,6 +72,23 @@ public static function fromExceptionToJson(
) {
return new self(new JsonPresenter(ApiProblem::fromException($exception, $title, $type, $additionalDetails)));
}

/**
* @param Throwable $throwable
* @param string $title
* @param string $type
* @param array $additionalDetails
*
* @return ApiProblemResponse
*/
public static function froThrowableToJson(
Throwable $throwable,
$title = '',
$type = '',
array $additionalDetails = []
) {
return new self(new JsonPresenter(ApiProblem::fromThrowable($throwable, $title, $type, $additionalDetails)));
}

/**
* @param $status
@@ -103,4 +120,21 @@ public static function fromExceptionToXml(
) {
return new self(new XmlPresenter(ApiProblem::fromException($exception, $title, $type, $additionalDetails)));
}

/**
* @param Throwable $throwable
* @param string $title
* @param string $type
* @param array $additionalDetails
*
* @return ApiProblemResponse
*/
public static function fromThroableToXml(
Throwable $throwable,
$title = '',
$type = '',
array $additionalDetails = []
) {
return new self(new XmlPresenter(ApiProblem::fromThrowable($throwable, $title, $type, $additionalDetails)));
}
}