Skip to content

Commit

Permalink
Refactor exception handling and update sykemelding responses
Browse files Browse the repository at this point in the history
Replaced custom exceptions with ResponseStatusException for better HTTP response handling. Updated service and controller methods to return SykemeldningResponseDTO for more informative responses. Added SykemeldningResponseDTO class in the DTO library.
  • Loading branch information
krharum committed Nov 26, 2024
1 parent 8d70cf9 commit cab02f9
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import lombok.extern.slf4j.Slf4j;
import no.nav.registre.testnorge.sykemelding.domain.Sykemelding;
import no.nav.testnav.libs.dto.sykemelding.v1.SykemeldningResponseDTO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

Expand All @@ -17,10 +19,16 @@ public SyfoConsumer(JmsTemplate jmsTemplate, @Value("${syfo.queue.name}") String
this.queueName = queueName;
}

public void send(Sykemelding sykemelding) {
String xml = sykemelding.toXml();
public SykemeldningResponseDTO send(Sykemelding sykemelding) {

var xml = sykemelding.toXml();
log.info("Legger sykemelding på kø med MsgId {}\n{}", sykemelding.getMsgId(), sykemelding);
jmsTemplate.send(queueName, session -> session.createTextMessage(xml));
log.trace(xml);

return SykemeldningResponseDTO.builder()
.sykemeldingId(sykemelding.getMsgId())
.status(HttpStatus.OK)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package no.nav.registre.testnorge.sykemelding.provider;

import lombok.RequiredArgsConstructor;
import no.nav.testnav.libs.dto.sykemelding.v1.SykemeldningResponseDTO;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -22,8 +23,8 @@ public class SykemeldingController {
private final ApplicationInfo applicationInfo;

@PostMapping
public ResponseEntity<HttpStatus> create(@RequestBody SykemeldingDTO dto) {
service.send(new Sykemelding(dto, applicationInfo));
return ResponseEntity.ok().build();
public SykemeldningResponseDTO create(@RequestBody SykemeldingDTO dto) {

return service.send(new Sykemelding(dto, applicationInfo));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import lombok.RequiredArgsConstructor;
import no.nav.registre.testnorge.sykemelding.consumer.SyfoConsumer;
import no.nav.registre.testnorge.sykemelding.domain.Sykemelding;
import no.nav.testnav.libs.dto.sykemelding.v1.SykemeldningResponseDTO;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class SykemeldingService {
private final SyfoConsumer syfoConsumer;

public void send(Sykemelding sykemelding) {
public SykemeldningResponseDTO send(Sykemelding sykemelding) {

syfoConsumer.send(sykemelding);
return syfoConsumer.send(sykemelding);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import no.nav.testnav.apps.syntsykemeldingapi.config.Consumers;
import no.nav.testnav.apps.syntsykemeldingapi.consumer.command.GetHelsepersonellCommand;
import no.nav.testnav.apps.syntsykemeldingapi.domain.HelsepersonellListe;
import no.nav.testnav.apps.syntsykemeldingapi.exception.HelsepersonellNotFoundException;
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
import no.nav.testnav.libs.servletsecurity.exchange.TokenExchange;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.server.ResponseStatusException;

import static java.util.Objects.nonNull;

Expand Down Expand Up @@ -44,7 +45,7 @@ public HelsepersonellListe hentHelsepersonell() {
return new HelsepersonellListe(response);
} else {
log.warn("Feil oppsto i henting av helsepersonell");
throw new HelsepersonellNotFoundException("Feil i henting av helsepersonell");
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Feil i henting av helsepersonell");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import no.nav.testnav.apps.syntsykemeldingapi.config.Consumers;
import no.nav.testnav.apps.syntsykemeldingapi.consumer.command.GetPdlPersonCommand;
import no.nav.testnav.apps.syntsykemeldingapi.domain.pdl.PdlPerson;
import no.nav.testnav.apps.syntsykemeldingapi.exception.PdlPersonException;
import no.nav.testnav.apps.syntsykemeldingapi.util.FilLaster;
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
import no.nav.testnav.libs.servletsecurity.exchange.TokenExchange;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.server.ResponseStatusException;

import java.io.BufferedReader;
import java.io.IOException;
Expand Down Expand Up @@ -60,12 +61,12 @@ public PdlPerson getPdlPerson(String ident) {
if (nonNull(response) && !response.getErrors().isEmpty()) {
var melding = response.getErrors().get(0).getMessage();
log.error("Klarte ikke hente pdlperson: " + melding);
throw new PdlPersonException("Feil i henting av person fra pdl" + melding);
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Feil i henting av person fra pdl" + melding);
}
return response;
} catch (Exception e) {
log.error("Klarte ikke hente pdlperson.", e);
throw new PdlPersonException("Feil i henting av person fra pdl");
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Feil i henting av person fra pdl");
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import no.nav.testnav.apps.syntsykemeldingapi.config.Consumers;
import no.nav.testnav.apps.syntsykemeldingapi.consumer.command.PostSykemeldingCommand;
import no.nav.testnav.libs.dto.sykemelding.v1.SykemeldingDTO;
import no.nav.testnav.libs.dto.sykemelding.v1.SykemeldningResponseDTO;
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
import no.nav.testnav.libs.servletsecurity.exchange.TokenExchange;
import org.springframework.stereotype.Component;
Expand All @@ -29,8 +30,10 @@ public SykemeldingConsumer(
.build();
}

public void opprettSykemelding(SykemeldingDTO sykemelding) {
tokenExchange.exchange(serverProperties).flatMap(accessToken ->
public SykemeldningResponseDTO opprettSykemelding(SykemeldingDTO sykemelding) {

return tokenExchange.exchange(serverProperties)
.flatMap(accessToken ->
new PostSykemeldingCommand(webClient, accessToken.getTokenValue(), sykemelding).call())
.block();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import no.nav.testnav.apps.syntsykemeldingapi.config.Consumers;
import no.nav.testnav.apps.syntsykemeldingapi.consumer.command.PostSyntSykemeldingCommand;
import no.nav.testnav.apps.syntsykemeldingapi.consumer.dto.SyntSykemeldingHistorikkDTO;
import no.nav.testnav.apps.syntsykemeldingapi.exception.GenererSykemeldingerException;
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
import no.nav.testnav.libs.servletsecurity.exchange.TokenExchange;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.server.ResponseStatusException;

import java.time.LocalDate;
import java.util.Map;
Expand Down Expand Up @@ -54,7 +55,7 @@ public SyntSykemeldingHistorikkDTO genererSykemeldinger(String ident, LocalDate
.block();

if (isNull(response)) {
throw new GenererSykemeldingerException("Klarte ikke å generere sykemeldinger.");
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Klarte ikke å generere sykemeldinger.");
}
log.info("Sykemelding generert.");
return response.get(ident);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import no.nav.testnav.apps.syntsykemeldingapi.exception.LagreSykemeldingException;
import no.nav.testnav.libs.dto.sykemelding.v1.SykemeldingDTO;
import no.nav.testnav.libs.dto.sykemelding.v1.SykemeldningResponseDTO;
import no.nav.testnav.libs.reactivecore.utils.WebClientFilter;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.server.ResponseStatusException;
import reactor.core.publisher.Mono;

import java.util.concurrent.Callable;
Expand All @@ -14,26 +17,25 @@

@Slf4j
@RequiredArgsConstructor
public class PostSykemeldingCommand implements Callable<Mono<String>> {
public class PostSykemeldingCommand implements Callable<Mono<SykemeldningResponseDTO>> {

private final WebClient webClient;
private final String token;
private final SykemeldingDTO sykemelding;

@SneakyThrows
@Override
public Mono<String> call() {
public Mono<SykemeldningResponseDTO> call() {
return webClient.post()
.uri(builder ->
builder.path("/api/v1/sykemeldinger").build()
)
.header(AUTHORIZATION, "Bearer " + token)
.bodyValue(sykemelding)
.retrieve()
.bodyToMono(String.class)
.onErrorResume(throwable -> {
log.error("Feil oppsto i innsending av sykemelding", throwable);
throw new LagreSykemeldingException("Feil oppsto i innsending av sykemelding");
});
.bodyToMono(SykemeldningResponseDTO.class)
.doOnError(WebClientFilter::logErrorMessage)
.onErrorResume(throwable ->
Mono.error(new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Feil oppsto i innsending av sykemelding")));
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.extern.slf4j.Slf4j;

import no.nav.testnav.apps.syntsykemeldingapi.service.SykemeldingService;
import no.nav.testnav.libs.dto.sykemelding.v1.SykemeldningResponseDTO;
import no.nav.testnav.libs.dto.synt.sykemelding.v1.SyntSykemeldingDTO;

import org.springframework.http.HttpStatus;
Expand All @@ -21,23 +22,11 @@
@RequestMapping("/api/v1/synt-sykemelding")
@RequiredArgsConstructor
public class SyntSykemeldingController {
private final SykemeldingService service;
private final SykemeldingService sykemeldingService;

@PostMapping
public ResponseEntity<HttpStatus> opprett(@RequestBody SyntSykemeldingDTO sykemelding) {
service.opprettSykemelding(sykemelding);
return ResponseEntity.ok().build();
}

@ControllerAdvice
public static class ExceptionHandlerAdvice {

@ExceptionHandler(HttpClientErrorException.class)
public ResponseEntity<String> handleException(HttpClientErrorException e) {
log.error("Klarte ikke å finne arbeidsforhold", e);
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body("Klarte ikke å finne aktivt arbeidsforhold for personen");
}
public SykemeldningResponseDTO opprett(@RequestBody SyntSykemeldingDTO sykemelding) {

return sykemeldingService.opprettSykemelding(sykemelding);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import no.nav.testnav.apps.syntsykemeldingapi.consumer.SyntElsamConsumer;
import no.nav.testnav.apps.syntsykemeldingapi.domain.Person;
import no.nav.testnav.apps.syntsykemeldingapi.domain.Sykemelding;
import no.nav.testnav.libs.dto.sykemelding.v1.SykemeldningResponseDTO;
import no.nav.testnav.libs.dto.synt.sykemelding.v1.SyntSykemeldingDTO;
import org.springframework.stereotype.Service;

Expand All @@ -26,7 +27,7 @@ public class SykemeldingService {
private final ArbeidsforholdAdapter arbeidsforholdAdapter;
private final PdlProxyConsumer pdlProxyConsumer;

public void opprettSykemelding(SyntSykemeldingDTO syntSykemelding) {
public SykemeldningResponseDTO opprettSykemelding(SyntSykemeldingDTO syntSykemelding) {

var pdlData = pdlProxyConsumer.getPdlPerson(syntSykemelding.getIdent());
var pasient = new Person(pdlData);
Expand All @@ -44,7 +45,7 @@ public void opprettSykemelding(SyntSykemeldingDTO syntSykemelding) {
);
var helsepersonellListe = helsepersonellConsumer.hentHelsepersonell();

sykemeldingConsumer.opprettSykemelding(
return sykemeldingConsumer.opprettSykemelding(
new Sykemelding(pasient, historikk, syntSykemelding, helsepersonellListe.getRandomLege(), arbeidsforhold).toDTO()
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package no.nav.testnav.libs.dto.sykemelding.v1;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SykemeldningResponseDTO {

private HttpStatus status;
private String sykemeldingId;
}

0 comments on commit cab02f9

Please sign in to comment.