Skip to content

Commit

Permalink
duplicated exception handler to one global
Browse files Browse the repository at this point in the history
  • Loading branch information
sparkoo committed Oct 29, 2018
1 parent e92db0e commit 29bc869
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
14 changes: 0 additions & 14 deletions src/main/java/cz/sparko/boxitory/controller/BoxController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
import cz.sparko.boxitory.service.BoxRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
Expand Down Expand Up @@ -52,15 +49,4 @@ public String latestBoxVersion(@PathVariable String boxName) {
.map(BoxVersion::getVersion)
.orElseThrow(() -> NotFoundException.boxNotFound(boxName));
}

@ExceptionHandler
public ResponseEntity<String> handleException(Exception e) {
final HttpStatus status;
if (e instanceof NotFoundException) {
status = HttpStatus.NOT_FOUND;
} else {
status = HttpStatus.INTERNAL_SERVER_ERROR;
}
return new ResponseEntity<>(e.getMessage(), status);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
Expand Down Expand Up @@ -53,15 +50,4 @@ public void downloadLatestBox(
LOG.info("Downloading latest version of box [{}], provider [{}]", boxName, boxProvider);
downloadBox(response, boxName, boxProvider, boxRepository.latestVersionOfBox(boxName, boxProvider));
}

@ExceptionHandler
public ResponseEntity<String> handleException(Exception e) {
final HttpStatus status;
if (e instanceof NotFoundException) {
status = HttpStatus.NOT_FOUND;
} else {
status = HttpStatus.INTERNAL_SERVER_ERROR;
}
return new ResponseEntity<>(e.getMessage(), status);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cz.sparko.boxitory.controller;

import cz.sparko.boxitory.conf.NotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalControllerExceptionHandler {
@ExceptionHandler
public ResponseEntity<String> handleException(Exception e) {
final HttpStatus status;
if (e instanceof NotFoundException) {
status = HttpStatus.NOT_FOUND;
} else {
status = HttpStatus.INTERNAL_SERVER_ERROR;
}
return new ResponseEntity<>(e.getMessage(), status);
}
}

0 comments on commit 29bc869

Please sign in to comment.