From 8e1249e543d04a5c69b64b8674f96df163b1d83c Mon Sep 17 00:00:00 2001 From: Lee Peuker Date: Wed, 12 Apr 2023 19:31:42 +0200 Subject: [PATCH] Improve user create/edit error handling from backend --- public/js/settings-users.js | 13 ++++-- src/HttpController/Rest/UserController.php | 48 ++++++++++++++++------ src/ValueObject/Http/Response.php | 4 +- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/public/js/settings-users.js b/public/js/settings-users.js index dccb1149..cf1f67e4 100644 --- a/public/js/settings-users.js +++ b/public/js/settings-users.js @@ -45,6 +45,8 @@ function prepareCreateUserModal(name) { document.getElementById('userModalRepeatPasswordInput').value = '' document.getElementById('userModalIsAdminInput').checked = '' + document.getElementById('userModalAlerts').innerHTML = '' + // Remove class invalid-input from all (input) elements Array.from(document.querySelectorAll('.invalid-input')).forEach((el) => el.classList.remove('invalid-input')); } @@ -67,6 +69,8 @@ function prepareEditUserModal(id, name, email, isAdmin, password, repeatPassword document.getElementById('userModalPasswordInput').value = '' document.getElementById('userModalRepeatPasswordInput').value = '' + document.getElementById('userModalAlerts').innerHTML = '' + // Remove class invalid-input from all (input) elements Array.from(document.querySelectorAll('.invalid-input')).forEach((el) => el.classList.remove('invalid-input')); } @@ -139,7 +143,7 @@ document.getElementById('createUserButton').addEventListener('click', async () = }) if (response.status !== 200) { - setUserModalAlertServerError() + setUserModalAlertServerError(await response.text()) return } @@ -149,8 +153,8 @@ document.getElementById('createUserButton').addEventListener('click', async () = userModal.hide() }) -function setUserModalAlertServerError() { - document.getElementById('userModalAlerts').innerHTML = '' +function setUserModalAlertServerError(message = "Server error, please try again.") { + document.getElementById('userModalAlerts').innerHTML = '' } document.getElementById('updateUserButton').addEventListener('click', async () => { @@ -177,7 +181,8 @@ document.getElementById('updateUserButton').addEventListener('click', async () = }) if (response.status !== 200) { - setUserModalAlertServerError() + setUserModalAlertServerError(await response.text()) + return } diff --git a/src/HttpController/Rest/UserController.php b/src/HttpController/Rest/UserController.php index fd26bf52..502a0f14 100644 --- a/src/HttpController/Rest/UserController.php +++ b/src/HttpController/Rest/UserController.php @@ -2,6 +2,10 @@ namespace Movary\HttpController\Rest; +use Movary\Domain\User\Exception\EmailNotUnique; +use Movary\Domain\User\Exception\PasswordTooShort; +use Movary\Domain\User\Exception\UsernameInvalidFormat; +use Movary\Domain\User\Exception\UsernameNotUnique; use Movary\Domain\User\Service\Authentication; use Movary\Domain\User\UserApi; use Movary\Util\Json; @@ -25,12 +29,22 @@ public function createUser(Request $request) : Response $requestUserData = Json::decode($request->getBody()); - $this->userApi->createUser( - $requestUserData['email'], - $requestUserData['password'], - $requestUserData['name'], - $requestUserData['isAdmin'], - ); + try { + $this->userApi->createUser( + $requestUserData['email'], + $requestUserData['password'], + $requestUserData['name'], + $requestUserData['isAdmin'], + ); + } catch (EmailNotUnique) { + return Response::createBadRequest('Email already in use.'); + } catch (UsernameNotUnique) { + return Response::createBadRequest('Name already in use.'); + } catch (PasswordTooShort) { + return Response::createBadRequest('Password too short.'); + } catch (UsernameInvalidFormat) { + return Response::createBadRequest('Name is not in a valid format.'); + } return Response::createOk(); } @@ -78,12 +92,22 @@ public function updateUser(Request $request) : Response $requestUserData = Json::decode($request->getBody()); - $this->userApi->updateName($userId, $requestUserData['name']); - $this->userApi->updateEmail($userId, $requestUserData['email']); - $this->userApi->updateIsAdmin($userId, $requestUserData['isAdmin']); - - if ($requestUserData['password'] !== null) { - $this->userApi->updatePassword($userId, $requestUserData['password']); + try { + $this->userApi->updateName($userId, $requestUserData['name']); + $this->userApi->updateEmail($userId, $requestUserData['email']); + $this->userApi->updateIsAdmin($userId, $requestUserData['isAdmin']); + + if ($requestUserData['password'] !== null) { + $this->userApi->updatePassword($userId, $requestUserData['password']); + } + } catch (EmailNotUnique) { + return Response::createBadRequest('Email already in use.'); + } catch (UsernameNotUnique) { + return Response::createBadRequest('Name already in use.'); + } catch (PasswordTooShort) { + return Response::createBadRequest('Password too short.'); + } catch (UsernameInvalidFormat) { + return Response::createBadRequest('Name is not in a valid format.'); } return Response::createOk(); diff --git a/src/ValueObject/Http/Response.php b/src/ValueObject/Http/Response.php index 25c0fe78..4e1a595a 100644 --- a/src/ValueObject/Http/Response.php +++ b/src/ValueObject/Http/Response.php @@ -19,9 +19,9 @@ public static function create(StatusCode $statusCode, ?string $body = null, arra return new self($statusCode, $body, $headers); } - public static function createBadRequest() : self + public static function createBadRequest(?string $body = null) : self { - return new self(StatusCode::createBadRequest()); + return new self(StatusCode::createBadRequest(), $body); } public static function createCsv(string $body) : self