2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2020 Nordix Foundation. All rights reserved.
6 * ======================================================================
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.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() //
40 // Returned as body for all failed REST calls
41 @ApiModel(value = "error_information", description = "Problem as defined in https://tools.ietf.org/html/rfc7807")
42 public static class ErrorInfo {
43 @SerializedName("type")
44 private String type = "about:blank";
46 @SerializedName("title")
47 private String title = null;
49 @SerializedName("status")
50 private final Integer status;
52 @SerializedName("detail")
53 private String detail = null;
55 @SerializedName("instance")
56 private String instance = null;
58 public ErrorInfo(String detail, Integer status) {
65 value = "The HTTP status code generated by the origin server for this occurrence of the problem.")
66 public Integer getStatus() {
71 example = "EI job type not found",
72 value = "A human-readable explanation specific to this occurrence of the problem.")
73 public String getDetail() {
78 @ApiModelProperty(value = "message")
79 public final String message;
81 ErrorResponse(String message) {
82 this.message = message;
85 public static Mono<ResponseEntity<Object>> createMono(Exception e, HttpStatus code) {
86 return Mono.just(create(e, code));
89 public static ResponseEntity<Object> create(Exception e, HttpStatus code) {
90 if (e instanceof RuntimeException) {
91 code = HttpStatus.INTERNAL_SERVER_ERROR;
93 ErrorInfo p = new ErrorInfo(e.toString(), code.value());
94 String json = gson.toJson(p);
95 HttpHeaders headers = new HttpHeaders();
96 headers.setContentType(MediaType.APPLICATION_PROBLEM_JSON);
97 return new ResponseEntity<>(json, headers, code);