Commit 00e4180a authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Add exception messages to not found exceptions missing messages

parent 42e894d3
......@@ -47,35 +47,36 @@ class Handler extends ExceptionHandler
*/
public function render($request, Throwable $e)
{
if (
$e instanceof EntityNotFoundException ||
$e instanceof EntityExistsException
) {
return $this->respondWithError($e->getMessage(), $e->getCode());
}
if ($request->expectsJson()) {
if (
$e instanceof EntityNotFoundException ||
$e instanceof EntityExistsException
) {
return $this->respondWithError($e->getMessage(), $e->getCode());
}
if ($e instanceof SourceAuthenticationException) {
return response()->json(['message' => $e->getMessage()], 401);
}
if ($e instanceof SourceAuthenticationException) {
return response()->json(['message' => $e->getMessage()], 401);
}
if ($e instanceof AuthorizationException) {
if ($request->is('api/door/*')) {
/** @var DoorGuard $doorGuard */
$doorGuard = app()->make(DoorGuard::class);
if ($e instanceof AuthorizationException) {
if ($request->is('api/door/*')) {
/** @var DoorGuard $doorGuard */
$doorGuard = app()->make(DoorGuard::class);
if (!$doorGuard->id()) {
// Should not be authenticated
return response()->json(['message' => $e->getMessage()], 401);
}
if (!$doorGuard->id()) {
// Should not be authenticated
return response()->json(['message' => $e->getMessage()], 401);
}
$presenter = new JsonPresenter();
/** @var ScheduleEventsUseCase $useCase */
$useCase = app()->make(ScheduleEventsUseCase::class);
$useCase->getStatusForDoor($doorGuard->id(), null, Carbon::now(), Carbon::now()->addMinutes(config('app.status_foresight')), $presenter);
return new JsonResponse($presenter->getViewModel(), 403);
}
$presenter = new JsonPresenter();
/** @var ScheduleEventsUseCase $useCase */
$useCase = app()->make(ScheduleEventsUseCase::class);
$useCase->getStatusForDoor($doorGuard->id(), null, Carbon::now(), Carbon::now()->addMinutes(config('app.status_foresight')), $presenter);
return new JsonResponse($presenter->getViewModel(), 403);
}
if ($request->expectsJson()) {
return response()->json(['message' => $e->getMessage()], 403);
}
}
......
......@@ -57,6 +57,12 @@ class GroupRelationController extends Controller
));
}
/**
* @param string $groupId
* @param \Source\UseCases\GroupSchedule\GetGroupSchedules\GetGroupSchedulesUseCase $groupSchedules
* @return \Illuminate\View\View
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function schedules(string $groupId, GetGroupSchedulesUseCase $groupSchedules): View
{
$presenter = new GroupSchedulesPresenter();
......
......@@ -23,7 +23,7 @@ class DatabaseDoorGroupRepository implements DoorGroupRepository
$door = Door::query()->find((self::castToInt($doorId)));
if (!$door) {
throw new EntityNotFoundException();
throw new EntityNotFoundException('Door does not exist.');
}
return array_map(static function (Group $group) {
......@@ -40,7 +40,7 @@ class DatabaseDoorGroupRepository implements DoorGroupRepository
$group = Group::query()->find(self::castToInt($groupId));
if (!$group) {
throw new EntityNotFoundException();
throw new EntityNotFoundException('Group does not exist.');
}
return array_map(static function (Door $door) {
......
......@@ -40,7 +40,7 @@ class InMemoryDoorGroupRepository implements DoorGroupRepository
public function getGroupsForDoor(string $doorId): array
{
if (!$this->doors->exists($doorId)) {
throw new EntityNotFoundException();
throw new EntityNotFoundException('Door does not exist.');
}
if (!isset($this->doorMap[$doorId])) {
......@@ -64,7 +64,7 @@ class InMemoryDoorGroupRepository implements DoorGroupRepository
public function getDoorsForGroup(string $groupId): array
{
if (!$this->groups->get($groupId)) {
throw new EntityNotFoundException();
throw new EntityNotFoundException('Group does not exist.');
}
$doors = [];
......
......@@ -36,7 +36,7 @@ class DatabaseGroupScheduleRepository implements GroupScheduleRepository
$schedule = Schedule::query()->find(self::castToInt($scheduleId));
if (!$schedule) {
return [];
throw new EntityNotFoundException('Schedules does not exist.');
}
return array_map(
......@@ -54,7 +54,7 @@ class DatabaseGroupScheduleRepository implements GroupScheduleRepository
$group = Group::query()->find(self::castToInt($groupId));
if (!$group) {
return [];
throw new EntityNotFoundException('Group does not exist.');
}
return array_map(
......
......@@ -11,12 +11,14 @@ interface GroupScheduleRepository
/**
* @param string $scheduleId
* @return \Source\Entities\Group[]
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function getGroupsForSchedule(string $scheduleId): array;
/**
* @param string $groupId
* @return \Source\Entities\Schedule[]
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function getSchedulesForGroup(string $groupId): array;
......
......@@ -56,7 +56,7 @@ class InMemoryGroupScheduleRepository implements GroupScheduleRepository
public function getSchedulesForGroup(string $groupId): array
{
if (!isset($this->groupMap[$groupId])) {
return [];
throw new EntityNotFoundException('Group does not exist.');
}
$schedules = [];
......
......@@ -23,7 +23,7 @@ class DatabaseGroupUserRepository implements GroupUserRepository
$user = User::query()->find(self::castToInt($userId));
if (!$user) {
throw new EntityNotFoundException();
throw new EntityNotFoundException('User does snot exist.');
}
return array_map(static function (Group $group) {
......@@ -40,7 +40,7 @@ class DatabaseGroupUserRepository implements GroupUserRepository
$group = Group::query()->find(self::castToInt($groupId));
if (!$group) {
throw new EntityNotFoundException();
throw new EntityNotFoundException('Group does not exist.');
}
return array_map(static function (User $user) {
......
......@@ -38,7 +38,7 @@ class InMemoryGroupUserRepository implements GroupUserRepository
public function getGroupsForUser(string $userId): array
{
if (!$this->users->exists($userId)) {
throw new EntityNotFoundException();
throw new EntityNotFoundException('User does not exist.');
}
if (!isset($this->groupMap[$userId])) {
......@@ -62,7 +62,7 @@ class InMemoryGroupUserRepository implements GroupUserRepository
public function getUsersForGroup(string $groupId): array
{
if (!$this->groups->get($groupId)) {
throw new EntityNotFoundException();
throw new EntityNotFoundException('Group does not exist.');
}
$users = [];
......
......@@ -59,7 +59,7 @@ class InMemoryTokensRepository implements TokensRepository
}
}
throw new EntityNotFoundException();
throw new EntityNotFoundException('Token does not exist.');
}
/**
......@@ -130,7 +130,7 @@ class InMemoryTokensRepository implements TokensRepository
}
}
throw new EntityNotFoundException();
throw new EntityNotFoundException('Token does not exist.');
}
/**
......
......@@ -28,13 +28,13 @@ class UpdateBinary implements UpdateBinaryUseCase
if (!$doorId || !$this->updateBinaryDirectory) {
// no door exists to find an update for
// Or there is no update directory specified
throw new EntityNotFoundException();
throw new EntityNotFoundException('You must provide a door.');
}
$binaries = $this->fs->getFilesForDirectory($this->updateBinaryDirectory);
if (!$binaries) {
throw new EntityNotFoundException();
throw new EntityNotFoundException('No available binaries.');
}
rsort($binaries);
......
......@@ -3,6 +3,7 @@
namespace Source\UseCases\DoorUser\UserDoorAccess;
use Source\Entities\Schedule;
use Source\Exceptions\EntityNotFoundException;
use Source\Gateways\DoorGroup\DoorGroupRepository;
use Source\Gateways\GroupUser\GroupUserRepository;
use Source\Gateways\GroupSchedule\GroupScheduleRepository;
......@@ -48,7 +49,13 @@ class UserDoorAccess implements UserDoorAccessUseCase
$groupId = $group->getId();
$doors = $this->doorGroupRepository->getDoorsForGroup($groupId);
$schedules = $this->schedulesRepository->getSchedulesForGroup($groupId);
$schedules = [];
try {
$schedules = $this->schedulesRepository->getSchedulesForGroup($groupId);
} catch (EntityNotFoundException $e) {
}
foreach ($doors as $door) {
$response->addDoorToGroup($groupId, $door);
......
......@@ -28,7 +28,7 @@ class DeleteDoor implements DeleteDoorUseCase
if ($this->doors->delete($doorId) > 0) {
$text = 'Door deleted.';
} else {
throw new EntityNotFoundException();
throw new EntityNotFoundException('Door does not exist.');
}
$response = new ResponseModel($text);
......
......@@ -37,7 +37,7 @@ class GenerateDoorToken implements GenerateDoorTokenUseCase
public function generate(string $doorId, Presenter $presenter): void
{
if (!($door = $this->doors->get($doorId))) {
throw new EntityNotFoundException();
throw new EntityNotFoundException('Door does not exist.');
}
$token = $this->tokens::generateTokenString();
......
......@@ -28,7 +28,7 @@ class GetDoor implements GetDoorUseCase
$door = $this->doorsRepository->get($doorId);
if (!$door) {
throw new EntityNotFoundException();
throw new EntityNotFoundException('Door does not exist.');
}
$response = new ResponseModel($door);
......
......@@ -29,7 +29,7 @@ class UpdateDoor implements UpdateDoorUseCase
$door = $this->doors->get($doorId);
if (!$door) {
throw new EntityNotFoundException();
throw new EntityNotFoundException('Door does not exist.');
}
$newDoor = new Door(
......
......@@ -8,6 +8,7 @@ interface GetGroupSchedulesUseCase
/**
* @param string $groupId
* @param \Source\UseCases\GroupSchedule\GetGroupSchedules\Presenter $presenter
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function getSchedulesForGroup(string $groupId, Presenter $presenter): void;
}
......@@ -28,7 +28,7 @@ class DeleteGroup implements DeleteGroupUseCase
$group = $this->groups->get($groupId);
if (!$group) {
throw new EntityNotFoundException();
throw new EntityNotFoundException('Group does not exist.');
}
$reflection = new ReflectionClass(Permissions::class);
......
......@@ -25,7 +25,7 @@ class GetGroup implements GetGroupUseCase
$group = $this->groups->get($groupId);
if (!$group) {
throw new EntityNotFoundException();
throw new EntityNotFoundException('Group does not exist.');
}
$response = new ResponseModel($group);
......
......@@ -32,7 +32,7 @@ class UpdateGroup implements UpdateGroupUseCase
$group = $this->groups->get($groupId);
if (!$group) {
throw new EntityNotFoundException();
throw new EntityNotFoundException('Group does not exist.');
}
$response = new ResponseModel();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment