Skip to content

Commit

Permalink
MAT-7233: On non-OK replies from VSAC FHIR Term Serv, parse the Opera…
Browse files Browse the repository at this point in the history
…tionOutcome Resource and return the diagnostic message to the caller.
  • Loading branch information
jkotanchik-SB committed Jun 14, 2024
1 parent 7a283e8 commit 282aa22
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package gov.cms.madie.terminology.controller;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.parser.IParser;
import gov.cms.madie.terminology.exceptions.VsacValueSetExpansionException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hl7.fhir.r4.model.OperationOutcome;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.HttpStatus;
Expand All @@ -27,6 +31,7 @@
public class VsacControllerAdvice {

private final ErrorAttributes errorAttributes;
private final FhirContext fhirContext;

@ExceptionHandler(WebClientResponseException.class)
public ResponseEntity<Map<String, Object>> handleWebClientResponseException(
Expand Down Expand Up @@ -60,6 +65,30 @@ Map<String, Object> onMissingServletRequestParameterException(
return errorAttributes;
}

@ExceptionHandler(VsacValueSetExpansionException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
ResponseEntity<Map<String, Object>> onVsacValueSetExpansionException(
VsacValueSetExpansionException ex, WebRequest request) {
IParser parser = fhirContext.newJsonParser();
OperationOutcome outcome = parser.parseResource(OperationOutcome.class, ex.getBody());
String filter = ex.getFilter();
if (filter.contains("Manifest")) {
filter += " " + ex.getValueSetUrl().substring(ex.getValueSetUrl().lastIndexOf("Library/"));
}
String message =
String.format(
"Value Set %s could not be expanded using %s. Per VSAC, \"%s\"\n\n (DEBUG) URL: %s",
ex.getValueSetUrl()
.substring("ValueSet/".length() + 1, ex.getValueSetUrl().lastIndexOf("/$")),
filter,
outcome.getIssueFirstRep().getDiagnostics(),
ex.getValueSetUrl());
return handleWebClientResponseException(
new WebClientResponseException(
message, ex.getStatusCode(), ex.getStatusText(), null, null, null, null),
request);
}

@ExceptionHandler(VsacGenericException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package gov.cms.madie.terminology.exceptions;

import lombok.Getter;
import lombok.Setter;
import org.springframework.http.HttpStatusCode;
import org.springframework.web.reactive.function.client.WebClientResponseException;

import java.nio.charset.Charset;

@Getter
@Setter
public class VsacValueSetExpansionException extends WebClientResponseException {

private String body;
private String filter;
private String valueSetUrl;

public VsacValueSetExpansionException(
String message,
HttpStatusCode status,
String statusText,
String body,
String filter,
String valueSetUrl) {
super(message, status, statusText, null, body.getBytes(), Charset.defaultCharset(), null);
this.body = body;
this.filter = filter;
this.valueSetUrl = valueSetUrl;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gov.cms.madie.terminology.webclient;

import gov.cms.madie.terminology.exceptions.VsacValueSetExpansionException;
import gov.cms.madie.terminology.models.CodeSystem;
import gov.cms.madie.terminology.util.TerminologyServiceUtil;
import gov.cms.madie.models.measure.ManifestExpansion;
Expand Down Expand Up @@ -124,7 +125,18 @@ private String fetchResourceFromVsac(String uri, String apiKey, String resourceT
return clientResponse.bodyToMono(String.class);
} else {
log.debug("Received NON-OK response while retrieving {}", resourceType);
return clientResponse.createException().flatMap(Mono::error);
return clientResponse
.createException()
.flatMap(
ex ->
Mono.error(
new VsacValueSetExpansionException(
"",
ex.getStatusCode(),
ex.getStatusText(),
ex.getResponseBodyAsString(),
uri.contains("manifest") ? "Manifest" : "Latest",
uri)));
}
})
.block();
Expand Down

0 comments on commit 282aa22

Please sign in to comment.