Skip to content

Commit

Permalink
added web handler exception
Browse files Browse the repository at this point in the history
  • Loading branch information
CriMDev97 committed Nov 2, 2023
1 parent f17c65e commit 37162a4
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public enum ExceptionTypeEnum {
JWT_EMPTY("JWT_EMPTY", "Il vaucher passato è vuoto"),
JWT_NOT_PRESENT("JWT_NOT_PRESENT", "Il vaucher non è stato passato"),
NO_AUTH_FOUNDED("NO_AUTH_FOUNDED", "Errore interno. Consumatore non trovato"),
GENERIC_ERROR("GENERIC_ERROR", "Si è verificato un errore interno"),

CORRESPONDENCE_NOT_FOUND("CORRESPONDENCE_NOT_FOUND", "Non risulta corrispondenza tra l'erogatore e l'id del servizio: "),
ESERVICE_STATUS_IS_NOT_ACTIVE("ESERVICE_STATUS_IS_NOT_ACTIVE", "Lo stato del servizio è diverso da ACTIVE. EServiceId: "),
SIGNALID_ALREADY_EXISTS("SIGNALID_NOT_EXISTS", "non esiste nessun segnale per il signalId selezionato");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package it.pagopa.interop.signalhub.pull.service.exception;

import com.fasterxml.jackson.databind.ObjectMapper;
import it.pagopa.interop.signalhub.pull.service.rest.v1.dto.Problem;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import reactor.util.annotation.NonNull;


@Slf4j
@Configuration
@AllArgsConstructor
public class PDNDErrorWebExceptionHandler implements ErrorWebExceptionHandler {
private PDNDExceptionHelper helper;
private ObjectMapper objectMapper;

@Override
public Mono<Void> handle(@NonNull ServerWebExchange serverWebExchange, @NonNull Throwable throwable) {
DataBufferFactory bufferFactory = serverWebExchange.getResponse().bufferFactory();
DataBuffer dataBuffer;
log.error("Error Internal : {}", throwable.getMessage(), throwable);
try {
Problem problem = this.helper.handle(throwable);
serverWebExchange.getResponse().setStatusCode(HttpStatus.resolve(problem.getStatus()));
dataBuffer = bufferFactory.wrap(this.objectMapper.writeValueAsBytes(problem));
} catch (Exception ex) {
log.error("cannot output problem", ex);
dataBuffer = bufferFactory.wrap(this.helper.generateFallbackProblem().getBytes());
}
serverWebExchange.getResponse().getHeaders().setContentType(MediaType.APPLICATION_JSON);
return serverWebExchange.getResponse().writeWith(Mono.just(dataBuffer));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package it.pagopa.interop.signalhub.pull.service.exception;



import it.pagopa.interop.signalhub.pull.service.rest.v1.dto.Problem;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.util.UUID;

import static it.pagopa.interop.signalhub.pull.service.exception.ExceptionTypeEnum.GENERIC_ERROR;


@Component
public class PDNDExceptionHelper {



public Problem handle(Throwable ex){
ProblemBuilder builder = ProblemBuilder.builder();

if (ex instanceof PDNDGenericException casted) {
return builder.addExceptionType(casted.getExceptionType())
.addStatusCode(casted.getHttpStatus())
.setMessage(casted.getMessage())
.build();
}

return builder.addExceptionType(GENERIC_ERROR)
.addStatusCode(HttpStatus.INTERNAL_SERVER_ERROR)
.build();

}

public String generateFallbackProblem() {
String fallback = "{\n \"status\": 500,\n \"title\": \"Internal Server Error\",\n \"detail\": \"Cannot output problem\",\n \"traceId\": \"{traceid}\",\n \"timestamp\": \"{timestamp}\",\n \"errors\": [\n {\n \"code\": \"{errorcode}\",\n \"element\": null,\n \"detail\": null\n }\n ]\n}\n";
fallback = fallback.replace("{traceid}", UUID.randomUUID().toString());
fallback = fallback.replace("{timestamp}", Instant.now().toString());
fallback = fallback.replace("{errorcode}", "GENERIC_ERROR");
return fallback;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package it.pagopa.interop.signalhub.pull.service.exception;

import it.pagopa.interop.signalhub.pull.service.rest.v1.dto.Problem;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;

import java.util.UUID;


public class ProblemBuilder {
private final Problem problem;
private ExceptionTypeEnum typeEnum;
private HttpStatus status;
private String customMessage;

private ProblemBuilder(){
this.problem = new Problem();
}


public ProblemBuilder addExceptionType(ExceptionTypeEnum typeEnum){
this.typeEnum = typeEnum;
return this;
}

public ProblemBuilder addStatusCode(HttpStatus status) {
this.status = status;
return this;
}

public ProblemBuilder setMessage(String message){
this.customMessage = message;
return this;
}

public Problem build(){
if (typeEnum == null || status == null) {
throw new IllegalArgumentException("Required arguments not founded");
}
this.problem.setStatus(this.status.value());
this.problem.setTitle(this.typeEnum.getTitle());
this.problem.setDetail(this.typeEnum.getMessage());
if (StringUtils.isNotBlank(this.customMessage)) {
this.problem.setDetail(this.customMessage);
}
this.problem.setCorrelationId(UUID.randomUUID().toString());
return problem;
}

public static ProblemBuilder builder(){
return new ProblemBuilder();
}


}

0 comments on commit 37162a4

Please sign in to comment.