X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=enrichment-coordinator-service%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fenrichment%2Fcontrollers%2FErrorResponse.java;h=4e68c1a95113ad84e81324b76c921a96e56ed7b7;hb=6a1eb6e2a6538decc54f5348fcb1589f5b829e68;hp=c90a649173215c8900ee3283b02c3c29ee5b5f57;hpb=57b7c521ca02753b12e995ec02f3a84dddd147ce;p=nonrtric.git diff --git a/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/controllers/ErrorResponse.java b/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/controllers/ErrorResponse.java index c90a6491..4e68c1a9 100644 --- a/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/controllers/ErrorResponse.java +++ b/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/controllers/ErrorResponse.java @@ -27,6 +27,7 @@ import com.google.gson.annotations.SerializedName; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; +import org.oransc.enrichment.exceptions.ServiceException; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -82,16 +83,25 @@ public class ErrorResponse { this.message = message; } - public static Mono> createMono(Exception e, HttpStatus code) { + public static Mono> createMono(Throwable e, HttpStatus code) { return Mono.just(create(e, code)); } - public static ResponseEntity create(Exception e, HttpStatus code) { + public static ResponseEntity create(Throwable e, HttpStatus code) { if (e instanceof RuntimeException) { code = HttpStatus.INTERNAL_SERVER_ERROR; + } else if (e instanceof ServiceException) { + ServiceException se = (ServiceException) e; + if (se.getHttpStatus() != null) { + code = se.getHttpStatus(); + } } - ErrorInfo p = new ErrorInfo(e.toString(), code.value()); - String json = gson.toJson(p); + return create(e.toString(), code); + } + + public static ResponseEntity create(String str, HttpStatus code) { + ErrorInfo errorInfo = new ErrorInfo(str, code.value()); + String json = gson.toJson(errorInfo); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_PROBLEM_JSON); return new ResponseEntity<>(json, headers, code);