Skip to content

Commit

Permalink
Use custom UserNotFoundException instead of UsernameNotFoundException
Browse files Browse the repository at this point in the history
Since the latter results in returning '401 Unauthorized' but the expected behaviour is '404 Not Found'

Signed-off-by: Kshitij Patil <[email protected]>
  • Loading branch information
Kshitij09 committed Oct 8, 2021
1 parent a513c3c commit f1afa26
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
import com.kshitijpatil.tazabazar.apiv2.userauth.Role;
import com.kshitijpatil.tazabazar.security.jwt.RefreshTokenNotFoundException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.List;

public interface IUserService {
UserAuthView createUser(CreateUserRequest user) throws UsernameExistsException, PhoneExistsException;

void storeRefreshTokenFor(String username, String refreshToken) throws UsernameNotFoundException;
void storeRefreshTokenFor(String username, String refreshToken) throws UserNotFoundException;

UserView loadUserViewByUsername(String username) throws UsernameNotFoundException;
UserView loadUserViewByUsername(String username) throws UserNotFoundException;

UserAuthView loadUserAuthViewByUsername(String username) throws UsernameNotFoundException;
UserAuthView loadUserAuthViewByUsername(String username) throws UserNotFoundException;

List<UserAuthView> loadAllUsers();

Expand All @@ -26,9 +25,9 @@ public interface IUserService {

void clearAll();

UserDetailView updateCart(String username, List<CartItemDto> cartItems) throws InventoryNotFoundException, UsernameNotFoundException;
UserDetailView updateCart(String username, List<CartItemDto> cartItems) throws InventoryNotFoundException, UserNotFoundException;

List<CartItemDto> getCartOf(String username) throws UsernameNotFoundException;
List<CartItemDto> getCartOf(String username) throws UserNotFoundException;

void deleteUserByUsername(String username) throws UserNotFoundException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Objects;
import java.util.stream.Collectors;

import static com.kshitijpatil.tazabazar.utils.ExceptionUtils.userNotFoundExceptionSupplier;
import static com.kshitijpatil.tazabazar.utils.ExceptionUtils.usernameNotFoundExceptionSupplier;


Expand Down Expand Up @@ -68,9 +69,9 @@ public UserAuthView createUser(CreateUserRequest request) throws UsernameExistsE
}

@Override
public void storeRefreshTokenFor(String username, String refreshToken) throws UsernameNotFoundException {
public void storeRefreshTokenFor(String username, String refreshToken) throws UserNotFoundException {
var user = userAccounts.findById(username)
.orElseThrow(usernameNotFoundExceptionSupplier(username));
.orElseThrow(userNotFoundExceptionSupplier(username));
user.setRefreshToken(refreshToken);
userAccounts.save(user);
}
Expand All @@ -91,18 +92,18 @@ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundEx
}

@Override
public UserView loadUserViewByUsername(String username) throws UsernameNotFoundException {
public UserView loadUserViewByUsername(String username) throws UserNotFoundException {
var userAuth = userAccounts.findById(username)
.orElseThrow(usernameNotFoundExceptionSupplier(username));
.orElseThrow(userNotFoundExceptionSupplier(username));
// At this point, we can be sure that user exists (one-to-one mapping)
var userDetails = users.findById(username).get();
return UserMapper.toUserView(userDetails, userAuth);
}

@Override
public UserAuthView loadUserAuthViewByUsername(String username) throws UsernameNotFoundException {
public UserAuthView loadUserAuthViewByUsername(String username) throws UserNotFoundException {
return userAccounts.getUserAuthViewByUsername(username)
.orElseThrow(usernameNotFoundExceptionSupplier(username));
.orElseThrow(userNotFoundExceptionSupplier(username));
}

@Override
Expand Down Expand Up @@ -138,9 +139,9 @@ public void clearAll() {
}

@Override
public UserDetailView updateCart(String username, List<CartItemDto> cartItems) throws InventoryNotFoundException, UsernameNotFoundException {
public UserDetailView updateCart(String username, List<CartItemDto> cartItems) throws InventoryNotFoundException, UserNotFoundException {
var user = users.findById(username)
.orElseThrow(usernameNotFoundExceptionSupplier(username));
.orElseThrow(userNotFoundExceptionSupplier(username));
user.clearCart();
cartItems.forEach(cartItem -> {
var inventory = inventories.findById(cartItem.inventoryId)
Expand All @@ -151,9 +152,9 @@ public UserDetailView updateCart(String username, List<CartItemDto> cartItems) t
}

@Override
public List<CartItemDto> getCartOf(String username) throws UsernameNotFoundException {
public List<CartItemDto> getCartOf(String username) throws UserNotFoundException {
var user = users.findById(username)
.orElseThrow(usernameNotFoundExceptionSupplier(username));
.orElseThrow(userNotFoundExceptionSupplier(username));
return user.cart.stream()
.map(UserMapper::toCartItemDto)
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kshitijpatil.tazabazar.utils;

import com.kshitijpatil.tazabazar.apiv2.userdetail.UserNotFoundException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.function.Supplier;
Expand All @@ -12,4 +13,8 @@ public static Supplier<UsernameNotFoundException> usernameNotFoundExceptionSuppl
public static UsernameNotFoundException makeUsernameNotFoundException(String username) {
return new UsernameNotFoundException(String.format("User: %s, not found", username));
}

public static Supplier<UserNotFoundException> userNotFoundExceptionSupplier(String username) {
return () -> new UserNotFoundException(username);
}
}

0 comments on commit f1afa26

Please sign in to comment.