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.v3.oas.annotations.media.Schema;
29 import org.oransc.enrichment.exceptions.ServiceException;
30 import org.springframework.http.HttpHeaders;
31 import org.springframework.http.HttpStatus;
32 import org.springframework.http.MediaType;
33 import org.springframework.http.ResponseEntity;
34 import reactor.core.publisher.Mono;
36 public class ErrorResponse {
37 private static Gson gson = new GsonBuilder().create();
39 // Returned as body for all failed REST calls
41 name = "ProblemDetails",
42 description = "A problem detail to carry details in a HTTP response according to RFC 7807")
43 public static class ErrorInfo {
44 @SerializedName("type")
45 private String type = "about:blank";
47 @SerializedName("title")
48 private String title = null;
50 @SerializedName("status")
51 private final Integer status;
53 @SerializedName("detail")
54 private String detail = null;
56 @SerializedName("instance")
57 private String instance = null;
59 public ErrorInfo(String detail, Integer status) {
66 description = "The HTTP status code generated by the origin server for this occurrence of the problem.")
67 public Integer getStatus() {
72 example = "Information Job type not found",
73 description = "A human-readable explanation specific to this occurrence of the problem.")
74 public String getDetail() {
79 @Schema(name = "message")
80 public final String message;
82 ErrorResponse(String message) {
83 this.message = message;
86 public static Mono<ResponseEntity<Object>> createMono(Throwable e, HttpStatus code) {
87 return Mono.just(create(e, code));
90 public static ResponseEntity<Object> create(Throwable e, HttpStatus code) {
91 if (e instanceof RuntimeException) {
92 code = HttpStatus.INTERNAL_SERVER_ERROR;
93 } else if (e instanceof ServiceException) {
94 ServiceException se = (ServiceException) e;
95 if (se.getHttpStatus() != null) {
96 code = se.getHttpStatus();
99 return create(e.toString(), code);
102 public static ResponseEntity<Object> create(String str, HttpStatus code) {
103 ErrorInfo errorInfo = new ErrorInfo(str, code.value());
104 String json = gson.toJson(errorInfo);
105 HttpHeaders headers = new HttpHeaders();
106 headers.setContentType(MediaType.APPLICATION_PROBLEM_JSON);
107 return new ResponseEntity<>(json, headers, code);