148ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood/*
248ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
348ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood * Please see the LICENSE.txt for licensing details.
448ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood */
548ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwoodpackage ch.ethz.ssh2;
648ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood
748ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwoodimport java.io.IOException;
848ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood
948ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwoodimport ch.ethz.ssh2.sftp.ErrorCodes;
1048ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood
1148ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood/**
1248ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood * Used in combination with the SFTPv3Client. This exception wraps
1348ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood * error messages sent by the SFTP server.
1448ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood *
1548ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood * @author Christian Plattner
1648ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood * @version 2.50, 03/15/10
1748ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood */
1848ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood
1948ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwoodpublic class SFTPException extends IOException
2048ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood{
2148ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	private static final long serialVersionUID = 578654644222421811L;
2248ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood
2348ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	private final String sftpErrorMessage;
2448ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	private final int sftpErrorCode;
2548ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood
2648ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	private static String constructMessage(String s, int errorCode)
2748ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	{
2848ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood		String[] detail = ErrorCodes.getDescription(errorCode);
2948ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood
3048ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood		if (detail == null)
3148ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood			return s + " (UNKNOWN SFTP ERROR CODE)";
3248ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood
3348ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood		return s + " (" + detail[0] + ": " + detail[1] + ")";
3448ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	}
3548ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood
3648ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	SFTPException(String msg, int errorCode)
3748ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	{
3848ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood		super(constructMessage(msg, errorCode));
3948ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood		sftpErrorMessage = msg;
4048ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood		sftpErrorCode = errorCode;
4148ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	}
4248ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood
4348ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	/**
4448ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	 * Get the error message sent by the server. Often, this
4548ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	 * message does not help a lot (e.g., "failure").
4648ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	 *
4748ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	 * @return the plain string as sent by the server.
4848ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	 */
4948ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	public String getServerErrorMessage()
5048ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	{
5148ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood		return sftpErrorMessage;
5248ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	}
5348ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood
5448ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	/**
5548ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	 * Get the error code sent by the server.
5648ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	 *
5748ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	 * @return an error code as defined in the SFTP specs.
5848ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	 */
5948ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	public int getServerErrorCode()
6048ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	{
6148ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood		return sftpErrorCode;
6248ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	}
6348ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood
6448ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	/**
6548ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	 * Get the symbolic name of the error code as given in the SFTP specs.
6648ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	 *
6748ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	 * @return e.g., "SSH_FX_INVALID_FILENAME".
6848ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	 */
6948ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	public String getServerErrorCodeSymbol()
7048ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	{
7148ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood		String[] detail = ErrorCodes.getDescription(sftpErrorCode);
7248ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood
7348ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood		if (detail == null)
7448ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood			return "UNKNOWN SFTP ERROR CODE " + sftpErrorCode;
7548ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood
7648ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood		return detail[0];
7748ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	}
7848ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood
7948ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	/**
8048ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	 * Get the description of the error code as given in the SFTP specs.
8148ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	 *
8248ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	 * @return e.g., "The filename is not valid."
8348ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	 */
8448ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	public String getServerErrorCodeVerbose()
8548ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	{
8648ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood		String[] detail = ErrorCodes.getDescription(sftpErrorCode);
8748ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood
8848ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood		if (detail == null)
8948ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood			return "The error code " + sftpErrorCode + " is unknown.";
9048ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood
9148ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood		return detail[1];
9248ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood	}
9348ded2421114c4c87ef3f8005c9f793a5d077cbdMike Lockwood}
94