mirror of
https://codeberg.org/demostf/api.git
synced 2026-06-03 09:54:17 +02:00
proper error response for not found demos
This commit is contained in:
parent
6ccf387d1e
commit
202d11fcc9
7 changed files with 61 additions and 9 deletions
|
|
@ -7,6 +7,7 @@ namespace Demostf\API\Controllers;
|
|||
use Demostf\API\Demo\DemoStore;
|
||||
use Demostf\API\Error\InvalidHashException;
|
||||
use Demostf\API\Error\InvalidKeyException;
|
||||
use Demostf\API\Error\NotFoundException;
|
||||
use Demostf\API\Providers\ChatProvider;
|
||||
use Demostf\API\Providers\DemoListProvider;
|
||||
use Demostf\API\Providers\DemoProvider;
|
||||
|
|
@ -38,7 +39,12 @@ class DemoController extends BaseController {
|
|||
}
|
||||
|
||||
public function get(string $id): void {
|
||||
$this->json($this->demoProvider->get(\intval($id, 10)));
|
||||
$demo = $this->demoProvider->get(\intval($id, 10));
|
||||
if ($demo === null) {
|
||||
throw new NotFoundException("requested demo not found");
|
||||
} else {
|
||||
$this->json($demo);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -22,4 +22,7 @@ declare(strict_types=1);
|
|||
namespace Demostf\API\Error;
|
||||
|
||||
class InvalidHashException extends \Exception {
|
||||
public function __construct(string $message) {
|
||||
parent::__construct($message, 412);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,4 +22,7 @@ declare(strict_types=1);
|
|||
namespace Demostf\API\Error;
|
||||
|
||||
class InvalidKeyException extends \Exception {
|
||||
public function __construct(string $message) {
|
||||
parent::__construct($message, 401);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
30
src/Error/NotFoundException.php
Normal file
30
src/Error/NotFoundException.php
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Demostf\API\Error;
|
||||
|
||||
class NotFoundException extends \Exception {
|
||||
public function __construct(string $message) {
|
||||
parent::__construct($message, 404);
|
||||
}
|
||||
}
|
||||
|
|
@ -71,10 +71,8 @@ Flight::route('/auth/logout/@token', [$authController, 'logout']);
|
|||
|
||||
Flight::map('error', function (\Throwable $ex) {
|
||||
$code = 500;
|
||||
if ($ex instanceof InvalidKeyException) {
|
||||
$code = 401;
|
||||
} elseif ($ex instanceof InvalidHashException) {
|
||||
$code = 412;
|
||||
if ($ex->getCode()) {
|
||||
$code = $ex->getCode();
|
||||
}
|
||||
/** @var Response $response */
|
||||
$response = Flight::response()->status($code);
|
||||
|
|
|
|||
|
|
@ -29,10 +29,8 @@ Flight::route('/do_upload', [$uploadController, 'upload']);
|
|||
|
||||
Flight::map('error', function (\Throwable $ex) {
|
||||
$code = 500;
|
||||
if ($ex instanceof InvalidKeyException) {
|
||||
$code = 401;
|
||||
} elseif ($ex instanceof InvalidHashException) {
|
||||
$code = 412;
|
||||
if ($ex->getCode()) {
|
||||
$code = $ex->getCode();
|
||||
}
|
||||
/** @var Response $response */
|
||||
$response = Flight::response()->status($code);
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ use Demostf\API\Demo\Demo;
|
|||
use Demostf\API\Demo\DemoStore;
|
||||
use Demostf\API\Error\InvalidHashException;
|
||||
use Demostf\API\Error\InvalidKeyException;
|
||||
use Demostf\API\Error\NotFoundException;
|
||||
use Demostf\API\Providers\ChatProvider;
|
||||
use Demostf\API\Providers\DemoListProvider;
|
||||
use Demostf\API\Providers\DemoProvider;
|
||||
|
|
@ -228,4 +229,17 @@ class DemoControllerTest extends ControllerTest {
|
|||
$controller->listDemos();
|
||||
$this->assertResponseData(['dummy']);
|
||||
}
|
||||
|
||||
public function testGetNotFound() {
|
||||
$controller = $this->getController();
|
||||
|
||||
$this->demoProvider->expects($this->once())
|
||||
->method('get')
|
||||
->with(1)
|
||||
->willReturn(null);
|
||||
|
||||
$this->expectException(NotFoundException::class);
|
||||
|
||||
$controller->get('1');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue