Changed error response encoding

This commit is contained in:
Mikhail Trofimov 2025-06-27 10:16:24 +03:00
parent a1fb717e10
commit 4d08bf0d27

View file

@ -1,5 +1,7 @@
package ru.nbch.credit_tracker.config.rest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ -7,15 +9,19 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
import ru.nbch.credit_tracker.entities.response.errors.CommonError;
import ru.nbch.credit_tracker.exceptions.SignalErrorException;
import java.nio.charset.Charset;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value = SignalErrorException.class, produces = MediaType.APPLICATION_XML_VALUE)
public ResponseEntity<CommonError> handleInvalidUser(SignalErrorException ex) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(new MediaType("application", "xml", Charset.forName("windows-1251")));
if (ex.isUserError()) {
return ResponseEntity.badRequest().body(ex.getError());
return new ResponseEntity<>(ex.getError(), headers, HttpStatus.BAD_REQUEST);
} else {
return ResponseEntity.internalServerError().body(ex.getError());
return new ResponseEntity<>(ex.getError(), headers, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}