package com.example.rezevirajmasa.demo.config; import com.example.rezevirajmasa.demo.dto.ErrorDto; import com.example.rezevirajmasa.demo.model.exceptions.AppException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.http.converter.HttpMessageNotWritableException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; @ControllerAdvice public class RestExceptionHandler { @ExceptionHandler(value = {AppException.class}) @ResponseBody public ResponseEntity handleException(AppException ex) { return ResponseEntity.status(ex.getCode()) .body(ErrorDto.builder().message(ex.getMessage()).build()); } @ExceptionHandler(HttpMessageNotWritableException.class) public ResponseEntity handleHttpMessageNotWritableException(HttpMessageNotWritableException ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred while processing the response."); } }