921b807328bd50fcfe98d473ae29ac71d12d74a7
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / controllers / ErrorResponse.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.oransc.enrichment.controllers;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.annotations.SerializedName;
26
27 import io.swagger.annotations.ApiModel;
28 import io.swagger.annotations.ApiModelProperty;
29
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;
36
37 public class ErrorResponse {
38     private static Gson gson = new GsonBuilder().create();
39
40     // Returned as body for all failed REST calls
41     @ApiModel(
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";
47
48         @SerializedName("title")
49         private String title = null;
50
51         @SerializedName("status")
52         private final Integer status;
53
54         @SerializedName("detail")
55         private String detail = null;
56
57         @SerializedName("instance")
58         private String instance = null;
59
60         public ErrorInfo(String detail, Integer status) {
61             this.detail = detail;
62             this.status = status;
63         }
64
65         @ApiModelProperty(
66             example = "404",
67             value = "The HTTP status code generated by the origin server for this occurrence of the problem.")
68         public Integer getStatus() {
69             return status;
70         }
71
72         @ApiModelProperty(
73             example = "EI job type not found",
74             value = "A human-readable explanation specific to this occurrence of the problem.")
75         public String getDetail() {
76             return this.detail;
77         }
78     }
79
80     @ApiModelProperty(value = "message")
81     public final String message;
82
83     ErrorResponse(String message) {
84         this.message = message;
85     }
86
87     public static Mono<ResponseEntity<Object>> createMono(Throwable e, HttpStatus code) {
88         return Mono.just(create(e, code));
89     }
90
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();
98             }
99         }
100         return create(e.toString(), code);
101     }
102
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);
109     }
110
111 }