efe2884440cec173d8129318908b5e7c239372d9
[portal/nonrtric-controlpanel.git] / webapp-backend / src / main / java / org / oransc / portal / nonrtric / controlpanel / model / ErrorTransport.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
6  * Modifications Copyright (C) 2020 Nordix Foundation
7  * %%
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ========================LICENSE_END===================================
20  */
21
22 package org.oransc.portal.nonrtric.controlpanel.model;
23
24 import java.time.Instant;
25
26 /**
27  * This mimics the model Spring-Boot uses for a message returned on failure, to
28  * be serialized as JSON.
29  */
30 public class ErrorTransport implements IControlpanelResponse {
31
32     private Instant timestamp;
33     private Integer status;
34     private String error;
35     private String message;
36     private String path;
37
38     /**
39      * Builds an empty object.
40      */
41     public ErrorTransport() {
42         // no-arg constructor
43     }
44
45     /**
46      * Convenience constructor for minimal value set.
47      *
48      * @param status
49      *        Integer value like 400
50      * @param error
51      *        Error message
52      */
53     public ErrorTransport(int status, String error) {
54         this(status, error, null, null);
55     }
56
57     /**
58      * Convenience constructor for populating an error from an exception
59      *
60      * @param status
61      *        Integer value like 400
62      * @param throwable
63      *        The caught exception/throwable to convert to String with
64      *        an upper bound on characters
65      */
66     public ErrorTransport(int status, Throwable throwable) {
67         this.timestamp = Instant.now();
68         this.status = status;
69         final int enough = 256;
70         String exString = throwable.toString();
71         this.error = exString.length() > enough ? exString.substring(0, enough) : exString;
72     }
73
74     /**
75      * Builds an object with all fields
76      *
77      * @param status
78      *        Integer value like 500
79      * @param error
80      *        Explanation
81      * @param message
82      *        Additional explanation
83      * @param path
84      *        Requested path
85      */
86     public ErrorTransport(int status, String error, String message, String path) {
87         this.timestamp = Instant.now();
88         this.status = status;
89         this.error = error;
90         this.message = message;
91         this.path = path;
92     }
93
94     public Integer getStatus() {
95         return status;
96     }
97
98     public void setStatus(Integer status) {
99         this.status = status;
100     }
101
102     public String getMessage() {
103         return message;
104     }
105
106     public void setMessage(String error) {
107         this.message = error;
108     }
109
110     public Instant getTimestamp() {
111         return timestamp;
112     }
113
114     public void setTimestamp(Instant timestamp) {
115         this.timestamp = timestamp;
116     }
117
118     public String getError() {
119         return error;
120     }
121
122     public void setError(String error) {
123         this.error = error;
124     }
125
126     public String getPath() {
127         return path;
128     }
129
130     public void setPath(String path) {
131         this.path = path;
132     }
133
134 }