simple chat creation

This commit is contained in:
Stepan Usatiuk
2023-12-22 14:01:03 +01:00
parent 9d38035205
commit 9910f38ec5
11 changed files with 186 additions and 12 deletions

View File

@@ -7,6 +7,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
@@ -18,28 +19,35 @@ import java.util.Objects;
@ControllerAdvice
public class ApiExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = {ConstraintViolationException.class})
@ExceptionHandler(ConstraintViolationException.class)
protected ResponseEntity<Object> handleConstraintViolation(ConstraintViolationException ex, WebRequest request) {
return handleExceptionInternal(ex,
new ErrorTo(ex.getConstraintViolations().stream().map(ConstraintViolation::getMessage), HttpStatus.BAD_REQUEST.value()),
new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
}
@ExceptionHandler(value = {AuthenticationException.class})
@ExceptionHandler(AuthenticationException.class)
protected ResponseEntity<Object> handleAuthenticationException(AuthenticationException ex, WebRequest request) {
return handleExceptionInternal(ex,
new ErrorTo(List.of(ex.getMessage()), HttpStatus.UNAUTHORIZED.value()),
new HttpHeaders(), HttpStatus.UNAUTHORIZED, request);
}
@ExceptionHandler(value = {ResponseStatusException.class})
@ExceptionHandler(UsernameNotFoundException.class)
protected ResponseEntity<Object> handleUsernameNotFoundException(UsernameNotFoundException ex, WebRequest request) {
return handleExceptionInternal(ex,
new ErrorTo(List.of(ex.getMessage()), HttpStatus.UNAUTHORIZED.value()),
new HttpHeaders(), HttpStatus.UNAUTHORIZED, request);
}
@ExceptionHandler(ResponseStatusException.class)
protected ResponseEntity<Object> handleResponseStatusException(ResponseStatusException ex, WebRequest request) {
return handleExceptionInternal(ex,
new ErrorTo(List.of(Objects.requireNonNullElse(ex.getReason(), ex.getStatusCode().toString())), ex.getStatusCode().value()),
new HttpHeaders(), ex.getStatusCode(), request);
}
@ExceptionHandler(value = {Exception.class})
@ExceptionHandler(Exception.class)
protected ResponseEntity<Object> handleGenericException(Exception ex, WebRequest request) {
return handleExceptionInternal(ex,
new ErrorTo(List.of("Error"), HttpStatus.INTERNAL_SERVER_ERROR.value()),