src/org/ohmage/jee/servlet/RequestServlet.java
protected void respondFailure( 
			final HttpServletResponse httpResponse,
			final int httpErrorCode, 
			final ErrorCode errorCode,
			final String errorMessage) {

		if (httpErrorCode <= 0)
			httpResponse.setStatus(200);	
		httpResponse.setStatus(httpErrorCode);
		
		String errorCodeString;
		if (errorCode == null)
			errorCodeString = ErrorCode.SYSTEM_GENERAL_ERROR.toString();
		errorCodeString = errorCode.toString();	
		
		if(errorMessage != null) {
			try {
				// try to send error in the json format
				Writer writer = httpResponse.getWriter();
				httpResponse.setContentType("application/json");
				String result;
				try {
					JSONObject resultJson = new JSONObject();
					resultJson.put(Request.JSON_KEY_RESULT, Request.RESULT_FAILURE);
					JSONArray jsonArray = new JSONArray();
					// add an error code
					jsonArray.put(new JSONObject().put(errorCodeString, errorMessage)); 
					resultJson.put(Request.JSON_KEY_ERRORS, jsonArray);
					result = resultJson.toString();
				} catch(JSONException je) {
					LOGGER.error("An error occurred while building the failure JSON response.", je);
					result = Request.RESPONSE_ERROR_JSON_TEXT;
				}
				writer.write(result);
				writer.flush();
				writer.close();
			}
			catch(IOException errorResponding) {
				LOGGER.error("Could not respond with an error.", 
					errorResponding);
			}
		}
	}
/**
	 * Send a failed message with the error code. 
	 * 
	 * @param httpResponse The HTTP response that will be sent back to the user
	 * 					   once the request has been processed.
	 * @param httpErrorCode The HTTP error code
	 * @param errorCode the ohmage error code
	 * @param errorMessage the error message that we want to include
	 */
Send a failed message with the error code.

@param httpResponse The HTTP response that will be sent back to the user
once the request has been processed.
@param httpErrorCode The HTTP error code
@param errorCode the ohmage error code
@param errorMessage the error message that we want to include
Send a failed message with the error code . @param httpResponse The HTTP response that will be sent back to the user once the request has been processed . @param httpErrorCode The HTTP error code @param errorCode the ohmage error code @param errorMessage the error message that we want to include
protected void respondFailure( 
			final HttpServletResponse httpResponse,
			final int httpErrorCode, 
			final ErrorCode errorCode,
			final String errorMessage) {
		if (httpErrorCode <= 0)
			httpResponse.setStatus(200);	
		httpResponse.setStatus(httpErrorCode);
		String errorCodeString;
		if (errorCode == null)
			errorCodeString = ErrorCode.SYSTEM_GENERAL_ERROR.toString();
		errorCodeString = errorCode.toString();	
		if(errorMessage != null) {
			try {
				Writer writer = httpResponse.getWriter();
				httpResponse.setContentType("application/json");
				String result;
				try {
					JSONObject resultJson = new JSONObject();
					resultJson.put(Request.JSON_KEY_RESULT, Request.RESULT_FAILURE);
					JSONArray jsonArray = new JSONArray();
					jsonArray.put(new JSONObject().put(errorCodeString, errorMessage)); 
					resultJson.put(Request.JSON_KEY_ERRORS, jsonArray);
					result = resultJson.toString();
				} catch(JSONException je) {
					LOGGER.error("An error occurred while building the failure JSON response.", je);
					result = Request.RESPONSE_ERROR_JSON_TEXT;
				}
				writer.write(result);
				writer.flush();
				writer.close();
			}
			catch(IOException errorResponding) {
				LOGGER.error("Could not respond with an error.", 
					errorResponding);
			}
		}
	}
protected void respondFailure ( final HttpServletResponse httpResponse , final int httpErrorCode , final ErrorCode errorCode , final String errorMessage ) { if ( httpErrorCode <= 0 ) httpResponse . setStatus ( 200 ) ; httpResponse . setStatus ( httpErrorCode ) ; String errorCodeString ; if ( errorCode == null ) errorCodeString = ErrorCode . SYSTEM_GENERAL_ERROR . toString ( ) ; errorCodeString = errorCode . toString ( ) ; if ( errorMessage != null ) { try { Writer writer = httpResponse . getWriter ( ) ; httpResponse . setContentType ( "application/json" ) ; String result ; try { JSONObject resultJson = new JSONObject ( ) ; resultJson . put ( Request . JSON_KEY_RESULT , Request . RESULT_FAILURE ) ; JSONArray jsonArray = new JSONArray ( ) ; jsonArray . put ( new JSONObject ( ) . put ( errorCodeString , errorMessage ) ) ; resultJson . put ( Request . JSON_KEY_ERRORS , jsonArray ) ; result = resultJson . toString ( ) ; } catch ( JSONException je ) { LOGGER . error ( "An error occurred while building the failure JSON response." , je ) ; result = Request . RESPONSE_ERROR_JSON_TEXT ; } writer . write ( result ) ; writer . flush ( ) ; writer . close ( ) ; } catch ( IOException errorResponding ) { LOGGER . error ( "Could not respond with an error." , errorResponding ) ; } } }
Send a failed message with the error code.
Send a failed message with the error code .
// try to send error in the json format // add an error code