Skip to content

Commit

Permalink
Merge pull request #86 from MeasureAuthoringTool/feature/mat-7233-ret…
Browse files Browse the repository at this point in the history
…urn-fhir-term-error-msgs

[MAT-7233] Bubble up VSAC FHIR Term Service Error Responses
  • Loading branch information
jkotanchik-SB committed Jun 14, 2024
2 parents 7a283e8 + 74bcb2f commit 6bc4cf5
Show file tree
Hide file tree
Showing 4 changed files with 77 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,32 @@ Map<String, Object> onMissingServletRequestParameterException(
return errorAttributes;
}

@ExceptionHandler(VsacValueSetExpansionException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
Map<String, Object> onVsacValueSetExpansionException(
VsacValueSetExpansionException ex, WebRequest request) {
IParser parser = fhirContext.newJsonParser();
OperationOutcome outcome = parser.parseResource(OperationOutcome.class, ex.getBody());

Map<String, Object> errorAttributes =
getErrorAttributes(request, HttpStatus.valueOf(ex.getStatusCode().value()));

errorAttributes.put("diagnostic", outcome.getIssueFirstRep().getDiagnostics());
errorAttributes.put(
"valueSet",
ex.getValueSetUri()
.substring("ValueSet/".length() + 1, ex.getValueSetUri().lastIndexOf("/$")));
if (ex.getFilter().equalsIgnoreCase("manifest") && ex.getValueSetUri().contains("Library/")) {
errorAttributes.put(
"manifest",
ex.getValueSetUri()
.substring(ex.getValueSetUri().lastIndexOf("Library/") + "Library/".length()));
}

return errorAttributes;
}

@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 valueSetUri;

public VsacValueSetExpansionException(
String message,
HttpStatusCode status,
String statusText,
String body,
String filter,
String valueSetUri) {
super(message, status, statusText, null, body.getBytes(), Charset.defaultCharset(), null);
this.body = body;
this.filter = filter;
this.valueSetUri = valueSetUri;
}
}
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gov.cms.madie.terminology.controller;

import ca.uhn.fhir.context.FhirContext;
import gov.cms.madie.models.measure.ManifestExpansion;
import gov.cms.madie.terminology.dto.Code;
import gov.cms.madie.terminology.dto.QdmValueSet;
Expand Down Expand Up @@ -42,6 +43,8 @@ class VsacFhirTerminologyControllerMvcTest {

@MockBean private FhirTerminologyService fhirTerminologyService;

@MockBean private FhirContext fhirContext;

@Autowired private MockMvc mockMvc;

private UmlsUser umlsUser;
Expand Down

0 comments on commit 6bc4cf5

Please sign in to comment.