Skip to content

Commit c4496ed

Browse files
committed
enhance the error code/message for post
1 parent b860a8b commit c4496ed

3 files changed

Lines changed: 25 additions & 5 deletions

File tree

g11n-ws/modules/md-restful-api-i18n/src/main/java/com/vmware/vip/core/except/ExceptionHandle.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import jakarta.servlet.http.HttpServletResponse;
1919
import org.slf4j.Logger;
2020
import org.slf4j.LoggerFactory;
21+
import org.springframework.http.converter.HttpMessageNotReadableException;
2122
import org.springframework.web.HttpRequestMethodNotSupportedException;
2223
import org.springframework.web.bind.annotation.ControllerAdvice;
2324
import org.springframework.web.bind.annotation.ExceptionHandler;
@@ -67,6 +68,10 @@ public APIResponseDTO handler(Exception e) {
6768
logger.error("====== HTTP Exception =======");
6869
logger.error(e.getMessage());
6970
response.setResponse(new Response(APIResponseStatus.INTERNAL_SERVER_ERROR.getCode(), e.getMessage()));
71+
} else if (e instanceof HttpMessageNotReadableException) {
72+
logger.error("====== HttpMessageNotReadableException =======");
73+
logger.error(e.getMessage());
74+
response.setResponse(new Response(APIResponseStatus.BAD_REQUEST.getCode(), "Required request body is missing"));
7075
} else {
7176
response.setResponse(new Response(APIResponseStatus.UNKNOWN_ERROR.getCode(), e.getMessage()));
7277
String errorStr = MessageFormat.format("unknown error: {0}" ,e.getMessage());

g11n-ws/modules/md-restful-api-i18n/src/main/java/com/vmware/vip/i18n/api/v2/translation/TranslationProductComponentKeyAPI.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2023 VMware, Inc.
2+
* Copyright 2019-2026 VMware, Inc.
33
* SPDX-License-Identifier: EPL-2.0
44
*/
55
package com.vmware.vip.i18n.api.v2.translation;
@@ -14,6 +14,7 @@
1414

1515
import org.apache.commons.lang3.StringUtils;
1616
import org.springframework.beans.factory.annotation.Autowired;
17+
import org.springframework.beans.factory.annotation.Value;
1718
import org.springframework.http.HttpStatus;
1819
import org.springframework.web.bind.annotation.PathVariable;
1920
import org.springframework.web.bind.annotation.RequestBody;
@@ -47,8 +48,8 @@
4748
@RestController("v2-TranslationKeyAPI")
4849
public class TranslationProductComponentKeyAPI extends TranslationProductComponentKeyAction {
4950

50-
51-
51+
@Value("${translation.update.enable:true}")
52+
private boolean isTranslationUpdateEnabled;
5253

5354
@Autowired(required = false)
5455
MeterRegistry meterRegistry;
@@ -101,6 +102,9 @@ public APIResponseDTO getTranslationByPost(
101102
// @RequestHeader(required = true) String authorization,
102103
HttpServletRequest request, HttpServletResponse response)
103104
throws L3APIException,IOException {
105+
if (!isTranslationUpdateEnabled) {
106+
throw new L3APIException("Endpoint is disabled");
107+
}
104108
source = source != null ? source:"";
105109
if(meterRegistry!= null) {
106110
meterRegistry.counter("vip.translation.key", APIParamName.KEY, key).increment();
@@ -124,6 +128,9 @@ public APIResponseDTO postSources(
124128
@RequestBody List<KeySourceCommentDTO> sourceSet,
125129
@Parameter(name = APIParamName.COLLECT_SOURCE, description = APIParamValue.COLLECT_SOURCE) @RequestParam(value = APIParamName.COLLECT_SOURCE, required = false, defaultValue = "false") String collectSource,
126130
HttpServletRequest request) throws JsonProcessingException, VIPAPIException {
131+
if (!isTranslationUpdateEnabled) {
132+
throw new L3APIException("Endpoint is disabled");
133+
}
127134
request.setAttribute(ConstantsKeys.KEY, ConstantsKeys.JSON_KEYSET);
128135
ObjectMapper mapper = new ObjectMapper();
129136
String requestJson = mapper.writeValueAsString(sourceSet);

g11n-ws/modules/md-restful-api-i18n/src/main/java/com/vmware/vip/i18n/api/v2/translation/TranslationSourceAPI.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2023 VMware, Inc.
2+
* Copyright 2019-2026 VMware, Inc.
33
* SPDX-License-Identifier: EPL-2.0
44
*/
55
package com.vmware.vip.i18n.api.v2.translation;
@@ -10,6 +10,7 @@
1010
import jakarta.servlet.http.HttpServletRequest;
1111
import jakarta.servlet.http.HttpServletResponse;
1212

13+
import org.springframework.beans.factory.annotation.Value;
1314
import org.springframework.http.HttpStatus;
1415
import org.springframework.web.bind.annotation.PathVariable;
1516
import org.springframework.web.bind.annotation.RequestBody;
@@ -37,6 +38,9 @@
3738
@RestController("v2-TranslationSourceAPI")
3839
public class TranslationSourceAPI extends TranslationSourceAction {
3940

41+
@Value("${translation.update.enable:true}")
42+
private boolean isTranslationUpdateEnabled;
43+
4044
/**
4145
* Provide translation based on String
4246
*
@@ -108,6 +112,10 @@ public APIResponseDTO createSource (
108112
@Parameter(name = APIParamName.PSEUDO, description = APIParamValue.PSEUDO)
109113
@RequestParam(value = APIParamName.PSEUDO, required=false, defaultValue="false") String pseudo,
110114
HttpServletRequest request) throws L3APIException {
111-
return super.createSource(productName, component, version, locale, source, sourceFormat, collectSource, pseudo, request);
115+
if (isTranslationUpdateEnabled) {
116+
return super.createSource(productName, component, version, locale, source, sourceFormat, collectSource, pseudo, request);
117+
} else {
118+
throw new L3APIException("Endpoint is disabled");
119+
}
112120
}
113121
}

0 commit comments

Comments
 (0)