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")
public ResponseEntity<?> registerUser(@RequestBody RegisterRequest registerRequest) {
try {
String response = authService.registerUser(registerRequest);
return ResponseEntity.ok(response);
} catch (RuntimeException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
public ResponseEntity<?> registerUser(@RequestBody RegisterRequest request) {
String response = authService.registerUser(request);
return ResponseEntity.ok(response);
}
@PostMapping("/login")
public ResponseEntity<?> authenticateUser(@RequestBody LoginRequest loginRequest) {
try {
JwtResponse jwtResponse = authService.authenticateUser(loginRequest);
return ResponseEntity.ok(jwtResponse);
} catch (RuntimeException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
public ResponseEntity<?> authenticateUser(@RequestBody LoginRequest request) {
JwtResponse jwtResponse = authService.authenticateUser(request);
return ResponseEntity.ok(jwtResponse);
}
}

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.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.RegisterRequest;
import dev.gfxv.blps.payload.response.JwtResponse;
@ -48,11 +50,11 @@ public class AuthService {
public String registerUser(RegisterRequest registerRequest) {
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())) {
throw new RuntimeException("Error: Email is already in use!");
throw new EmailAlreadyExistsException("Email is already in use!");
}
User user = new User();