feat: add global exception handler

This commit is contained in:
gfxv 2025-03-12 19:45:33 +03:00
parent 951427e4e4
commit b916a49a39
5 changed files with 58 additions and 16 deletions

View File

@ -23,22 +23,14 @@ public class AuthController {
} }
@PostMapping("/register") @PostMapping("/register")
public ResponseEntity<?> registerUser(@RequestBody RegisterRequest registerRequest) { public ResponseEntity<?> registerUser(@RequestBody RegisterRequest request) {
try { String response = authService.registerUser(request);
String response = authService.registerUser(registerRequest); return ResponseEntity.ok(response);
return ResponseEntity.ok(response);
} catch (RuntimeException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
} }
@PostMapping("/login") @PostMapping("/login")
public ResponseEntity<?> authenticateUser(@RequestBody LoginRequest loginRequest) { public ResponseEntity<?> authenticateUser(@RequestBody LoginRequest request) {
try { JwtResponse jwtResponse = authService.authenticateUser(request);
JwtResponse jwtResponse = authService.authenticateUser(loginRequest); return ResponseEntity.ok(jwtResponse);
return ResponseEntity.ok(jwtResponse);
} catch (RuntimeException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
} }
} }

View File

@ -0,0 +1,7 @@
package dev.gfxv.blps.exception;
public class EmailAlreadyExistsException extends RuntimeException {
public EmailAlreadyExistsException(String message) {
super(message);
}
}

View File

@ -0,0 +1,34 @@
package dev.gfxv.blps.exception;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler({UsernameAlreadyExistsException.class, EmailAlreadyExistsException.class})
public ResponseEntity<String> handleAlreadyExists(UsernameAlreadyExistsException ex) {
System.out.println("triggered handleAlreadyExists");
return ResponseEntity.badRequest().body(ex.getMessage());
}
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<String> handleRuntimeException(RuntimeException ex) {
System.out.println("triggered handleRuntimeException");
System.out.println("AHTUNG AHTUNG:" + ex.getMessage());
return ResponseEntity
.internalServerError()
.body("An unexpected error occurred");
}
@ExceptionHandler(BadCredentialsException.class)
public ResponseEntity<String> handleBadCredentials(BadCredentialsException ex) {
System.out.println("triggered handleBadCredentials");
// TODO: probably log?
return ResponseEntity
.unprocessableEntity()
.body("Invalid username or password");
}
}

View File

@ -0,0 +1,7 @@
package dev.gfxv.blps.exception;
public class UsernameAlreadyExistsException extends RuntimeException {
public UsernameAlreadyExistsException(String message) {
super(message);
}
}

View File

@ -2,6 +2,8 @@ package dev.gfxv.blps.service;
import dev.gfxv.blps.entity.Role; import dev.gfxv.blps.entity.Role;
import dev.gfxv.blps.entity.User; import dev.gfxv.blps.entity.User;
import dev.gfxv.blps.exception.EmailAlreadyExistsException;
import dev.gfxv.blps.exception.UsernameAlreadyExistsException;
import dev.gfxv.blps.payload.request.LoginRequest; import dev.gfxv.blps.payload.request.LoginRequest;
import dev.gfxv.blps.payload.request.RegisterRequest; import dev.gfxv.blps.payload.request.RegisterRequest;
import dev.gfxv.blps.payload.response.JwtResponse; import dev.gfxv.blps.payload.response.JwtResponse;
@ -48,11 +50,11 @@ public class AuthService {
public String registerUser(RegisterRequest registerRequest) { public String registerUser(RegisterRequest registerRequest) {
if (userRepository.existsByUsername(registerRequest.getUsername())) { if (userRepository.existsByUsername(registerRequest.getUsername())) {
throw new RuntimeException("Error: Username is already taken!"); throw new UsernameAlreadyExistsException("Username is already taken!");
} }
if (userRepository.existsByEmail(registerRequest.getEmail())) { if (userRepository.existsByEmail(registerRequest.getEmail())) {
throw new RuntimeException("Error: Email is already in use!"); throw new EmailAlreadyExistsException("Email is already in use!");
} }
User user = new User(); User user = new User();