From 1e47dc583667141436fb37c53a41f3dfd4fae152 Mon Sep 17 00:00:00 2001 From: Hank CP Date: Fri, 22 Dec 2023 14:53:58 +0800 Subject: [PATCH] update client bad request ErrorInfo --- .../java/org/skr/common/exception/ErrorInfo.java | 1 + .../org/skr/config/GeneralExceptionHandler.java | 15 +++++++++++---- .../src/main/resources/skr-error.properties | 1 + 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/skr-common/src/main/java/org/skr/common/exception/ErrorInfo.java b/skr-common/src/main/java/org/skr/common/exception/ErrorInfo.java index fad75a2..aa9a1de 100644 --- a/skr-common/src/main/java/org/skr/common/exception/ErrorInfo.java +++ b/skr-common/src/main/java/org/skr/common/exception/ErrorInfo.java @@ -225,6 +225,7 @@ public ErrorInfo clone() { public static final ErrorInfo PARSE_METHOD_NOT_FOUND = ErrorInfo.of(1017, "error.PARSE_METHOD_NOT_FOUND", ErrorLevel.FATAL); public static final ErrorInfo INVALID_CONFIGURATION = ErrorInfo.of(1019, "error.INVALID_CONFIGURATION", ErrorLevel.FATAL); public static final ErrorInfo BAD_IMPLEMENTATION = ErrorInfo.of(1020, "error.BAD_IMPLEMENTATION", ErrorLevel.FATAL); + public static final ErrorInfo BAD_REQUEST = ErrorInfo.of(1021, "error.BAD_REQUEST"); public static final ErrorInfo AUTHENTICATION_REQUIRED = ErrorInfo.of(1100, "error.AUTHENTICATION_REQUIRED"); public static final ErrorInfo BAD_CERTIFICATION = ErrorInfo.of(1101, "error.BAD_CERTIFICATION"); diff --git a/skr-common/src/main/java/org/skr/config/GeneralExceptionHandler.java b/skr-common/src/main/java/org/skr/config/GeneralExceptionHandler.java index 50e05cb..5a4dd81 100644 --- a/skr-common/src/main/java/org/skr/config/GeneralExceptionHandler.java +++ b/skr-common/src/main/java/org/skr/config/GeneralExceptionHandler.java @@ -53,10 +53,17 @@ protected ResponseEntity handleExceptionInternal(Exception ex, Object bo HttpHeaders headers, HttpStatusCode status, WebRequest request) { - applicationContext.publishEvent(new ErrorOccurredEvent(ex, request)); - Object standerBody = Optional.ofNullable(body) - .orElse(exceptionFormatter.convert(ex)); - return super.handleExceptionInternal(ex, standerBody, headers, status, request); + Object respBody; + if (status.value() == HttpStatus.BAD_REQUEST.value() + || status.value() == HttpStatus.NOT_FOUND.value() + || status.value() == HttpStatus.METHOD_NOT_ALLOWED.value()) { + respBody = ErrorInfo.BAD_REQUEST.msgArgs(status.value()); + } else { + applicationContext.publishEvent(new ErrorOccurredEvent(ex, request)); + respBody = Optional.ofNullable(body) + .orElse(exceptionFormatter.convert(ex)); + } + return super.handleExceptionInternal(ex, respBody, headers, status, request); } @ExceptionHandler(ConfException.class) diff --git a/skr-common/src/main/resources/skr-error.properties b/skr-common/src/main/resources/skr-error.properties index 2d0f970..5f5cf2d 100644 --- a/skr-common/src/main/resources/skr-error.properties +++ b/skr-common/src/main/resources/skr-error.properties @@ -31,6 +31,7 @@ error.MISSING_ARGUMENT=\u53C2\u6570{0}\u672A\u8BBE\u7F6E error.PARSE_METHOD_NOT_FOUND=\u679A\u4E3E\u7C7B\u578B{0}\u672A\u6B63\u786E\u914D\u7F6E error.INVALID_CONFIGURATION=\u9519\u8BEF\u914D\u7F6E: {0} error.BAD_IMPLEMENTATION=\u903B\u8F91\u5F02\u5E38: {0} +error.BAD_REQUEST=\u975E\u6CD5\u8BF7\u6C42({0}) error.AUTHENTICATION_REQUIRED=\u672A\u63D0\u4F9B\u8BA4\u8BC1\u4FE1\u606F error.BAD_CERTIFICATION=\u65E0\u6548\u7684\u767B\u5F55\u51ED\u8BC1: {0}