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