mirror of
https://codeberg.org/demostf/api.git
synced 2026-06-03 18:04:08 +02:00
better status codes, again
This commit is contained in:
parent
0d70d93e13
commit
c7b637a05f
6 changed files with 78 additions and 7 deletions
|
|
@ -5,13 +5,14 @@ declare(strict_types=1);
|
|||
namespace Demostf\API\Controllers;
|
||||
|
||||
use Demostf\API\Demo\DemoStore;
|
||||
use Demostf\API\Error\InvalidHashException;
|
||||
use Demostf\API\Error\InvalidKeyException;
|
||||
use Demostf\API\Providers\ChatProvider;
|
||||
use Demostf\API\Providers\DemoListProvider;
|
||||
use Demostf\API\Providers\DemoProvider;
|
||||
use flight\net\Request;
|
||||
use flight\net\Response;
|
||||
use function intval;
|
||||
use InvalidArgumentException;
|
||||
use function is_array;
|
||||
|
||||
class DemoController extends BaseController {
|
||||
|
|
@ -140,7 +141,7 @@ class DemoController extends BaseController {
|
|||
$url = (string) $this->post('url', '');
|
||||
$editKey = (string) $this->post('key', '');
|
||||
if ($editKey !== $this->editKey || '' === $editKey) {
|
||||
throw new InvalidArgumentException('Invalid key');
|
||||
throw new InvalidKeyException('Invalid key');
|
||||
}
|
||||
|
||||
$demo = $this->demoProvider->get((int) $id);
|
||||
|
|
@ -152,7 +153,7 @@ class DemoController extends BaseController {
|
|||
$this->store->remove($demo);
|
||||
}
|
||||
} else {
|
||||
throw new InvalidArgumentException('Invalid demo hash');
|
||||
throw new InvalidHashException('Invalid demo hash');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
28
src/Error/InvalidHashException.php
Normal file
28
src/Error/InvalidHashException.php
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?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 InvalidHashException extends \Exception {
|
||||
|
||||
}
|
||||
28
src/Error/InvalidKeyException.php
Normal file
28
src/Error/InvalidKeyException.php
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?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 InvalidKeyException extends \Exception {
|
||||
|
||||
}
|
||||
12
src/app.php
12
src/app.php
|
|
@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace Demostf\API;
|
||||
|
||||
use Demostf\API\Error\InvalidHashException;
|
||||
use Demostf\API\Error\InvalidKeyException;
|
||||
use Flight;
|
||||
|
||||
/** @var Container $container */
|
||||
|
|
@ -66,4 +68,14 @@ Flight::route('/auth/handle/@token', [$authController, 'handle']);
|
|||
Flight::route('/auth/login/@token', [$authController, 'login']);
|
||||
Flight::route('/auth/logout/@token', [$authController, 'logout']);
|
||||
|
||||
Flight::map('error', function(\Exception $ex){
|
||||
$code = 500;
|
||||
if ($ex instanceof InvalidKeyException) {
|
||||
$code = 401;
|
||||
} else if ($ex instanceof InvalidHashException) {
|
||||
$code = 412;
|
||||
}
|
||||
Flight::response()->status($code)->write($ex->getMessage())->send();
|
||||
});
|
||||
|
||||
Flight::start();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue