use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
-use Symfony\Component\HttpKernel\Exception\HttpException;
+use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Throwable;
class Handler extends ExceptionHandler
$code = 500;
$headers = [];
- if ($e instanceof HttpException) {
+ if ($e instanceof HttpExceptionInterface) {
$code = $e->getStatusCode();
$headers = $e->getHeaders();
}
$code = $e->status;
}
- if (method_exists($e, 'getStatus')) {
- $code = $e->getStatus();
- }
-
$responseData['error']['code'] = $code;
return new JsonResponse($responseData, $code, $headers);
use Exception;
use Illuminate\Contracts\Support\Responsable;
+use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
-class NotifyException extends Exception implements Responsable
+class NotifyException extends Exception implements Responsable, HttpExceptionInterface
{
public $message;
- public $redirectLocation;
- protected $status;
+ public string $redirectLocation;
+ protected int $status;
public function __construct(string $message, string $redirectLocation = '/', int $status = 500)
{
$this->message = $message;
$this->redirectLocation = $redirectLocation;
$this->status = $status;
+
parent::__construct();
}
/**
- * Get the desired status code for this exception.
+ * Get the desired HTTP status code for this exception.
*/
- public function getStatus(): int
+ public function getStatusCode(): int
{
return $this->status;
}
+ /**
+ * Get the desired HTTP headers for this exception.
+ */
+ public function getHeaders(): array
+ {
+ return [];
+ }
+
/**
* Send the response for this type of exception.
*
// Front-end JSON handling. API-side handling managed via handler.
if ($request->wantsJson()) {
- return response()->json(['error' => $message], 403);
+ return response()->json(['error' => $message], $this->getStatusCode());
}
if (!empty($message)) {
use Exception;
use Illuminate\Contracts\Support\Responsable;
+use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
-class PrettyException extends Exception implements Responsable
+class PrettyException extends Exception implements Responsable, HttpExceptionInterface
{
- /**
- * @var ?string
- */
- protected $subtitle = null;
-
- /**
- * @var ?string
- */
- protected $details = null;
+ protected ?string $subtitle = null;
+ protected ?string $details = null;
/**
* Render a response for when this exception occurs.
*/
public function toResponse($request)
{
- $code = ($this->getCode() === 0) ? 500 : $this->getCode();
+ $code = $this->getStatusCode();
return response()->view('errors.' . $code, [
'message' => $this->getMessage(),
return $this;
}
+
+ /**
+ * Get the desired HTTP status code for this exception.
+ */
+ public function getStatusCode(): int
+ {
+ return ($this->getCode() === 0) ? 500 : $this->getCode();
+ }
+
+ /**
+ * Get the desired HTTP headers for this exception.
+ */
+ public function getHeaders(): array
+ {
+ return [];
+ }
}
$this->assertStringContainsString('testing', $html);
}
+ public function test_read_endpoint_returns_not_found()
+ {
+ $this->actingAsApiEditor();
+ // get an id that is not used
+ $id = Page::orderBy('id', 'desc')->first()->id + 1;
+ $this->assertNull(Page::find($id));
+
+ $resp = $this->getJson($this->baseEndpoint . "/$id");
+
+ $resp->assertNotFound();
+ $this->assertNull($resp->json('id'));
+ $resp->assertJsonIsObject('error');
+ $resp->assertJsonStructure([
+ 'error' => [
+ 'code',
+ 'message',
+ ],
+ ]);
+ $this->assertSame(404, $resp->json('error')['code']);
+ }
+
public function test_update_endpoint()
{
$this->actingAsApiEditor();