Add multi-layer RIC instance selector
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / model / ErrorTransport.java
index 035b9ef..2a90fca 100644 (file)
@@ -2,7 +2,7 @@
  * ========================LICENSE_START=================================
  * O-RAN-SC
  * %%
- * Copyright (C) 2019 AT&T Intellectual Property and Nokia
+ * Copyright (C) 2019 AT&T Intellectual Property
  * %%
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.oransc.ric.portal.dashboard.model;
 
+import java.time.Instant;
+
 /**
- * Model for message returned on failure, to be serialized as JSON.
+ * This mimics the model Spring-Boot uses for a message returned on failure, to
+ * be serialized as JSON.
  */
 public class ErrorTransport implements IDashboardResponse {
 
+       private Instant timestamp;
        private Integer status;
        private String error;
-       private String exception;
+       private String message;
+       private String path;
 
        /**
         * Builds an empty object.
@@ -37,38 +42,52 @@ public class ErrorTransport implements IDashboardResponse {
        }
 
        /**
-        * Builds an object with the specified values.
+        * Convenience constructor for minimal value set.
+        * 
+        * @param status
+        *                   Integer value like 400
+        * @param error
+        *                   Error message
+        */
+       public ErrorTransport(int status, String error) {
+               this(status, error, null, null);
+       }
+
+       /**
+        * Convenience constructor for populating an error from an exception
         * 
-        * @param statusCode
-        *                       Integer value like 400
-        * @param errMsg
-        *                       Explanation
+        * @param status
+        *                      Integer value like 400
+        * @param throwable
+        *                      The caught exception/throwable to convert to String with
+        *                      an upper bound on characters
         */
-       public ErrorTransport(int statusCode, String errMsg) {
-               this(statusCode, errMsg, null);
+       public ErrorTransport(int status, Throwable throwable) {
+               this.timestamp = Instant.now();
+               this.status = status;
+               final int enough = 256;
+               String exString = throwable.toString();
+               this.error = exString.length() > enough ? exString.substring(0, enough) : exString;
        }
 
        /**
-        * Builds an object with the specified status code, message and a String version
-        * of the exception.
+        * Builds an object with all fields
         * 
-        * @param statusCode
-        *                       Integer value like 500
-        * @param errMsg
-        *                       Explanation
-        * @param exception
-        *                       Exception that should be reported; optional and ignored
-        *                       if null.
+        * @param status
+        *                    Integer value like 500
+        * @param error
+        *                    Explanation
+        * @param message
+        *                    Additional explanation
+        * @param path
+        *                    Requested path
         */
-       public ErrorTransport(int statusCode, String errMsg, Exception exception) {
-               this.status = statusCode;
-               this.error = errMsg;
-               if (exception != null) {
-                       final int enough = 512;
-                       String exString = exception.toString();
-                       String exceptionMsg = exString.length() > enough ? exString.substring(0, enough) : exString;
-                       this.exception = exceptionMsg;
-               }
+       public ErrorTransport(int status, String error, String message, String path) {
+               this.timestamp = Instant.now();
+               this.status = status;
+               this.error = error;
+               this.message = message;
+               this.path = path;
        }
 
        public Integer getStatus() {
@@ -79,6 +98,22 @@ public class ErrorTransport implements IDashboardResponse {
                this.status = status;
        }
 
+       public String getMessage() {
+               return message;
+       }
+
+       public void setMessage(String error) {
+               this.message = error;
+       }
+
+       public Instant getTimestamp() {
+               return timestamp;
+       }
+
+       public void setTimestamp(Instant timestamp) {
+               this.timestamp = timestamp;
+       }
+
        public String getError() {
                return error;
        }
@@ -87,12 +122,18 @@ public class ErrorTransport implements IDashboardResponse {
                this.error = error;
        }
 
-       public String getException() {
-               return exception;
+       public String getPath() {
+               return path;
+       }
+
+       public void setPath(String path) {
+               this.path = path;
        }
 
-       public void setException(String exception) {
-               this.exception = exception;
+       @Override
+       public String toString() {
+               return this.getClass().getSimpleName() + "[timestamp=" + getTimestamp() + ", error=" + getError() + ", path="
+                               + getPath() + "]";
        }
 
 }