Framework-agnostic REST controller toolkit in vanilla PHP. - Router with path parameters, 404 and 405 with an Allow header. - Immutable Request, JsonRequest and Response; nothing is printed. - CrudController over a ResourceGateway port the application implements. - Composable Middleware/Pipeline chain. - Security middlewares: BearerAuth, RequireRole, RateLimit, Cors. - Transport middlewares: Logging, ContentNegotiation. - Nine typed HTTP exceptions with snake_case JSON bodies. - Declares PHP >=8.3 and no dependency on an ORM: ports are local. - MIT license.
31 lines
737 B
PHP
31 lines
737 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Camelia\Controllers\Exception;
|
|
|
|
/**
|
|
* A failure with an HTTP status and a stable machine-readable code.
|
|
*
|
|
* The status and code are public contract: clients branch on them.
|
|
*/
|
|
abstract class HttpError extends ControllerException
|
|
{
|
|
abstract public function status(): int;
|
|
|
|
/** Stable snake_case identifier sent to the client, such as `not_found`. */
|
|
abstract public function errorCode(): string;
|
|
|
|
/** @return array<string, string> */
|
|
public function headers(): array
|
|
{
|
|
return [];
|
|
}
|
|
|
|
/** @return array<string, mixed> */
|
|
public function toArray(): array
|
|
{
|
|
return ['code' => $this->errorCode(), 'message' => $this->getMessage()];
|
|
}
|
|
}
|