58 lines
2.2 KiB
Java
58 lines
2.2 KiB
Java
package ba.steleks.repository;
|
|
|
|
import com.fasterxml.jackson.databind.util.JSONPObject;
|
|
import jdk.nashorn.internal.runtime.JSONFunctions;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
import org.springframework.context.MessageSource;
|
|
import org.springframework.context.i18n.LocaleContextHolder;
|
|
import org.springframework.data.rest.webmvc.support.RepositoryConstraintViolationExceptionMessage;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.validation.BindingResult;
|
|
import org.springframework.validation.FieldError;
|
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
|
import org.springframework.web.client.HttpStatusCodeException;
|
|
import org.springframework.web.servlet.ModelAndView;
|
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Locale;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* Created by admin on 01/04/2017.
|
|
*/
|
|
@ControllerAdvice
|
|
public class RestErrorHandler extends ResponseEntityExceptionHandler {
|
|
|
|
@ExceptionHandler(javax.validation.ConstraintViolationException.class)
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
@ResponseBody
|
|
public Map<String, String> handleCustomException(javax.validation.ConstraintViolationException ex) {
|
|
|
|
Map<String, String> map= new HashMap<String, String>();
|
|
map.put("status", HttpStatus.BAD_REQUEST.toString());
|
|
map.put("error", ex.getLocalizedMessage());
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
@ExceptionHandler(HttpStatusCodeException.class)
|
|
@ResponseBody
|
|
public ResponseEntity<Map<String, String>> handleAllExceptions(HttpStatusCodeException ex) {
|
|
Map<String, String> map= new HashMap<String, String>();
|
|
map.put("status", ex.getStatusCode().toString());
|
|
map.put("error", ex.getLocalizedMessage());
|
|
|
|
return ResponseEntity.status(ex.getStatusCode()).body(map);
|
|
}
|
|
|
|
}
|