View Javadoc
1   package org.apache.maven.scm.provider.integrity;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  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,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21   
22  import com.mks.api.response.APIException;
23  import com.mks.api.response.Response;
24  import com.mks.api.response.WorkItemIterator;
25  import com.mks.api.response.InterruptedException;
26  
27  /**
28   * Helper class to appropriately diagnose an APIException
29   * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
30   */
31  public class ExceptionHandler 
32  {
33      // Private variables to provide diagnostics on the exception
34      private String message;
35      private String command;
36      private int exitCode;
37      
38      /**
39       * Constructor requires a single APIException to debug
40       * @param ex APIException 
41       */
42      public ExceptionHandler( APIException ex )
43      {
44          
45          // API Exceptions can be nested.  Hence we will need to recurse the 
46          // exception hierarchy to dig for a conclusive message
47          Response response = ex.getResponse();
48  
49          // Print the stack trace to standard out for debugging purposes
50          ex.printStackTrace();
51          
52          // The API failed to execute the command (i.e. a real API error)
53          if ( null == response )
54          {            
55              message = ex.getMessage();
56              command = new java.lang.String();
57              //exitCode = Integer.parseInt(ex.getExceptionId());
58              exitCode = -1;
59          }
60          else
61          {
62              command = response.getCommandString();
63              try
64              {
65                  exitCode = response.getExitCode();
66              }
67              catch ( InterruptedException ie )
68              {
69                  // Just print out the stack trace
70                  ie.printStackTrace();
71                  exitCode = -1;
72              }
73              WorkItemIterator wit = response.getWorkItems();
74              // In the event there is a problem with one of the command's elements
75              // we have to dig deeper into the exception...
76              try
77              {
78                  while ( wit.hasNext() )
79                  {
80                      wit.next();
81                  }
82                  // If we got here then just extract the message
83                  if ( ex.getMessage() != null )
84                  {
85                      message = ex.getMessage();
86                  }
87              }
88              catch ( APIException ae )
89          {
90                  // This message will be the real reason for the exception
91                  String curMessage = ae.getMessage();
92                  if ( curMessage != null )
93                  {
94                      message = curMessage;
95                  }
96                  ae.printStackTrace();
97              }
98          }        
99      }
100     
101     /**
102      * Returns the Message obtained from the APIException
103      * @return message APIException String
104      */
105     public String getMessage()
106     {
107         return message;
108     }
109     
110     /**
111      * Returns the executed command that caused the exception
112      * @return command Complete CLI Command String
113      */
114     public String getCommand()
115     {
116         return command;
117     }
118     
119     /**
120      * Returns the exit codes associated with executing the command
121      * @return exitCode API/CLI Exit Code
122      */
123     public int getExitCode()
124     {
125         return exitCode;
126     }
127 }