content: package com.taskmanager.taskmanager;
  
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.List;
  
  import org.springframework.http.HttpHeaders;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.HttpStatusCode;
  import org.springframework.http.ResponseEntity;
  import org.springframework.web.bind.MethodArgumentNotValidException;
  import org.springframework.web.bind.annotation.ControllerAdvice;
  import org.springframework.web.bind.annotation.ExceptionHandler;
  import org.springframework.web.context.request.WebRequest;
  import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
  
  import com.taskmanager.taskmanager.exception.ErrorResponse;
  import com.taskmanager.taskmanager.exception.TaskNotFoundException;
  
  @ControllerAdvice
  public class ApplicationExceptionHandler extends ResponseEntityExceptionHandler {
      @ExceptionHandler(TaskNotFoundException.class)
      public ResponseEntity<Object> handleTaskNotFound(TaskNotFoundException ex) {
          ErrorResponse errors = new ErrorResponse(Arrays.asList(ex.getMessage()), 404);
          return new ResponseEntity<>(errors, HttpStatus.NOT_FOUND);
      }
  
      @ExceptionHandler(RuntimeException.class)
      public ResponseEntity<Object> handleException(RuntimeException ex) {
          ErrorResponse errors = new ErrorResponse(Arrays.asList(ex.getMessage()), 404);
          return new ResponseEntity<>(errors, HttpStatus.NOT_FOUND);
      }
  
      @Override
      protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
              HttpHeaders headers, HttpStatusCode status, WebRequest request) {
          List<String> messages = new ArrayList<>();
          ex.getBindingResult().getAllErrors().forEach((error) -> messages.add(error.getDefaultMessage()));
          return new ResponseEntity<>(new ErrorResponse(messages, 400), HttpStatus.BAD_REQUEST);
      }
  }
  