-
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.
Merge pull request #837 from PennyDreadfulMTG/validation
Validation comes to Gatherling, a little
- Loading branch information
Showing
30 changed files
with
660 additions
and
94 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gatherling\Exceptions; | ||
|
||
use Throwable; | ||
|
||
abstract class BadRequestException extends GatherlingException | ||
{ | ||
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null, int $httpStatusCode = 400) | ||
{ | ||
parent::__construct($message, $code, $previous, $httpStatusCode); | ||
} | ||
|
||
abstract public function getUserMessage(): string; | ||
} |
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
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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gatherling\Exceptions; | ||
|
||
use Gatherling\Helpers\Types\Type; | ||
use Throwable; | ||
|
||
class RequestException extends BadRequestException | ||
{ | ||
public function __construct( | ||
private string $key, | ||
private Type $expectedType, | ||
private mixed $value, | ||
Throwable $previous | ||
) { | ||
$s = "Error processing request parameter '{$key}', expecting {$expectedType->getTypeName()} but got " . var_export($value, true); | ||
parent::__construct($s, 0, $previous); | ||
} | ||
|
||
public function getUserMessage(): string | ||
{ | ||
$s = "The {$this->key} field requires {$this->expectedType->getDisplayName($this->value)} but it was "; | ||
if ($this->value === null) { | ||
$s .= "missing"; | ||
} elseif ($this->value === '') { | ||
$s .= "blank"; | ||
} elseif (is_scalar($this->value)) { | ||
$s .= "'{$this->value}'"; | ||
} else { | ||
$s .= "invalid"; | ||
} | ||
return $s; | ||
} | ||
} |
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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gatherling\Exceptions; | ||
|
||
class ValidationException extends BadRequestException | ||
{ | ||
public function getUserMessage(): string | ||
{ | ||
return $this->getMessage(); | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gatherling\Helpers; | ||
|
||
use Gatherling\Exceptions\BadRequestException; | ||
use Gatherling\Exceptions\GatherlingException; | ||
use Gatherling\Views\Pages\Error; | ||
use Throwable; | ||
|
||
class ErrorHandler | ||
{ | ||
public function handle(Throwable $e): never | ||
{ | ||
$requestId = Request::getRequestId(); | ||
|
||
$message = (string)$e; | ||
|
||
if ($e instanceof BadRequestException) { | ||
logger()->warning($message); | ||
$userMessage = $e->getUserMessage(); | ||
} else { | ||
logger()->error($message); | ||
$userMessage = "Oops! Something went wrong."; | ||
} | ||
|
||
if ($e instanceof GatherlingException) { | ||
$httpStatusCode = $e->httpStatusCode; | ||
} else { | ||
$httpStatusCode = 500; | ||
} | ||
$page = new Error($requestId, $userMessage, $httpStatusCode); | ||
$page->send(); | ||
} | ||
} |
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.