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() )
080                {
081                    wit.next();
082                }
083                // If we got here then just extract the message
084                if ( ex.getMessage() != null )
085                {
086                    message = ex.getMessage();
087                }
088            }
089            catch ( APIException ae )
090        {
091                // This message will be the real reason for the exception
092                String curMessage = ae.getMessage();
093                if ( curMessage != null )
094                {
095                    message = curMessage;
096                }
097                ae.printStackTrace();
098            }
099        }        
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}