TwitterApiException extends TwitterException { static ApiError parseApiError(String body) { final Gson gson = new GsonBuilder() .registerTypeAdapterFactory(new SafeListAdapter()) .registerTypeAdapterFactory(new SafeMapAdapter()) .create(); try { final ApiErrors apiErrors = gson.fromJson(body, ApiErrors.class); if (!apiErrors.errors.isEmpty()) { return apiErrors.errors.get(0); } } catch (JsonSyntaxException e) { Twitter.getLogger().e(TwitterCore.TAG, "Invalid json: " + body, e); } return null; } TwitterApiException(Response response);   TwitterApiException(Response response, ApiError apiError, TwitterRateLimit twitterRateLimit,
            int code); int getStatusCode(); int getErrorCode(); String getErrorMessage(); TwitterRateLimit getTwitterRateLimit(); Response getResponse(); static TwitterRateLimit readApiRateLimit(Response response); static ApiError readApiError(Response response); static final int DEFAULT_ERROR_CODE; }
@Test public void testParseErrorCode() throws IOException { final ApiError apiError = TwitterApiException.parseApiError(API_ERROR_JSON); assertEquals(API_ERROR_CODE, apiError.code); assertEquals(API_ERROR_MESSAGE, apiError.message); }
@Test public void testParseError_nonJSON() throws Exception { assertNull(TwitterApiException.parseApiError(API_ERROR_NON_JSON)); }
@Test public void testParseError_noErrorCode() throws Exception { final ApiError apiError = TwitterApiException.parseApiError(API_ERROR_NO_ERROR_CODE); assertEquals(DEFAULT_ERROR_CODE, apiError.code); assertEquals(API_ERROR_MESSAGE, apiError.message); }
@Test public void testParseError_noErrors() throws Exception { final ApiError apiError = TwitterApiException.parseApiError(API_ERROR_NO_ERRORS); assertNull(apiError); }
@Test public void testParseError_noMessage() throws Exception { final ApiError apiError = TwitterApiException.parseApiError(API_ERROR_NO_ERROR_MESSAGE); assertEquals(API_ERROR_CODE, apiError.code); assertEquals(null, apiError.message); }