aboutsummaryrefslogtreecommitdiffstats
path: root/lib/src/main/java/com/trilead/ssh2/SFTPException.java
blob: f3b6d3eab6b8aaab53c3b5504be540eb77f44fbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.trilead.ssh2;

import java.io.IOException;

import com.trilead.ssh2.sftp.ErrorCodes;


/**
 * Used in combination with the SFTPv3Client. This exception wraps
 * error messages sent by the SFTP server.
 * 
 * @author Christian Plattner, plattner@trilead.com
 * @version $Id: SFTPException.java,v 1.1 2007/10/15 12:49:56 cplattne Exp $
 */

public class SFTPException extends IOException
{
	private static final long serialVersionUID = 578654644222421811L;

	private final String sftpErrorMessage;
	private final int sftpErrorCode;

	private static String constructMessage(String s, int errorCode)
	{
		String[] detail = ErrorCodes.getDescription(errorCode);

		if (detail == null)
			return s + " (UNKNOW SFTP ERROR CODE)";

		return s + " (" + detail[0] + ": " + detail[1] + ")";
	}

	SFTPException(String msg, int errorCode)
	{
		super(constructMessage(msg, errorCode));
		sftpErrorMessage = msg;
		sftpErrorCode = errorCode;
	}

	/**
	 * Get the error message sent by the server. Often, this
	 * message does not help a lot (e.g., "failure").
	 * 
	 * @return the plain string as sent by the server.
	 */
	public String getServerErrorMessage()
	{
		return sftpErrorMessage;
	}

	/**
	 * Get the error code sent by the server.
	 * 
	 * @return an error code as defined in the SFTP specs.
	 */
	public int getServerErrorCode()
	{
		return sftpErrorCode;
	}

	/**
	 * Get the symbolic name of the error code as given in the SFTP specs.
	 * 
	 * @return e.g., "SSH_FX_INVALID_FILENAME".
	 */
	public String getServerErrorCodeSymbol()
	{
		String[] detail = ErrorCodes.getDescription(sftpErrorCode);

		if (detail == null)
			return "UNKNOW SFTP ERROR CODE " + sftpErrorCode;

		return detail[0];
	}

	/**
	 * Get the description of the error code as given in the SFTP specs.
	 * 
	 * @return e.g., "The filename is not valid."
	 */
	public String getServerErrorCodeVerbose()
	{
		String[] detail = ErrorCodes.getDescription(sftpErrorCode);

		if (detail == null)
			return "The error code " + sftpErrorCode + " is unknown.";

		return detail[1];
	}
}