View Javadoc

1   /***
2    *     Ambient - A music player for the Android platform
3    Copyright (C) 2007 Martin Vysny
4    
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9    
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14  
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package sk.baka.ambient.collection.ampache;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  /***
25   * Error communicating with ampache.
26   * 
27   * @author Martin Vysny
28   */
29  public final class AmpacheException extends Exception {
30  	private static final long serialVersionUID = 6318537191144874353L;
31  
32  	private static final Map<String, String> errorMessages = new HashMap<String, String>();
33  
34  	/***
35  	 * The error message.
36  	 */
37  	public final String errorMsg;
38  	
39  	static {
40  		errorMessages.put("501", "ACL DISABLED");
41  		errorMessages.put("401", "HANDSHAKE FAILED");
42  		errorMessages.put("403", "ACCESS DENIED");
43  		errorMessages.put("405", "UNKNOWN METHOD");
44  	}
45  
46  	/***
47  	 * Creates new exception from given error code.
48  	 * 
49  	 * @param errorCode
50  	 *            the code
51  	 * @param errorMsg
52  	 *            error message as sent from the server.
53  	 */
54  	public AmpacheException(final String errorCode, final String errorMsg) {
55  		super(errorCode + " " + getMessage(errorCode) + ": " + errorMsg);
56  		this.errorCode = errorCode;
57  		this.errorMsg = errorMsg;
58  	}
59  
60  	/***
61  	 * The error code.
62  	 */
63  	public final String errorCode;
64  
65  	private static String getMessage(final String errorCode) {
66  		final String result = errorMessages.get(errorCode);
67  		if (result != null) {
68  			return result;
69  		}
70  		return "";
71  	}
72  }