-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...java/it/pagopa/interop/signalhub/pull/service/exception/PDNDErrorWebExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/it/pagopa/interop/signalhub/pull/service/exception/PDNDExceptionHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/it/pagopa/interop/signalhub/pull/service/exception/ProblemBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
|
||
} |