00001 /* 00002 * Copyright (C) 2005-2015 Team XBMC 00003 * http://xbmc.org 00004 * 00005 * This Program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2, or (at your option) 00008 * any later version. 00009 * 00010 * This Program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with XBMC Remote; see the file license. If not, write to 00017 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 00018 * http://www.gnu.org/copyleft/gpl.html 00019 * 00020 */ 00021 00022 package org.xbmc.android.jsonrpc.io; 00023 00024 import org.codehaus.jackson.JsonNode; 00025 00032 public class ApiException extends Exception { 00033 00037 public static final int MALFORMED_URL = 0x01; 00041 public static final int IO_EXCEPTION = 0x02; 00045 public static final int IO_EXCEPTION_WHILE_READING = 0x03; 00049 public static final int IO_EXCEPTION_WHILE_WRITING = 0x04; 00053 public static final int IO_EXCEPTION_WHILE_OPENING = 0x05; 00057 public static final int IO_SOCKETTIMEOUT = 0x06; 00061 public static final int IO_UNKNOWN_HOST = 0x07; 00065 public static final int IO_DISCONNECTED = 0x08; 00069 public static final int UNSUPPORTED_ENCODING = 0x09; 00073 public static final int JSON_EXCEPTION = 0x0a; 00077 public static final int RESPONSE_ERROR = 0x0b; 00081 public static final int API_ERROR = 0x0c; 00085 public static final int HTTP_UNKNOWN = 0x0d; 00089 public static final int HTTP_INFO = 0x0e; 00093 public static final int HTTP_SUCCESS = 0x0f; 00097 public static final int HTTP_REDIRECTION = 0x10; 00101 public static final int HTTP_CLIENT_ERROR = 0x11; 00105 public static final int HTTP_SERVER_ERROR = 0x12; 00109 public static final int HTTP_BAD_REQUEST = 0x13; 00113 public static final int HTTP_UNAUTHORIZED = 0x14; 00117 public static final int HTTP_FORBIDDEN = 0x15; 00121 public static final int HTTP_NOT_FOUND = 0x16; 00122 00123 public static final String EXTRA_ERROR_CODE = "org.xbmc.android.jsonprc.extra.ERROR_CODE"; 00124 public static final String EXTRA_ERROR_MESSAGE = "org.xbmc.android.jsonprc.extra.ERROR_MESSAGE"; 00125 public static final String EXTRA_ERROR_HINT = "org.xbmc.android.jsonprc.extra.ERROR_HINT"; 00126 00127 private int code; 00128 00129 public ApiException(int code, String message) { 00130 super(message); 00131 this.code = code; 00132 //Log.e(TAG, message, this); 00133 } 00134 00135 public ApiException(int code, String message, Throwable cause) { 00136 super(message, cause); 00137 this.code = code; 00138 //Log.e(TAG, message, this); 00139 } 00140 00141 public ApiException(JsonNode node) { 00142 super(node.get("error").get("message").getValueAsText()); 00143 final StringBuilder sb = new StringBuilder("API Error: "); 00144 try { 00145 this.code = API_ERROR; 00146 sb.append("Error in "); 00147 sb.append(node.get("error").get("data").get("method").getValueAsText()); 00148 sb.append(" for "); 00149 sb.append(node.get("error").get("data").get("stack").get("name").getValueAsText()); 00150 sb.append(": "); 00151 sb.append(node.get("error").get("data").get("stack").get("property").get("message").getValueAsText()); 00152 } catch(NullPointerException e) { 00153 // this message didn't conform to our expected format 00154 throw new RuntimeException(node.toString()); 00155 } 00156 //Log.e(TAG, sb.toString()); 00157 } 00158 00159 public int getCode() { 00160 return code; 00161 } 00162 00168 public String getDisplayableMessage() { 00169 final String msg = getError(code); 00170 return msg == null ? this.getMessage() : msg; 00171 } 00172 00178 public String getHint() { 00179 return getHint(code); 00180 } 00181 00182 public static String getError(int error) { 00183 switch (error) { 00184 case MALFORMED_URL: 00185 return "error_malformed_url"; 00186 case IO_EXCEPTION: 00187 return "error_io_exception"; 00188 case IO_EXCEPTION_WHILE_READING: 00189 return "error_io_exception_while_reading"; 00190 case IO_EXCEPTION_WHILE_WRITING: 00191 return "error_io_exception_while_writing"; 00192 case IO_EXCEPTION_WHILE_OPENING: 00193 return "error_io_exception_while_connecting"; 00194 case IO_SOCKETTIMEOUT: 00195 return "error_connection_timeout"; 00196 case IO_UNKNOWN_HOST: 00197 return "error_unknown_host"; 00198 case UNSUPPORTED_ENCODING: 00199 return "error_encoding"; 00200 case JSON_EXCEPTION: 00201 return "error_parse_json"; 00202 case RESPONSE_ERROR: 00203 return "error_response"; 00204 case API_ERROR: 00205 return "error_api"; 00206 00207 case HTTP_UNKNOWN: 00208 case HTTP_INFO: 00209 case HTTP_SUCCESS: 00210 case HTTP_REDIRECTION: 00211 case HTTP_CLIENT_ERROR: 00212 case HTTP_SERVER_ERROR: 00213 return "error_http"; 00214 case HTTP_BAD_REQUEST: 00215 return "error_http_400"; 00216 case HTTP_UNAUTHORIZED: 00217 return "error_http_401"; 00218 case HTTP_FORBIDDEN: 00219 return "error_http_403"; 00220 case HTTP_NOT_FOUND: 00221 return "error_http_404"; 00222 } 00223 return null; 00224 } 00225 00226 public static String getHint(int error) { 00227 switch (error) { 00228 case MALFORMED_URL: 00229 return "error_malformed_url_hint"; 00230 case IO_EXCEPTION: 00231 return "error_io_exception_hint"; 00232 case IO_EXCEPTION_WHILE_READING: 00233 return "error_io_exception_while_reading_hint"; 00234 case IO_EXCEPTION_WHILE_WRITING: 00235 return "error_io_exception_while_writing_hint"; 00236 case IO_EXCEPTION_WHILE_OPENING: 00237 return "error_io_exception_while_connecting_hint"; 00238 case IO_SOCKETTIMEOUT: 00239 return "error_connection_timeout_hint"; 00240 case IO_UNKNOWN_HOST: 00241 return "error_unknown_host_hint"; 00242 case UNSUPPORTED_ENCODING: 00243 return "error_encoding_hint"; 00244 case JSON_EXCEPTION: 00245 return "error_parse_json_hint"; 00246 case RESPONSE_ERROR: 00247 return "error_response_hint"; 00248 case API_ERROR: 00249 return "error_api_hint"; 00250 00251 case HTTP_UNKNOWN: 00252 case HTTP_INFO: 00253 case HTTP_SUCCESS: 00254 case HTTP_REDIRECTION: 00255 case HTTP_CLIENT_ERROR: 00256 case HTTP_SERVER_ERROR: 00257 return "error_http_hint"; 00258 case HTTP_BAD_REQUEST: 00259 return "error_http_400_hint"; 00260 case HTTP_UNAUTHORIZED: 00261 return "error_http_401_hint"; 00262 case HTTP_FORBIDDEN: 00263 return "error_http_403_hint"; 00264 case HTTP_NOT_FOUND: 00265 return "error_http_404_hint"; 00266 } 00267 return null; 00268 } 00269 00270 private static final long serialVersionUID = -8668163106123710291L; 00271 }