Skip to content

Commit

Permalink
Error message validation for GetCertificate API [MOSIP-23028] (#1170)
Browse files Browse the repository at this point in the history
* Error message validation for GetCertificate API [MOSIP-23028]

Signed-off-by: GOKULRAJ136 <110164849+GOKULRAJ136@users.noreply.github.com>

* added proper error code

Signed-off-by: GOKULRAJ136 <110164849+GOKULRAJ136@users.noreply.github.com>

---------

Signed-off-by: GOKULRAJ136 <110164849+GOKULRAJ136@users.noreply.github.com>
  • Loading branch information
GOKULRAJ136 authored Feb 3, 2025
1 parent 434f844 commit fd01f11
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public enum AdminServiceErrorCode {
INVALID_TIMESTAMP_EXCEPTION("KER-ADM-102", "Timestamp cannot be future date"),
LAST_UPDATED_PARSE_EXCEPTION("KER-ADM-103", "Error occurred while parsing lastUpdated timestamp"),
SYNC_JOB_DEF_FETCH_EXCEPTION("KER-ADM-104", "Error while fetching sync job def details"),
DATA_NOT_FOUND_EXCEPTION("KER-ADM-105", "data not found for sync job def");
DATA_NOT_FOUND_EXCEPTION("KER-ADM-105", "data not found for sync job def"),
VALIDATION_ERROR ("KER-ADM-106", "Validation error while fetching Certificate");

private final String errorCode;
private final String errorMessage;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.mosip.kernel.syncdata.exception;

import java.util.List;

import io.mosip.kernel.core.exception.BaseUncheckedException;
import io.mosip.kernel.core.exception.ServiceError;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class SyncInvalidArgumentException extends BaseUncheckedException {

/**
Expand All @@ -20,6 +22,7 @@ public class SyncInvalidArgumentException extends BaseUncheckedException {
* @param list The error list.
*/
public SyncInvalidArgumentException(List<ServiceError> list) {
super(constructMessage(list));
this.list = list;
}

Expand All @@ -32,4 +35,11 @@ public List<ServiceError> getList() {
return list;
}

private static String constructMessage(List<ServiceError> list) {
return list.stream()
.map(ServiceError::getMessage)
.filter(Objects::nonNull)
.collect(Collectors.joining(", "));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.mosip.kernel.core.exception.ExceptionUtils;
import io.mosip.kernel.core.http.RequestWrapper;
import io.mosip.kernel.core.http.ResponseWrapper;
Expand Down Expand Up @@ -56,8 +55,7 @@ public KeyPairGenerateResponseDto getCertificate(String applicationId, Optional<
try {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(certificateUrl);
builder.queryParam("applicationId", applicationId);
if (referenceId.isPresent())
builder.queryParam("referenceId", referenceId.get());
referenceId.ifPresent(id -> builder.queryParam("referenceId", id));
ResponseEntity<String> responseEntity = restTemplate.getForEntity(builder.build().toUri(), String.class);

ResponseWrapper<KeyPairGenerateResponseDto> resp = objectMapper.readValue(responseEntity.getBody(),
Expand All @@ -67,6 +65,9 @@ public KeyPairGenerateResponseDto getCertificate(String applicationId, Optional<
throw new SyncInvalidArgumentException(resp.getErrors());

return resp.getResponse();
} catch (SyncInvalidArgumentException e) {
LOGGER.warn("Validation error while fetching Certificate: {}", e.getMessage());
throw new SyncDataServiceException(AdminServiceErrorCode.VALIDATION_ERROR.getErrorCode(), e.getMessage(), e);
} catch (Exception e) {
LOGGER.error("Failed to fetch Certificate from keymanager", e);
throw new SyncDataServiceException(AdminServiceErrorCode.INTERNAL_SERVER_ERROR.getErrorCode(),
Expand Down

0 comments on commit fd01f11

Please sign in to comment.