View Javadoc
1   /*
2    * Metis: Java Data Framework
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.metis.viewer;
18  
19  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20  import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
21  import io.github.tonywasher.joceanus.metis.field.MetisFieldItem;
22  import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
23  
24  /**
25   * Wrapper class to facilitate reporting of exception.
26   */
27  public class MetisViewerExceptionWrapper
28          implements MetisFieldItem {
29      /**
30       * Local Report fields.
31       */
32      private static final MetisFieldSet<MetisViewerExceptionWrapper> FIELD_DEFS = MetisFieldSet.newFieldSet(MetisViewerExceptionWrapper.class);
33  
34      /*
35       * Declare Fields.
36       */
37      static {
38          FIELD_DEFS.declareLocalField(MetisViewerResource.VIEWER_ERROR_CLASS, MetisViewerExceptionWrapper::getClassName);
39          FIELD_DEFS.declareLocalField(MetisViewerResource.VIEWER_ERROR_MESSAGE, MetisViewerExceptionWrapper::getWrappedMessage);
40          FIELD_DEFS.declareLocalField(MetisViewerResource.VIEWER_ERROR_ORIGIN, MetisViewerExceptionWrapper::getWrappedCause);
41          FIELD_DEFS.declareLocalField(MetisViewerResource.VIEWER_ERROR_CAUSE, MetisViewerExceptionWrapper::getWrappedOrigin);
42          FIELD_DEFS.declareLocalField(MetisViewerResource.VIEWER_ERROR_OBJECT, MetisViewerExceptionWrapper::getWrappedObject);
43          FIELD_DEFS.declareLocalField(MetisViewerResource.VIEWER_ERROR_STACK, MetisViewerExceptionWrapper::getWrappedStack);
44      }
45  
46      /**
47       * The wrapped exception.
48       */
49      private final Throwable theWrapped;
50  
51      /**
52       * The wrapped class.
53       */
54      private final String theClass;
55  
56      /**
57       * Create a new Metis Exception Wrapper for an underlying exception.
58       *
59       * @param e the underlying exception
60       */
61      public MetisViewerExceptionWrapper(final Throwable e) {
62          /* Store details */
63          theWrapped = e;
64          theClass = theWrapped.getClass().getSimpleName();
65      }
66  
67      @Override
68      public MetisFieldSet<MetisViewerExceptionWrapper> getDataFieldSet() {
69          return FIELD_DEFS;
70      }
71  
72      @Override
73      public String formatObject(final OceanusDataFormatter pFormatter) {
74          return getMessage();
75      }
76  
77      /**
78       * Obtain message.
79       *
80       * @return the message
81       */
82      public String getMessage() {
83          return theClass
84                  + ": "
85                  + theWrapped.getMessage();
86      }
87  
88      /**
89       * Obtain the className.
90       *
91       * @return the name
92       */
93      private String getClassName() {
94          return theClass;
95      }
96  
97      /**
98       * Obtain the wrappedCause.
99       *
100      * @return the cause
101      */
102     private Throwable getWrappedCause() {
103         return theWrapped.getCause();
104     }
105 
106     /**
107      * Obtain the wrappedMessage.
108      *
109      * @return the message
110      */
111     private String getWrappedMessage() {
112         return theWrapped.getMessage();
113     }
114 
115     /**
116      * Obtain the wrappedMessage.
117      *
118      * @return the message
119      */
120     private StackTraceElement[] getWrappedStack() {
121         return theWrapped.getStackTrace();
122     }
123 
124     /**
125      * Obtain the wrappedObject.
126      *
127      * @return the object
128      */
129     private Object getWrappedObject() {
130         return theWrapped instanceof OceanusException myExc
131                 ? myExc.getObject()
132                 : null;
133     }
134 
135     /**
136      * Obtain the wrappedOrigin.
137      *
138      * @return the origin
139      */
140     private Throwable getWrappedOrigin() {
141         Throwable myResult = theWrapped;
142         if (myResult.getCause() == null) {
143             return null;
144         }
145         while (myResult.getCause() != null) {
146             myResult = myResult.getCause();
147         }
148         return myResult;
149     }
150 }