Skip to content

Commit

Permalink
Refactor handling of error messages and JSON responses
Browse files Browse the repository at this point in the history
#deploy-organisasjon-forvalter

Updated `OrganisasjonServiceConsumer` to correctly map organization numbers. Introduced `convertFeilmelding` in `OrdreStatusService` to streamline error processing and handle JSON parsing exceptions. Modified `OrganisasjonController` to specify JSON response type and adjusted data type for error in `OrdreResponse`.
  • Loading branch information
krharum committed Sep 12, 2024
1 parent 1ddb0c1 commit 2dd178d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public Flux<Map<String, OrganisasjonDTO>> getStatus(Set<String> orgnummere, Set<

return tokenExchange.exchange(serverProperties)
.flatMapMany(token -> Flux.fromIterable(miljoer)
.map(orgnr -> Flux.fromIterable(orgnummere)
.flatMap(miljoe -> new OrganisasjonServiceCommand(webClient, orgnr, miljoe, token.getTokenValue()).call())
.collect(Collectors.toMap(orgMap -> orgnr, orgMap -> orgMap))))
.map(miljoe -> Flux.fromIterable(orgnummere)
.flatMap(orgnr -> new OrganisasjonServiceCommand(webClient, orgnr, miljoe, token.getTokenValue()).call())
.collect(Collectors.toMap(orgMap -> miljoe, orgMap -> orgMap))))
.flatMap(Mono::flux);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import no.nav.organisasjonforvalter.service.OrganisasjonService;
import no.nav.testnav.libs.servletsecurity.action.GetAuthenticatedId;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -34,7 +35,7 @@
import static no.nav.organisasjonforvalter.config.CacheConfig.CACHE_BEDRIFT;

@RestController
@RequestMapping("api/v2/organisasjoner")
@RequestMapping(value = "api/v2/organisasjoner", produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class OrganisasjonController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public static class StatusEnv {
private String environment;
private StatusDTO.Status status;
private String details;
private JsonNode error;
private Object error;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,23 @@ public OrdreResponse getStatus(List<String> orgnumre) {
.collect(Collectors.groupingBy(BestillingStatus::getOrgnummer))
.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, org -> org.getValue().stream()
.map(value -> {
try {
return StatusEnv.builder()
.status(nonNull(value.getStatus()) ? value.getStatus().getStatus() : ERROR)
.details(nonNull(value.getStatus()) ? value.getStatus().getDescription() : "Se feilbeskrivelse")
.environment(value.getMiljoe())
.error(nonNull(value.getFeilmelding()) ?
objectMapper.readValue(value.getFeilmelding(), JsonNode.class) :
null)
.build();
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
})
.map(value -> StatusEnv.builder()
.status(nonNull(value.getStatus()) ? value.getStatus().getStatus() : ERROR)
.details(nonNull(value.getStatus()) ? value.getStatus().getDescription() : "Se feilbeskrivelse")
.environment(value.getMiljoe())
.error(convertFeilmelding(value.getFeilmelding()))
.build())
.toList())))
.build();
}

private Object convertFeilmelding(String feilmelding) {

try {
return isNotBlank(feilmelding) ? objectMapper.readValue(feilmelding, JsonNode.class) : null;

} catch (JsonProcessingException e) {
return feilmelding;
}
}
}

0 comments on commit 2dd178d

Please sign in to comment.