-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OpenApi] Specific service to convert error to http status code
- Loading branch information
Showing
9 changed files
with
257 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...open-api/HttpFoundation/ErrorToHttpCodeConverter/ConfigurableErrorToHttpCodeConverter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Draw\Component\OpenApi\HttpFoundation\ErrorToHttpCodeConverter; | ||
|
||
class ConfigurableErrorToHttpCodeConverter implements ErrorToHttpCodeConverterInterface | ||
{ | ||
private const DEFAULT_STATUS_CODE = 500; | ||
|
||
/** | ||
* @var array<string,int> | ||
*/ | ||
private array $errorCodes; | ||
|
||
public static function getDefaultPriority(): int | ||
{ | ||
return -1000; | ||
} | ||
|
||
public function __construct(array $errorCodes = []) | ||
{ | ||
$this->errorCodes = array_filter($errorCodes); | ||
} | ||
|
||
public function convertToHttpCode(\Throwable $error): int | ||
{ | ||
$exceptionClass = $error::class; | ||
|
||
foreach ($this->errorCodes as $exceptionMapClass => $value) { | ||
switch (true) { | ||
case $exceptionClass === $exceptionMapClass: | ||
case is_a($error, $exceptionMapClass, true): | ||
return $value; | ||
} | ||
} | ||
|
||
return self::DEFAULT_STATUS_CODE; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...es/open-api/HttpFoundation/ErrorToHttpCodeConverter/ErrorToHttpCodeConverterInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Draw\Component\OpenApi\HttpFoundation\ErrorToHttpCodeConverter; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag; | ||
|
||
#[AutoconfigureTag(ErrorToHttpCodeConverterInterface::class)] | ||
interface ErrorToHttpCodeConverterInterface | ||
{ | ||
public static function getDefaultPriority(): int; | ||
|
||
public function convertToHttpCode(\Throwable $error): ?int; | ||
} |
22 changes: 22 additions & 0 deletions
22
...ges/open-api/HttpFoundation/ErrorToHttpCodeConverter/HttpExceptionToHttpCodeConverter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Draw\Component\OpenApi\HttpFoundation\ErrorToHttpCodeConverter; | ||
|
||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
|
||
class HttpExceptionToHttpCodeConverter implements ErrorToHttpCodeConverterInterface | ||
{ | ||
public static function getDefaultPriority(): int | ||
{ | ||
return 100; | ||
} | ||
|
||
public function convertToHttpCode(\Throwable $error): ?int | ||
{ | ||
if ($error instanceof HttpException) { | ||
return $error->getStatusCode(); | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.