src/org/ohmage/lib/OhmageApi.java
private void checkFailure(final byte[] response) 
			throws ApiException, RequestErrorException {
		
		JSONObject jsonResponse;
		try {
			jsonResponse = new JSONObject(new String(response));
		}
		catch(JSONException e) {
			// If it isn't JSON, we were mistaken and can abort.
			return;
		}
		
		try {
			// If it is flagged as failed, we need to throw an exception.
			if(Request.RESULT_FAILURE.equals(
					jsonResponse.getString(Request.JSON_KEY_RESULT))) {
				
				// Generate an ohmage-specific error.
				try {
					JSONObject error = jsonResponse.getJSONArray(
							Request.JSON_KEY_ERRORS).getJSONObject(0);
					
					String errorCodeString = 
						error.getString(Annotator.JSON_KEY_CODE);
					ErrorCode errorCode;
					try {
						errorCode = ErrorCode.getValue(errorCodeString);
					}
					catch(IllegalArgumentException e) {
						throw new ApiException("The error code was unknown.", e);
					}
					
					String errorText = error.getString(Annotator.JSON_KEY_TEXT);
					
					throw new RequestErrorException(errorCode, errorText);
				}
				catch(JSONException e) {
					throw new IllegalArgumentException(
							"The failed JSON response doesn't contain a proper error object.",
							e);
				}
			}
		}
		catch(JSONException e) {
			// The result is JSON, but it isn't ohmage JSON, so we can safely
			// abort.
		}
	}
/**
	 * Throws an exception if the response is valid ohmage JSON and indicates
	 * failure.
	 * 
	 * @param response The byte array response returned by the server.
	 * 
	 * @throws IllegalArgumentException Thrown if the response is valid ohmage
	 * 									JSON and indicates failure, but the 
	 * 									error code and error text are missing.
	 * 
	 * @throws ApiException Thrown if the error code is unknown.
	 * 
	 * @throws RequestErrorException Thrown if the response is valid ohmage 
	 * 								 JSON and indicates failure.
	 */
Throws an exception if the response is valid ohmage JSON and indicates
failure.

@param response The byte array response returned by the server.

@throws IllegalArgumentException Thrown if the response is valid ohmage
JSON and indicates failure, but the
error code and error text are missing.

@throws ApiException Thrown if the error code is unknown.

@throws RequestErrorException Thrown if the response is valid ohmage
JSON and indicates failure.
Throws an exception if the response is valid ohmage JSON and indicates failure . @param response The byte array response returned by the server . @throws IllegalArgumentException Thrown if the response is valid ohmage JSON and indicates failure but the error code and error text are missing . @throws ApiException Thrown if the error code is unknown . @throws RequestErrorException Thrown if the response is valid ohmage JSON and indicates failure .
private void checkFailure(final byte[] response) 
			throws ApiException, RequestErrorException {
		JSONObject jsonResponse;
		try {
			jsonResponse = new JSONObject(new String(response));
		}
		catch(JSONException e) {
			return;
		}
		try {
			if(Request.RESULT_FAILURE.equals(
					jsonResponse.getString(Request.JSON_KEY_RESULT))) {
				try {
					JSONObject error = jsonResponse.getJSONArray(
							Request.JSON_KEY_ERRORS).getJSONObject(0);
					String errorCodeString = 
						error.getString(Annotator.JSON_KEY_CODE);
					ErrorCode errorCode;
					try {
						errorCode = ErrorCode.getValue(errorCodeString);
					}
					catch(IllegalArgumentException e) {
						throw new ApiException("The error code was unknown.", e);
					}
					String errorText = error.getString(Annotator.JSON_KEY_TEXT);
					throw new RequestErrorException(errorCode, errorText);
				}
				catch(JSONException e) {
					throw new IllegalArgumentException(
							"The failed JSON response doesn't contain a proper error object.",
							e);
				}
			}
		}
		catch(JSONException e) {
		}
	}
private void checkFailure ( final byte [ ] response ) throws ApiException , RequestErrorException { JSONObject jsonResponse ; try { jsonResponse = new JSONObject ( new String ( response ) ) ; } catch ( JSONException e ) { return ; } try { if ( Request . RESULT_FAILURE . equals ( jsonResponse . getString ( Request . JSON_KEY_RESULT ) ) ) { try { JSONObject error = jsonResponse . getJSONArray ( Request . JSON_KEY_ERRORS ) . getJSONObject ( 0 ) ; String errorCodeString = error . getString ( Annotator . JSON_KEY_CODE ) ; ErrorCode errorCode ; try { errorCode = ErrorCode . getValue ( errorCodeString ) ; } catch ( IllegalArgumentException e ) { throw new ApiException ( "The error code was unknown." , e ) ; } String errorText = error . getString ( Annotator . JSON_KEY_TEXT ) ; throw new RequestErrorException ( errorCode , errorText ) ; } catch ( JSONException e ) { throw new IllegalArgumentException ( "The failed JSON response doesn't contain a proper error object." , e ) ; } } } catch ( JSONException e ) { } }
Throws an exception if the response is valid ohmage JSON and indicates
 failure.
Throws an exception if the response is valid ohmage JSON and indicates failure .
// If it isn't JSON, we were mistaken and can abort. // If it is flagged as failed, we need to throw an exception. // Generate an ohmage-specific error. // The result is JSON, but it isn't ohmage JSON, so we can safely // abort.