package uk.ac.ebi.quickgo.rest;

import uk.ac.ebi.quickgo.rest.search.RetrievalException;
import uk.ac.ebi.quickgo.rest.service.ServiceConfigException;

import javax.servlet.http.HttpServletRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * Handler responsible for returning error responses with a meaningful message to the REST client.
 *
 * @author Ricardo Antunes
 */
@ControllerAdvice
public class ResponseExceptionHandler {
    @ExceptionHandler({IllegalStateException.class, RetrievalException.class, ServiceConfigException.class})
    protected ResponseEntity<ErrorInfo> handleInternalServer(RuntimeException ex, HttpServletRequest request) {
        ErrorInfo error = new ErrorInfo(request.getRequestURL().toString(), ex.getMessage());
        return new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(IllegalArgumentException.class)
    protected ResponseEntity<ErrorInfo> handleBadRequest(RuntimeException ex, HttpServletRequest request) {
        ErrorInfo error = new ErrorInfo(request.getRequestURL().toString(), ex.getMessage());
        return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler(ResourceNotFoundException.class)
    protected ResponseEntity<ErrorInfo> handleNotFoundRequest(RuntimeException ex, HttpServletRequest request) {
        ErrorInfo error = new ErrorInfo(request.getRequestURL().toString(), ex.getMessage());
        return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
    }

    public static class ErrorInfo {
        private final String url;
        private final String message;

        public ErrorInfo(String url, String message) {
            assert url != null : "Error URL can't be null";
            assert message != null : "Error message can't be null";

            this.url = url;
            this.message = message;
        }

        public String getUrl() {
            return url;
        }

        public String getMessage() {
            return message;
        }
    }
}
@@ -146,16 +146,105 @@
 ption;%0A%0A
+import java.util.Collections;%0Aimport java.util.List;%0Aimport java.util.stream.Collectors;%0A
 import j
@@ -586,16 +586,17 @@
  message
+s
  to the 
@@ -1423,32 +1423,416 @@
 EQUEST);%0A    %7D%0A%0A
+    @ExceptionHandler(ParameterException.class)%0A    protected ResponseEntity%3CErrorInfo%3E handleParameterErrorRequest(ParameterException ex, HttpServletRequest request) %7B%0A        ErrorInfo error = new ErrorInfo(request.getRequestURL().toString(),%0A                ex.getMessages().collect(Collectors.toList()));%0A        return new ResponseEntity%3C%3E(error, HttpStatus.BAD_REQUEST);%0A    %7D%0A%0A
     @ExceptionHa
@@ -2242,30 +2242,36 @@
 e final 
+List%3C
 String
+%3E
  message
 ;%0A%0A     
@@ -2262,16 +2262,17 @@
  message
+s
 ;%0A%0A     
@@ -2432,16 +2432,17 @@
  message
+s
  can't b
@@ -2507,18 +2507,316 @@
 sage
+s
  = 
-message
+Collections.singletonList(message);%0A        %7D%0A%0A        public ErrorInfo(String url, List%3CString%3E messages) %7B%0A            assert url != null : %22Error URL can't be null%22;%0A            assert messages != null : %22Error messages can't be null%22;%0A%0A            this.url = url;%0A            this.messages = messages
 ;%0A  
@@ -2903,30 +2903,36 @@
      public 
+List%3C
 String
+%3E
  getMessage(
@@ -2930,16 +2930,17 @@
 tMessage
+s
 () %7B%0A   
@@ -2962,16 +2962,17 @@
  message
+s
 ;%0A      

Add code in the ResponseExceptionHandler to handle ParameterExceptions correctly.