2 * ========================LICENSE_START=================================
5 * Copyright (C) 2023 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.oran.pmproducer.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 java.lang.invoke.MethodHandles;
31 import org.oran.pmproducer.exceptions.ServiceException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.http.HttpHeaders;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.MediaType;
37 import org.springframework.http.ResponseEntity;
38 import reactor.core.publisher.Mono;
40 public class ErrorResponse {
41 private static Gson gson = new GsonBuilder() //
42 .disableHtmlEscaping() //
45 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
47 // Returned as body for all failed REST calls
48 @Schema(name = "error_information", description = "Problem as defined in https://tools.ietf.org/html/rfc7807")
49 public static class ErrorInfo {
50 @SerializedName("type")
51 private String type = "about:blank";
53 @SerializedName("title")
54 private String title = null;
56 @SerializedName("status")
57 private final Integer status;
59 @SerializedName("detail")
60 private String detail = null;
62 @SerializedName("instance")
63 private String instance = null;
65 public ErrorInfo(String detail, Integer status) {
70 @Schema(example = "503",
71 description = "The HTTP status code generated by the origin server for this occurrence of the problem. ")
72 public Integer getStatus() {
76 @Schema(example = "Policy type not found",
77 description = " A human-readable explanation specific to this occurrence of the problem.")
78 public String getDetail() {
84 @Schema(name = "message", description = "message")
85 public final String message;
87 ErrorResponse(String message) {
88 this.message = message;
91 static Mono<ResponseEntity<Object>> createMono(String text, HttpStatus code) {
92 return Mono.just(create(text, code));
95 static Mono<ResponseEntity<Object>> createMono(Exception e, HttpStatus code) {
96 return createMono(e.toString(), code);
99 public static ResponseEntity<Object> create(String text, HttpStatus code) {
100 logger.debug("Error response: {}, {}", code, text);
101 ErrorInfo p = new ErrorInfo(text, code.value());
102 String json = gson.toJson(p);
103 HttpHeaders headers = new HttpHeaders();
104 headers.setContentType(MediaType.APPLICATION_PROBLEM_JSON);
105 return new ResponseEntity<>(json, headers, code);
108 public static ResponseEntity<Object> create(Throwable e, HttpStatus code) {
109 if (e instanceof RuntimeException) {
110 code = HttpStatus.INTERNAL_SERVER_ERROR;
111 } else if (e instanceof ServiceException se && se.getHttpStatus() != null) {
112 code = se.getHttpStatus();
114 return create(e.toString(), code);