package com.mattrixwv.cipherstream.controller; import org.springframework.http.HttpStatus; 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.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import lombok.extern.slf4j.Slf4j; @Slf4j @RestControllerAdvice(annotations = RestController.class) public class ExceptionRestController extends ResponseEntityExceptionHandler{ private static final ObjectMapper mapper = new ObjectMapper(); @ExceptionHandler(InvalidCipherParameterException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseBody public ObjectNode invalidCipherParameterHandler(InvalidCipherParameterException error){ log.warn("Invalid user input was supplied: {}", error.getMessage()); ObjectNode returnJson = mapper.createObjectNode(); returnJson.put("message", error.getMessage()); return returnJson; } @ExceptionHandler(Exception.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ResponseBody public ObjectNode genericExceptionHandler(Exception error){ log.error(error.getMessage(), error); ObjectNode returnJson = mapper.createObjectNode(); returnJson.put("message", error.getMessage()); return returnJson; } }