package uk.ac.ebi.quickgo.rest;

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

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
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 meaningful messages 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(ParameterException.class)
    protected ResponseEntity<ErrorInfo> handleParameterErrorRequest(ParameterException ex, HttpServletRequest request) {
        ErrorInfo error = new ErrorInfo(request.getRequestURL().toString(),
                ex.getMessages().collect(Collectors.toList()));
        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 List<String> messages;

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

            this.url = url;
            this.messages = Collections.singletonList(message);
        }

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

            this.url = url;
            this.messages = messages;
        }

        public String getUrl() {
            return url;
        }

        public List<String> getMessages() {
            return messages;
        }
    }
}
@@ -721,16 +721,48 @@
 andler(%7B
+IllegalArgumentException.class, 
 IllegalS
@@ -807,16 +807,28 @@
 n.class,
+%0A           
  Service
@@ -1049,32 +1049,32 @@
 .getMessage());%0A
+
         return n
@@ -1146,339 +1146,8 @@
  %7D%0A%0A
-    @ExceptionHandler(IllegalArgumentException.class)%0A    protected ResponseEntity%3CErrorInfo%3E handleBadRequest(RuntimeException ex, HttpServletRequest request) %7B%0A        ErrorInfo error = new ErrorInfo(request.getRequestURL().toString(), ex.getMessage());%0A        return new ResponseEntity%3C%3E(error, HttpStatus.BAD_REQUEST);%0A    %7D%0A%0A
     

Remove special treatment of the IllegalArgumentException.