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=1df2df7396446a4364b465913587abc91bd483f3;hpb=6f86ab364ac739951556bf2d5bf70429b518de47;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 1df2df73..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; @@ -61,7 +62,7 @@ public class ErrorResponse { } @ApiModelProperty( - example = "503", + example = "404", value = "The HTTP status code generated by the origin server for this occurrence of the problem.") public Integer getStatus() { return status; @@ -73,7 +74,6 @@ public class ErrorResponse { public String getDetail() { return this.detail; } - } @ApiModelProperty(value = "message") @@ -83,24 +83,28 @@ public class ErrorResponse { this.message = message; } - public static Mono> createMono(String text, HttpStatus code) { - return Mono.just(create(text, code)); + public static Mono> createMono(Throwable e, HttpStatus code) { + return Mono.just(create(e, code)); } - public static Mono> createMono(Exception e, HttpStatus code) { - return createMono(e.toString(), 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(); + } + } + return create(e.toString(), code); } - public static ResponseEntity create(String text, HttpStatus code) { - ErrorInfo p = new ErrorInfo(text, code.value()); - String json = gson.toJson(p); + 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); } - public static ResponseEntity create(Exception e, HttpStatus code) { - return create(e.toString(), code); - } - }