Revise controller error handling
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / model / ErrorTransport.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property and Nokia
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.ric.portal.dashboard.model;
22
23 import java.time.Instant;
24
25 /**
26  * This mimics the model Spring-Boot uses for a message returned on failure, to
27  * be serialized as JSON.
28  */
29 public class ErrorTransport implements IDashboardResponse {
30
31         private Instant timestamp;
32         private Integer status;
33         private String error;
34         private String message;
35         private String path;
36
37         /**
38          * Builds an empty object.
39          */
40         public ErrorTransport() {
41                 // no-arg constructor
42         }
43
44         /**
45          * Convenience constructor for minimal value set.
46          * 
47          * @param status
48          *                   Integer value like 400
49          * @param error
50          *                   Error message
51          */
52         public ErrorTransport(int status, String error) {
53                 this(status, error, null, null);
54         }
55
56         /**
57          * Convenience constructor for populating an error from an exception
58          * 
59          * @param status
60          *                      Integer value like 400
61          * @param throwable
62          *                      The caught exception/throwable to convert to String with
63          *                      an upper bound on characters
64          */
65         public ErrorTransport(int status, Throwable throwable) {
66                 this.timestamp = Instant.now();
67                 this.status = status;
68                 final int enough = 256;
69                 String exString = throwable.toString();
70                 this.error = exString.length() > enough ? exString.substring(0, enough) : exString;
71         }
72
73         /**
74          * Builds an object with all fields
75          * 
76          * @param status
77          *                    Integer value like 500
78          * @param error
79          *                    Explanation
80          * @param message
81          *                    Additional explanation
82          * @param path
83          *                    Requested path
84          */
85         public ErrorTransport(int status, String error, String message, String path) {
86                 this.timestamp = Instant.now();
87                 this.status = status;
88                 this.error = error;
89                 this.message = message;
90                 this.path = path;
91         }
92
93         public Integer getStatus() {
94                 return status;
95         }
96
97         public void setStatus(Integer status) {
98                 this.status = status;
99         }
100
101         public String getMessage() {
102                 return message;
103         }
104
105         public void setMessage(String error) {
106                 this.message = error;
107         }
108
109         public Instant getTimestamp() {
110                 return timestamp;
111         }
112
113         public void setTimestamp(Instant timestamp) {
114                 this.timestamp = timestamp;
115         }
116
117         public String getError() {
118                 return error;
119         }
120
121         public void setError(String error) {
122                 this.error = error;
123         }
124
125         public String getPath() {
126                 return path;
127         }
128
129         public void setPath(String path) {
130                 this.path = path;
131         }
132
133 }