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 * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
030 */
031public class ExceptionHandler 
032{
033    // Private variables to provide diagnostics on the exception
034    private String message;
035    private String command;
036    private int exitCode;
037    
038    /**
039     * Constructor requires a single APIException to debug
040     * @param ex APIException 
041     */
042    public ExceptionHandler( APIException ex )
043    {
044        
045        // API Exceptions can be nested.  Hence we will need to recurse the 
046        // exception hierarchy to dig for a conclusive message
047        Response response = ex.getResponse();
048
049        // Print the stack trace to standard out for debugging purposes
050        ex.printStackTrace();
051        
052        // The API failed to execute the command (i.e. a real API error)
053        if ( null == response )
054        {            
055            message = ex.getMessage();
056            command = new java.lang.String();
057            //exitCode = Integer.parseInt(ex.getExceptionId());
058            exitCode = -1;
059        }
060        else
061        {
062            command = response.getCommandString();
063            try
064            {
065                exitCode = response.getExitCode();
066            }
067            catch ( InterruptedException ie )
068            {
069                // Just print out the stack trace
070                ie.printStackTrace();
071                exitCode = -1;
072            }
073            WorkItemIterator wit = response.getWorkItems();
074            // In the event there is a problem with one of the command's elements
075            // we have to dig deeper into the exception...
076            try
077            {
078                while ( wit.hasNext() )
079                {
080                    wit.next();
081                }
082                // If we got here then just extract the message
083                if ( ex.getMessage() != null )
084                {
085                    message = ex.getMessage();
086                }
087            }
088            catch ( APIException ae )
089        {
090                // This message will be the real reason for the exception
091                String curMessage = ae.getMessage();
092                if ( curMessage != null )
093                {
094                    message = curMessage;
095                }
096                ae.printStackTrace();
097            }
098        }        
099    }
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}