fix: standardize Chinese validation messages

master
chenyuan 4 weeks ago
parent 2938bd4bda
commit 6d01ee90e9

@ -67,7 +67,7 @@ public class PlatformTokenVerifier {
} catch (PlatformTokenException exception) { } catch (PlatformTokenException exception) {
throw exception; throw exception;
} catch (Exception exception) { } catch (Exception exception) {
throw new PlatformTokenException("Invalid platform token", exception); throw new PlatformTokenException("平台登录凭据无效", exception);
} }
} }
@ -85,5 +85,5 @@ public class PlatformTokenVerifier {
private long requiredLong(JsonNode payload, String name) { JsonNode node = payload.path(name); if (!node.canConvertToLong()) throw invalid(); return node.asLong(); } private long requiredLong(JsonNode payload, String name) { JsonNode node = payload.path(name); if (!node.canConvertToLong()) throw invalid(); return node.asLong(); }
private String requiredText(JsonNode payload, String name) { String value = payload.path(name).asText(); if (value == null || value.trim().isEmpty()) throw invalid(); return value; } private String requiredText(JsonNode payload, String name) { String value = payload.path(name).asText(); if (value == null || value.trim().isEmpty()) throw invalid(); return value; }
private String optionalText(JsonNode payload, String name) { JsonNode node = payload.path(name); return node.isMissingNode() || node.isNull() ? null : node.asText(); } private String optionalText(JsonNode payload, String name) { JsonNode node = payload.path(name); return node.isMissingNode() || node.isNull() ? null : node.asText(); }
private PlatformTokenException invalid() { return new PlatformTokenException("Invalid platform token"); } private PlatformTokenException invalid() { return new PlatformTokenException("平台登录凭据无效"); }
} }

@ -22,7 +22,7 @@ public class GlobalExceptionHandler {
@ExceptionHandler({ConstraintViolationException.class, MethodArgumentNotValidException.class}) @ExceptionHandler({ConstraintViolationException.class, MethodArgumentNotValidException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseStatus(HttpStatus.BAD_REQUEST)
public ApiResponse<Void> handleValidation(Exception exception) { public ApiResponse<Void> handleValidation(Exception exception) {
return ApiResponse.failure(ErrorCode.VALIDATION_ERROR, exception.getMessage(), traceId()); return ApiResponse.failure(ErrorCode.VALIDATION_ERROR, "请求参数校验失败", traceId());
} }
@ExceptionHandler(BusinessException.class) @ExceptionHandler(BusinessException.class)

@ -8,6 +8,7 @@ import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -26,4 +27,12 @@ class GlobalExceptionHandlerTest {
.andExpect(jsonPath("$.code").value("VALIDATION_ERROR")) .andExpect(jsonPath("$.code").value("VALIDATION_ERROR"))
.andExpect(jsonPath("$.traceId").isNotEmpty()); .andExpect(jsonPath("$.traceId").isNotEmpty());
} }
@Test
void invalidRequestBodyReturnsChineseValidationMessage() throws Exception {
mvc.perform(post("/api/v1/auth/login").contentType("application/json").content("{}"))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("VALIDATION_ERROR"))
.andExpect(jsonPath("$.message").value("请求参数校验失败"));
}
} }

Loading…
Cancel
Save