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 * @version $Id: ExceptionHandler.java 1.2 2011/08/22 13:06:45EDT Cletus D'Souza (dsouza) Exp $
30 * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
31 */
32 public class ExceptionHandler
33 {
34 // Private variables to provide diagnostics on the exception
35 private String message;
36 private String command;
37 private int exitCode;
38
39 /**
40 * Constructor requires a single APIException to debug
41 * @param ex APIException
42 */
43 public ExceptionHandler( APIException ex )
44 {
45
46 // API Exceptions can be nested. Hence we will need to recurse the
47 // exception hierarchy to dig for a conclusive message
48 Response response = ex.getResponse();
49
50 // Print the stack trace to standard out for debugging purposes
51 ex.printStackTrace();
52
53 // The API failed to execute the command (i.e. a real API error)
54 if ( null == response )
55 {
56 message = ex.getMessage();
57 command = new java.lang.String();
58 //exitCode = Integer.parseInt(ex.getExceptionId());
59 exitCode = -1;
60 }
61 else
62 {
63 command = response.getCommandString();
64 try
65 {
66 exitCode = response.getExitCode();
67 }
68 catch ( InterruptedException ie )
69 {
70 // Just print out the stack trace
71 ie.printStackTrace();
72 exitCode = -1;
73 }
74 WorkItemIterator wit = response.getWorkItems();
75 // In the event there is a problem with one of the command's elements
76 // we have to dig deeper into the exception...
77 try
78 {
79 while ( wit.hasNext() )
80 {
81 wit.next();
82 }
83 // If we got here then just extract the message
84 if ( ex.getMessage() != null )
85 {
86 message = ex.getMessage();
87 }
88 }
89 catch ( APIException ae )
90 {
91 // This message will be the real reason for the exception
92 String curMessage = ae.getMessage();
93 if ( curMessage != null )
94 {
95 message = curMessage;
96 }
97 ae.printStackTrace();
98 }
99 }
100 }
101
102 /**
103 * Returns the Message obtained from the APIException
104 * @return message APIException String
105 */
106 public String getMessage()
107 {
108 return message;
109 }
110
111 /**
112 * Returns the executed command that caused the exception
113 * @return command Complete CLI Command String
114 */
115 public String getCommand()
116 {
117 return command;
118 }
119
120 /**
121 * Returns the exit codes associated with executing the command
122 * @return exitCode API/CLI Exit Code
123 */
124 public int getExitCode()
125 {
126 return exitCode;
127 }
128 }