Skip to content

Commit

Permalink
Improve user create/edit error handling from backend
Browse files Browse the repository at this point in the history
  • Loading branch information
leepeuker committed Apr 12, 2023
1 parent d8c667c commit 8e1249e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
13 changes: 9 additions & 4 deletions public/js/settings-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand All @@ -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'));
}
Expand Down Expand Up @@ -139,7 +143,7 @@ document.getElementById('createUserButton').addEventListener('click', async () =
})

if (response.status !== 200) {
setUserModalAlertServerError()
setUserModalAlertServerError(await response.text())
return
}

Expand All @@ -149,8 +153,8 @@ document.getElementById('createUserButton').addEventListener('click', async () =
userModal.hide()
})

function setUserModalAlertServerError() {
document.getElementById('userModalAlerts').innerHTML = '<div class="alert alert-danger alert-dismissible" role="alert">Server error, please try again.<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>'
function setUserModalAlertServerError(message = "Server error, please try again.") {
document.getElementById('userModalAlerts').innerHTML = '<div class="alert alert-danger alert-dismissible" role="alert">' + message + '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>'
}

document.getElementById('updateUserButton').addEventListener('click', async () => {
Expand All @@ -177,7 +181,8 @@ document.getElementById('updateUserButton').addEventListener('click', async () =
})

if (response.status !== 200) {
setUserModalAlertServerError()
setUserModalAlertServerError(await response.text())

return
}

Expand Down
48 changes: 36 additions & 12 deletions src/HttpController/Rest/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/ValueObject/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8e1249e

Please sign in to comment.