Home | History | Annotate | Download | only in ssh2
      1 /*
      2  * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
      3  * Please see the LICENSE.txt for licensing details.
      4  */
      5 package ch.ethz.ssh2;
      6 
      7 import java.io.IOException;
      8 
      9 import ch.ethz.ssh2.sftp.ErrorCodes;
     10 
     11 /**
     12  * Used in combination with the SFTPv3Client. This exception wraps
     13  * error messages sent by the SFTP server.
     14  *
     15  * @author Christian Plattner
     16  * @version 2.50, 03/15/10
     17  */
     18 
     19 public class SFTPException extends IOException
     20 {
     21 	private static final long serialVersionUID = 578654644222421811L;
     22 
     23 	private final String sftpErrorMessage;
     24 	private final int sftpErrorCode;
     25 
     26 	private static String constructMessage(String s, int errorCode)
     27 	{
     28 		String[] detail = ErrorCodes.getDescription(errorCode);
     29 
     30 		if (detail == null)
     31 			return s + " (UNKNOWN SFTP ERROR CODE)";
     32 
     33 		return s + " (" + detail[0] + ": " + detail[1] + ")";
     34 	}
     35 
     36 	SFTPException(String msg, int errorCode)
     37 	{
     38 		super(constructMessage(msg, errorCode));
     39 		sftpErrorMessage = msg;
     40 		sftpErrorCode = errorCode;
     41 	}
     42 
     43 	/**
     44 	 * Get the error message sent by the server. Often, this
     45 	 * message does not help a lot (e.g., "failure").
     46 	 *
     47 	 * @return the plain string as sent by the server.
     48 	 */
     49 	public String getServerErrorMessage()
     50 	{
     51 		return sftpErrorMessage;
     52 	}
     53 
     54 	/**
     55 	 * Get the error code sent by the server.
     56 	 *
     57 	 * @return an error code as defined in the SFTP specs.
     58 	 */
     59 	public int getServerErrorCode()
     60 	{
     61 		return sftpErrorCode;
     62 	}
     63 
     64 	/**
     65 	 * Get the symbolic name of the error code as given in the SFTP specs.
     66 	 *
     67 	 * @return e.g., "SSH_FX_INVALID_FILENAME".
     68 	 */
     69 	public String getServerErrorCodeSymbol()
     70 	{
     71 		String[] detail = ErrorCodes.getDescription(sftpErrorCode);
     72 
     73 		if (detail == null)
     74 			return "UNKNOWN SFTP ERROR CODE " + sftpErrorCode;
     75 
     76 		return detail[0];
     77 	}
     78 
     79 	/**
     80 	 * Get the description of the error code as given in the SFTP specs.
     81 	 *
     82 	 * @return e.g., "The filename is not valid."
     83 	 */
     84 	public String getServerErrorCodeVerbose()
     85 	{
     86 		String[] detail = ErrorCodes.getDescription(sftpErrorCode);
     87 
     88 		if (detail == null)
     89 			return "The error code " + sftpErrorCode + " is unknown.";
     90 
     91 		return detail[1];
     92 	}
     93 }
     94