001package org.apache.maven.scm.provider.integrity;
002
003/**
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021 
022import com.mks.api.response.APIException;
023import com.mks.api.response.Response;
024import com.mks.api.response.WorkItemIterator;
025import com.mks.api.response.InterruptedException;
026
027/**
028 * Helper class to appropriately diagnose an APIException
029 * @version $Id: ExceptionHandler.java 1.2 2011/08/22 13:06:45EDT Cletus D'Souza (dsouza) Exp  $
030 * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
031 */
032public class ExceptionHandler 
033{
034        // Private variables to provide diagnostics on the exception
035        private String message;
036        private String command;
037        private int exitCode;
038        
039        /**
040         * Constructor requires a single APIException to debug
041         * @param ex APIException 
042         */
043        public ExceptionHandler(APIException ex) 
044        {
045                
046                // API Exceptions can be nested.  Hence we will need to recurse the 
047                // exception hierarchy to dig for a conclusive message
048        Response response = ex.getResponse();
049
050        // Print the stack trace to standard out for debugging purposes
051        ex.printStackTrace();
052        
053                // The API failed to execute the command (i.e. a real API error)
054                if( null == response) 
055                {                       
056                        message = ex.getMessage();
057                        command = new java.lang.String();
058                        //exitCode = Integer.parseInt(ex.getExceptionId());
059                        exitCode = -1;
060                }
061                else
062                {
063                        command = response.getCommandString();
064                        try
065                        {
066                                exitCode = response.getExitCode();
067                        }
068                        catch(InterruptedException ie)
069                        {
070                                // Just print out the stack trace
071                                ie.printStackTrace();
072                                exitCode = -1;
073                        }
074                        WorkItemIterator wit = response.getWorkItems();
075                        // In the event there is a problem with one of the command's elements
076                        // we have to dig deeper into the exception...
077                        try
078                        {
079                                while(wit.hasNext()){ wit.next(); }
080                                // If we got here then just extract the message
081                                if(ex.getMessage() != null) 
082                                {
083                                        message = ex.getMessage();
084                                }                               
085                        }
086                        catch(APIException ae)
087                        {
088                                // This message will be the real reason for the exception
089                                String curMessage = ae.getMessage();
090                                if(curMessage != null)
091                                {
092                                        message = curMessage;
093                                }
094                ae.printStackTrace();
095                        }
096                }               
097        }
098        
099        /**
100         * Returns the Message obtained from the APIException
101         * @return message APIException String
102         */
103        public String getMessage()
104        {
105                return message;
106        }
107        
108        /**
109         * Returns the executed command that caused the exception
110         * @return command Complete CLI Command String
111         */
112        public String getCommand()
113        {
114                return command;
115        }
116        
117        /**
118         * Returns the exit codes associated with executing the command
119         * @return exitCode API/CLI Exit Code
120         */
121        public int getExitCode()
122        {
123                return exitCode;
124        }
125}