1
0
Fork 0
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:
Robin Appelman 2020-11-25 23:26:54 +01:00
commit c7b637a05f
6 changed files with 78 additions and 7 deletions

View file

@ -5,13 +5,14 @@ declare(strict_types=1);
namespace Demostf\API\Controllers; namespace Demostf\API\Controllers;
use Demostf\API\Demo\DemoStore; use Demostf\API\Demo\DemoStore;
use Demostf\API\Error\InvalidHashException;
use Demostf\API\Error\InvalidKeyException;
use Demostf\API\Providers\ChatProvider; use Demostf\API\Providers\ChatProvider;
use Demostf\API\Providers\DemoListProvider; use Demostf\API\Providers\DemoListProvider;
use Demostf\API\Providers\DemoProvider; use Demostf\API\Providers\DemoProvider;
use flight\net\Request; use flight\net\Request;
use flight\net\Response; use flight\net\Response;
use function intval; use function intval;
use InvalidArgumentException;
use function is_array; use function is_array;
class DemoController extends BaseController { class DemoController extends BaseController {
@ -140,7 +141,7 @@ class DemoController extends BaseController {
$url = (string) $this->post('url', ''); $url = (string) $this->post('url', '');
$editKey = (string) $this->post('key', ''); $editKey = (string) $this->post('key', '');
if ($editKey !== $this->editKey || '' === $editKey) { if ($editKey !== $this->editKey || '' === $editKey) {
throw new InvalidArgumentException('Invalid key'); throw new InvalidKeyException('Invalid key');
} }
$demo = $this->demoProvider->get((int) $id); $demo = $this->demoProvider->get((int) $id);
@ -152,7 +153,7 @@ class DemoController extends BaseController {
$this->store->remove($demo); $this->store->remove($demo);
} }
} else { } else {
throw new InvalidArgumentException('Invalid demo hash'); throw new InvalidHashException('Invalid demo hash');
} }
} }
} }

View 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 {
}

View 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 {
}

View file

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace Demostf\API; namespace Demostf\API;
use Demostf\API\Error\InvalidHashException;
use Demostf\API\Error\InvalidKeyException;
use Flight; use Flight;
/** @var Container $container */ /** @var Container $container */
@ -66,4 +68,14 @@ Flight::route('/auth/handle/@token', [$authController, 'handle']);
Flight::route('/auth/login/@token', [$authController, 'login']); Flight::route('/auth/login/@token', [$authController, 'login']);
Flight::route('/auth/logout/@token', [$authController, 'logout']); 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(); Flight::start();

View file

@ -8,6 +8,8 @@ use Demostf\API\Controllers\DemoController;
use Demostf\API\Demo\ChatMessage; use Demostf\API\Demo\ChatMessage;
use Demostf\API\Demo\Demo; use Demostf\API\Demo\Demo;
use Demostf\API\Demo\DemoStore; use Demostf\API\Demo\DemoStore;
use Demostf\API\Error\InvalidHashException;
use Demostf\API\Error\InvalidKeyException;
use Demostf\API\Providers\ChatProvider; use Demostf\API\Providers\ChatProvider;
use Demostf\API\Providers\DemoListProvider; use Demostf\API\Providers\DemoListProvider;
use Demostf\API\Providers\DemoProvider; use Demostf\API\Providers\DemoProvider;
@ -90,7 +92,7 @@ class DemoControllerTest extends ControllerTest {
'key' => 'invalid', 'key' => 'invalid',
]); ]);
$this->expectException(InvalidArgumentException::class); $this->expectException(InvalidKeyException::class);
$this->expectExceptionMessage('Invalid key'); $this->expectExceptionMessage('Invalid key');
$controller->setDemoUrl('1'); $controller->setDemoUrl('1');
@ -105,7 +107,7 @@ class DemoControllerTest extends ControllerTest {
'key' => 'supersecretkey', 'key' => 'supersecretkey',
]); ]);
$this->expectException(InvalidArgumentException::class); $this->expectException(InvalidHashException::class);
$this->expectExceptionMessage('Invalid demo hash'); $this->expectExceptionMessage('Invalid demo hash');
$demo = $this->createConfiguredMock(Demo::class, [ $demo = $this->createConfiguredMock(Demo::class, [

View file

@ -201,7 +201,7 @@ describe("Set url", function () {
} }
}); });
expect(setUrl).to.be.containsText('Invalid key'); expect(setUrl).to.be.containsText('Invalid key');
expect(setUrl).to.have.status(500); expect(setUrl).to.have.status(401);
return chakram.wait(); return chakram.wait();
}); });
}); });
@ -220,7 +220,7 @@ describe("Set url", function () {
} }
}); });
expect(setUrl).to.be.containsText('Invalid demo hash'); expect(setUrl).to.be.containsText('Invalid demo hash');
expect(setUrl).to.have.status(500); expect(setUrl).to.have.status(412);
return chakram.wait(); return chakram.wait();
}); });
}); });