简体   繁体   中英

Java - throwing exception still generating 200 status code

I have a method which should throw an exception if the input is blank. The input, in this case, a list, will throw an exception if it is empty. I have debugged this using JUnit and can confirm that passing in [] would throw the exception. In my front end application however, passing in an empty list still shows the response status as 200 (as seen in the developer console). Shouldn't throwing an exception generate a response status of 500? At least that's what I'm looking for, so that way I can respond with an error message to the user. How can I get the below to generate a response status of 500? I'm using spring framework.

public Boolean validate(List<HashMap<String, Object>> myList) throws MyException{
 Boolean hasErrors = false;
     if(!myList.isEmpty()){
        //logic goes here
        ....
    } else{
       hasErrors = true;
       throw new MyException("myList shouldn't be empty ");
   }
   return hasErrors;
}

Exception class here:

public class MyException extends Exception {
  public MyException() { super(); }
  public MyException(String message) { super(message); }
  public MyException(String message, Throwable cause) { super(message, cause); }
  public MyException(Throwable cause) { super(cause); }
}

Any help would be appreciated.

I would suggest that you use proper exception handling using Spring MVC , that way, it would be much more clear on what should be returned as status code when throwing an exception.

For example, you can do something like this in your exception class:

@ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR, reason="myList shouldn't be empty")
public class MyException extends Exception {
  public MyException() { super(); }
  public MyException(String message) { super(message); }
  public MyException(String message, Throwable cause) { super(message, cause); }
  public MyException(Throwable cause) { super(cause); }
}

You can use a @ControllerAdvice to handle exception that you throw then specify what return status you want to the client.

Here is an example that I have that I test to see if the file the user is uploading is too large for the website and return a bad request (html code 400).

The toExceptionResponeString is just a method that converts my exception to a json string, any string will do for the return.

You can use your MyException class in lieu of the MaxUploadSizeExceededException and handle it from there.

@ControllerAdvice
public class ExceptionController extends ResponseEntityExceptionHandler {

  @Autowired
  private ApplicationLogService appLogService;

  @ExceptionHandler(value = { MaxUploadSizeExceededException.class })
  protected ResponseEntity<Object> handleMaxUploadSizeExceededException(Exception ex, WebRequest request) {
    MaxUploadSizeExceededException musee = (MaxUploadSizeExceededException)ex;
    SizeLimitExceededException slee = musee.getCause() instanceof SizeLimitExceededException ? (SizeLimitExceededException) musee.getCause() : null;
    Long maxSize = slee == null ? musee.getMaxUploadSize() : slee.getPermittedSize();
    Long actualSize = slee == null ? Long.valueOf(request.getHeader("Content-Length")) : slee.getActualSize();

    PortalException pre = PortalException.fileSizeTooLarge(maxSize, actualSize);
    try {
      appLogService.addExceptionEntry(pre.getMessage());
    } catch (Exception e) { }
    return handleExceptionInternal(ex, toExceptionResponseString(pre), new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM