2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 package org.oransc.enrichment.controllers;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.annotations.SerializedName;
27 import io.swagger.annotations.ApiModel;
28 import io.swagger.annotations.ApiModelProperty;
30 import org.oransc.enrichment.exceptions.ServiceException;
31 import org.springframework.http.HttpHeaders;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.MediaType;
34 import org.springframework.http.ResponseEntity;
35 import reactor.core.publisher.Mono;
37 public class ErrorResponse {
38 private static Gson gson = new GsonBuilder().create();
40 // Returned as body for all failed REST calls
42 value = "ProblemDetails",
43 description = "A problem detail to carry details in a HTTP response according to RFC 7807")
44 public static class ErrorInfo {
45 @SerializedName("type")
46 private String type = "about:blank";
48 @SerializedName("title")
49 private String title = null;
51 @SerializedName("status")
52 private final Integer status;
54 @SerializedName("detail")
55 private String detail = null;
57 @SerializedName("instance")
58 private String instance = null;
60 public ErrorInfo(String detail, Integer status) {
67 value = "The HTTP status code generated by the origin server for this occurrence of the problem.")
68 public Integer getStatus() {
73 example = "EI job type not found",
74 value = "A human-readable explanation specific to this occurrence of the problem.")
75 public String getDetail() {
80 @ApiModelProperty(value = "message")
81 public final String message;
83 ErrorResponse(String message) {
84 this.message = message;
87 public static Mono<ResponseEntity<Object>> createMono(Throwable e, HttpStatus code) {
88 return Mono.just(create(e, code));
91 public static ResponseEntity<Object> create(Throwable e, HttpStatus code) {
92 if (e instanceof RuntimeException) {
93 code = HttpStatus.INTERNAL_SERVER_ERROR;
94 } else if (e instanceof ServiceException) {
95 ServiceException se = (ServiceException) e;
96 if (se.getHttpStatus() != null) {
97 code = se.getHttpStatus();
100 return create(e.toString(), code);
103 public static ResponseEntity<Object> create(String str, HttpStatus code) {
104 ErrorInfo errorInfo = new ErrorInfo(str, code.value());
105 String json = gson.toJson(errorInfo);
106 HttpHeaders headers = new HttpHeaders();
107 headers.setContentType(MediaType.APPLICATION_PROBLEM_JSON);
108 return new ResponseEntity<>(json, headers, code);