Skip to content

Commit c74be90

Browse files
committed
Correctly handle InvalidProtocolBufferException in controllers
1 parent 02bef48 commit c74be90

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

plugin/src/main/shell/protoc-gen-flit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# The quiet flag is required otherwise the tooling will dump output to stdout which will break the
1616
# plugin output and generation will fail.
1717
# -----------------------------------------------------------------------------
18-
DIR=$(dirname $(readlink -f "$0"))
18+
DIR=$(dirname "$0")
1919

2020
JAR=$(ls -c ${DIR}/plugin-*-all.jar | head -1)
2121
java ${FLIT_JAVA_OPTS} -jar $JAR $@

runtime/spring/src/main/java/com/flit/runtime/spring/FlitExceptionHandler.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import com.flit.runtime.ErrorCode;
44
import com.flit.runtime.FlitException;
5+
import com.google.protobuf.InvalidProtocolBufferException;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
import javax.servlet.http.HttpServletRequest;
59
import org.slf4j.Logger;
610
import org.slf4j.LoggerFactory;
711
import org.springframework.http.MediaType;
@@ -11,15 +15,11 @@
1115
import org.springframework.web.bind.annotation.ExceptionHandler;
1216
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
1317

14-
import javax.servlet.http.HttpServletRequest;
15-
import java.util.HashMap;
16-
import java.util.Map;
17-
1818
@ControllerAdvice
1919
@Component
2020
public class FlitExceptionHandler extends ResponseEntityExceptionHandler {
2121

22-
private static final Logger LOGGER = LoggerFactory.getLogger(FlitExceptionHandler.class);
22+
private static final Logger LOGGER = LoggerFactory.getLogger(FlitExceptionHandler.class);
2323

2424

2525
@ExceptionHandler(Exception.class)
@@ -40,6 +40,21 @@ public ResponseEntity<?> handleException(HttpServletRequest request, Exception e
4040
.body(response);
4141
}
4242

43+
@ExceptionHandler(InvalidProtocolBufferException.class)
44+
public ResponseEntity<?> handlehandleInvalidProtocolBufferException(HttpServletRequest request, Exception e) {
45+
LOGGER.error("InvalidProtocolBufferException: request = {}, method = {}, msg= {}",
46+
request.getRequestURI(), request.getMethod(), e.getMessage(), e);
47+
48+
Map<String, Object> response = new HashMap<>();
49+
response.put("code", ErrorCode.INVALID_ARGUMENT);
50+
response.put("msg", e.getMessage());
51+
52+
return ResponseEntity
53+
.status(ErrorCode.INVALID_ARGUMENT.getHttpStatus())
54+
.contentType(MediaType.APPLICATION_JSON)
55+
.body(response);
56+
}
57+
4358
@ExceptionHandler(FlitException.class)
4459
public ResponseEntity<?> handleFlitException(HttpServletRequest request, FlitException e) {
4560

0 commit comments

Comments
 (0)