Skip to content

Commit

Permalink
Handled error message for phone number update.
Browse files Browse the repository at this point in the history
  • Loading branch information
karthik-tarento committed Oct 4, 2022
1 parent 70b4652 commit e7bce21
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package org.sunbird.common.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.HttpClientErrorException;
Expand All @@ -12,6 +17,7 @@
import org.sunbird.core.logger.CbExtLogger;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

Expand Down Expand Up @@ -101,12 +107,13 @@ public Object fetchUsingGetWithHeaders(String uri, Map<String, String> headersVa
}
HttpEntity entity = new HttpEntity(headers);
response = restTemplate.exchange(uri, HttpMethod.GET, entity, Map.class);
return response.getBody();
} catch (HttpClientErrorException e) {
log.error(e);
} catch (Exception e) {
log.error(e);
}
return response.getBody();
return null;
}

public Object fetchUsingGetWithHeadersProfile(String uri, Map<String, String> headersValues) {
Expand Down Expand Up @@ -169,8 +176,6 @@ public Map<String, Object> fetchResultUsingPost(String uri, Object request, Map<
}

public Map<String, Object> fetchResultUsingPatch(String uri, Object request, Map<String, String> headersValues) {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
Map<String, Object> response = null;
try {
HttpHeaders headers = new HttpHeaders();
Expand All @@ -180,21 +185,33 @@ public Map<String, Object> fetchResultUsingPatch(String uri, Object request, Map
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Object> entity = new HttpEntity<>(request, headers);
if (log.isDebugEnabled()) {
StringBuilder str = new StringBuilder(this.getClass().getCanonicalName()).append(".fetchResult")
.append(System.lineSeparator());
str.append("URI: ").append(uri).append(System.lineSeparator());
str.append("Request: ").append(mapper.writeValueAsString(request)).append(System.lineSeparator());
log.debug(str.toString());
logDetails(uri, request);
}
response = restTemplate.patchForObject(uri, entity, Map.class);
if (log.isDebugEnabled()) {
StringBuilder str = new StringBuilder("Response: ");
str.append(mapper.writeValueAsString(response)).append(System.lineSeparator());
log.debug(str.toString());
logDetails(uri, response);
}
} catch (HttpClientErrorException | JsonProcessingException e) {
log.error(e);
} catch (HttpClientErrorException e) {
try {
response = (new ObjectMapper()).readValue(e.getResponseBodyAsString(),
new TypeReference<HashMap<String, Object>>() {
});
} catch (Exception e1) {
}
log.error("Error received: " + e.getResponseBodyAsString(), e);
}
return response;
}

private void logDetails(String uri, Object objectDetails) {
try {
StringBuilder str = new StringBuilder(this.getClass().getCanonicalName()).append(".fetchResult")
.append(System.lineSeparator());
str.append("URI: ").append(uri).append(System.lineSeparator());
str.append("Request/Response: ").append((new ObjectMapper()).writeValueAsString(objectDetails))
.append(System.lineSeparator());
log.debug(str.toString());
} catch (JsonProcessingException je) {
}
}
}
10 changes: 9 additions & 1 deletion src/main/java/org/sunbird/common/util/PropertiesCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public class PropertiesCache {
private final String[] fileName = {
"cassandra.config.properties",
"cassandratablecolumn.properties",
"application.properties"
"application.properties",
"customerror.properties"
};
private final Properties configProp = new Properties();

Expand Down Expand Up @@ -72,4 +73,11 @@ public String readProperty(String key) {
if (StringUtils.isNotBlank(value)) return value;
return configProp.getProperty(key);
}

public String readCustomError(String key) {
if (StringUtils.isNoneBlank(key)) {
key = key.replace(" ", "_");
}
return readProperty(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.sunbird.common.util.Constants;
import org.sunbird.common.util.IndexerService;
import org.sunbird.common.util.ProjectUtil;
import org.sunbird.common.util.PropertiesCache;
import org.sunbird.org.service.ExtendedOrgService;
import org.sunbird.storage.service.StorageServiceImpl;
import org.sunbird.user.service.UserUtilityServiceImpl;
Expand Down Expand Up @@ -156,6 +157,10 @@ public SBApiResponse profileUpdate(Map<String, Object> request, String userToken
} else {
response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR);
response.getParams().setStatus(Constants.FAILED);
String errMsg = (String) ((Map<String, Object>) updateResponse.get(Constants.PARAMS))
.get(Constants.ERROR_MESSAGE);
errMsg = PropertiesCache.getInstance().readCustomError(errMsg);
response.getParams().setErrmsg(errMsg);
return response;
}
}
Expand Down Expand Up @@ -718,7 +723,7 @@ private void getModifiedPersonalDetails(Object personalDetailsObj, Map<String, O
} else if (Constants.SURNAME.equalsIgnoreCase(paramName)) {
updatedRequest.put(Constants.FIRSTNAME, (String) personalDetailsMap.get(paramName));
} else if (Constants.MOBILE.equalsIgnoreCase(paramName)) {
updatedRequest.put(Constants.PHONE, (String) personalDetailsMap.get(paramName));
updatedRequest.put(Constants.PHONE, String.valueOf(personalDetailsMap.get(paramName)));
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/customerror.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
phone_already_exists=Phone number is already registered with another account, please use a different number.

0 comments on commit e7bce21

Please sign in to comment.