changed from string type status code to integer and errorMessage string

This commit is contained in:
Carl Bingel 2020-02-18 21:27:49 +01:00
parent 31da64c986
commit c9d7470993
3 changed files with 55 additions and 30 deletions

View file

@ -41,7 +41,7 @@ public class JsonEventLoopReceiveMessageHandler implements Manager.ReceiveMessag
//ObjectNode result = jsonProcessor.createObjectNode(); //ObjectNode result = jsonProcessor.createObjectNode();
JsonEventLoopStatusReport resp = null; JsonEventLoopStatusReport resp = null;
if (exception != null) { if (exception != null) {
resp = new JsonEventLoopStatusReport( "error", null, "error", "JsonEventLoopReceiveMessageHandler::handleMessage: Exception: " + exception.toString()); resp = new JsonEventLoopStatusReport( "error", null, JsonEventLoopStatusReport.STATUSCODE_GENERIC_ERROR, "JsonEventLoopReceiveMessageHandler::handleMessage: Exception: " + exception.toString());
//result.putPOJO("error", new JsonError(exception)); //result.putPOJO("error", new JsonError(exception));
} }
if (envelope != null) { if (envelope != null) {
@ -49,7 +49,7 @@ public class JsonEventLoopReceiveMessageHandler implements Manager.ReceiveMessag
resp = new JsonEventLoopStatusReport( new JsonMessageEnvelope(envelope, content)); resp = new JsonEventLoopStatusReport( new JsonMessageEnvelope(envelope, content));
} }
if(resp == null) { if(resp == null) {
new JsonEventLoopStatusReport( "error", null, "JsonEventLoopReceiveMessageHandler::handleMessage: both exceptino and envelope is null!").emit(); new JsonEventLoopStatusReport( "error", null, JsonEventLoopStatusReport.STATUSCODE_GENERIC_ERROR, "JsonEventLoopReceiveMessageHandler::handleMessage: both exception and envelope is null!").emit();
} else { } else {
if( this.responseEmitter != null) { if( this.responseEmitter != null) {
this.responseEmitter.emit( resp); this.responseEmitter.emit( resp);

View file

@ -98,9 +98,9 @@ public class JsonEventLoopCommand implements LocalCommand {
try { try {
m.sendMessage( body_text, attachments, recipientNumber); m.sendMessage( body_text, attachments, recipientNumber);
} catch( IOException e) { } catch( IOException e) {
return new JsonEventLoopStatusReport( "send_message", reqID, "error", "IOException: " + e.toString() ); return new JsonEventLoopStatusReport( "send_message", reqID, JsonEventLoopStatusReport.STATUSCODE_SEND_ERROR, "IOException: " + e.toString() );
} catch( EncapsulatedExceptions e) { } catch( EncapsulatedExceptions e) {
return new JsonEventLoopStatusReport( "send_message", reqID, "error", "EncapsulatedException: " + e.toString() ); return new JsonEventLoopStatusReport( "send_message", reqID, JsonEventLoopStatusReport.STATUSCODE_SEND_ERROR, "EncapsulatedException: " + e.toString() );
} }
} else if( reqObj.get("recipientGroupID") != null) { } else if( reqObj.get("recipientGroupID") != null) {
// send message to group // send message to group
@ -109,17 +109,17 @@ public class JsonEventLoopCommand implements LocalCommand {
byte[] recipientGroupID_bytearray = Util.decodeGroupId(recipientGroupID); byte[] recipientGroupID_bytearray = Util.decodeGroupId(recipientGroupID);
m.sendGroupMessage( body_text, attachments, recipientGroupID_bytearray); m.sendGroupMessage( body_text, attachments, recipientGroupID_bytearray);
} catch( GroupIdFormatException e) { } catch( GroupIdFormatException e) {
return new JsonEventLoopStatusReport( "send_message", reqID, "error", "GroupIdFormatException: " + e.toString() ); return new JsonEventLoopStatusReport( "send_message", reqID, JsonEventLoopStatusReport.STATUSCODE_GROUP_ID_FORMAT_ERROR, "GroupIdFormatException: " + e.toString() );
} catch( IOException e) { } catch( IOException e) {
return new JsonEventLoopStatusReport( "send_message", reqID, "error", "IOException: " + e.toString() ); return new JsonEventLoopStatusReport( "send_message", reqID, JsonEventLoopStatusReport.STATUSCODE_SEND_ERROR, "IOException: " + e.toString() );
} catch( EncapsulatedExceptions e) { } catch( EncapsulatedExceptions e) {
return new JsonEventLoopStatusReport( "send_message", reqID, "error", "EncapsulatedException: " + e.toString() ); return new JsonEventLoopStatusReport( "send_message", reqID, JsonEventLoopStatusReport.STATUSCODE_SEND_ERROR, "EncapsulatedException: " + e.toString() );
} }
} else { } else {
return new JsonEventLoopStatusReport( "send_message", reqID, "error", "Neither recipientNumber or recipientGroupID present in request, nothing to do"); return new JsonEventLoopStatusReport( "send_message", reqID, JsonEventLoopStatusReport.STATUSCODE_RECIPIENT_FORMAT_ERROR, "Neither recipientNumber or recipientGroupID present in request, nothing to do");
} }
return new JsonEventLoopStatusReport("send_message", reqID, "ok"); return new JsonEventLoopStatusReport("send_message", reqID, JsonEventLoopStatusReport.STATUSCODE_OK);
} }
/** /**
@ -140,10 +140,10 @@ public class JsonEventLoopCommand implements LocalCommand {
JsonNode reqID = reqObj.get("reqID"); JsonNode reqID = reqObj.get("reqID");
switch(reqType) { switch(reqType) {
case "alive": case "alive":
resp = new JsonEventLoopStatusReport("alive", reqID, "ok"); resp = new JsonEventLoopStatusReport("alive", reqID, JsonEventLoopStatusReport.STATUSCODE_OK);
break; break;
case "exit": case "exit":
resp = new JsonEventLoopStatusReport("exit", reqID, "ok"); resp = new JsonEventLoopStatusReport("exit", reqID, JsonEventLoopStatusReport.STATUSCODE_OK);
//System.exit(0); //System.exit(0);
exitNow = true; exitNow = true;
break; break;
@ -157,20 +157,20 @@ public class JsonEventLoopCommand implements LocalCommand {
resp = this.list_contacts( reqObj, reqID); resp = this.list_contacts( reqObj, reqID);
break; break;
default: default:
resp = new JsonEventLoopStatusReport("error", reqID, "error", "Unknown reqType '" + reqType + "'"); resp = new JsonEventLoopStatusReport("error", reqID, JsonEventLoopStatusReport.STATUSCODE_REQUEST_FORMAT_ERROR, "Unknown reqType '" + reqType + "'");
System.err.println("JsonEvtRequestHandler: ERROR: Unknown reqType '" + reqType + "'"); System.err.println("JsonEvtRequestHandler: ERROR: Unknown reqType '" + reqType + "'");
break; break;
} }
} else { } else {
resp = new JsonEventLoopStatusReport("error", null, "error", "reqType attribute missing"); resp = new JsonEventLoopStatusReport("error", null, JsonEventLoopStatusReport.STATUSCODE_REQUEST_FORMAT_ERROR, "reqType attribute missing");
System.err.println("JsonEvtRequestHandler: ERROR: reqType attribute is missing in request"); System.err.println("JsonEvtRequestHandler: ERROR: reqType attribute is missing in request");
} }
} else { } else {
resp = new JsonEventLoopStatusReport("error", null, "error", "Failed to parse JSON, reqObj is NULL"); resp = new JsonEventLoopStatusReport("error", null, JsonEventLoopStatusReport.STATUSCODE_JSON_PARSE_ERROR, "Failed to parse JSON, reqObj is NULL");
System.err.println( "JsonEvtRequestHandler: Failed to parse JSON, reqObj is NULL"); System.err.println( "JsonEvtRequestHandler: Failed to parse JSON, reqObj is NULL");
} }
} catch( IOException e) { } catch( IOException e) {
resp = new JsonEventLoopStatusReport("error", null, "error", "Failed to parse JSON: " + e.toString()); resp = new JsonEventLoopStatusReport("error", null, JsonEventLoopStatusReport.STATUSCODE_JSON_PARSE_ERROR, "Failed to parse JSON: " + e.toString());
System.err.println( "JsonEvtRequestHandler: Failed to parse JSON, text='" + textRow + "', IOException: " + e.toString()); System.err.println( "JsonEvtRequestHandler: Failed to parse JSON, text='" + textRow + "', IOException: " + e.toString());
} }
@ -218,9 +218,6 @@ public class JsonEventLoopCommand implements LocalCommand {
@Override @Override
public void attachToSubparser(final Subparser subparser) { public void attachToSubparser(final Subparser subparser) {
subparser.addArgument("--ignore-attachments") subparser.addArgument("--ignore-attachments")
@ -232,7 +229,7 @@ public class JsonEventLoopCommand implements LocalCommand {
public int handleCommand(final Namespace ns, final Manager m) { public int handleCommand(final Namespace ns, final Manager m) {
if (!m.isRegistered()) { if (!m.isRegistered()) {
//System.err.println("User is not registered."); //System.err.println("User is not registered.");
new JsonEventLoopStatusReport( "error", null, "error", "JsonEventLoopCommand::handleCommand: User is not registered, aborting"); new JsonEventLoopStatusReport( "error", null, JsonEventLoopStatusReport.STATUSCODE_USER_NOT_REGISTERED_ERROR, "JsonEventLoopCommand::handleCommand: User is not registered, aborting");
return 1; return 1;
} }
@ -242,6 +239,12 @@ public class JsonEventLoopCommand implements LocalCommand {
Thread jsonStdinReaderThread = new Thread( reader); Thread jsonStdinReaderThread = new Thread( reader);
jsonStdinReaderThread.start(); jsonStdinReaderThread.start();
// Emit metadata response message at startup
JsonEventLoopStatusReport meta_resp = new JsonEventLoopStatusReport("metadata", null, JsonEventLoopStatusReport.STATUSCODE_OK);
meta_resp.apiVer = JsonEventLoopStatusReport.CURRENT_APIVER;
meta_resp.attachmentsPath = m.getAttachmentsPath();
meta_resp.emit();
// start JsonReceiveMessageHandler and let it run forever // start JsonReceiveMessageHandler and let it run forever
boolean ignoreAttachments = ns.getBoolean("ignore_attachments"); boolean ignoreAttachments = ns.getBoolean("ignore_attachments");
try { try {
@ -254,7 +257,7 @@ public class JsonEventLoopCommand implements LocalCommand {
return 0; return 0;
} catch (IOException e) { } catch (IOException e) {
System.err.println("Error while receiving messages: " + e.getMessage()); System.err.println("Error while receiving messages: " + e.getMessage());
new JsonEventLoopStatusReport( "error", null, "error", "JsonEventLoopCommand::handleCommand: Error while receiving messages: " + e.getMessage()).emit(); new JsonEventLoopStatusReport( "error", null, JsonEventLoopStatusReport.STATUSCODE_GENERIC_ERROR, "JsonEventLoopCommand::handleCommand: Error while receiving messages: " + e.getMessage()).emit();
return 3; return 3;
} catch (AssertionError e) { } catch (AssertionError e) {
handleAssertionError(e); handleAssertionError(e);

View file

@ -13,15 +13,37 @@ import com.fasterxml.jackson.databind.*;
@JsonInclude(Include.NON_NULL) @JsonInclude(Include.NON_NULL)
public class JsonEventLoopStatusReport { public class JsonEventLoopStatusReport {
public static final int CURRENT_APIVER = 2;
public int apiVer = 2; // statusCode constants
public static final int STATUSCODE_UNKNOWN = -1;
public static final int STATUSCODE_OK = 0;
public static final int STATUSCODE_GENERIC_ERROR = 1;
public static final int STATUSCODE_SEND_ERROR = 2;
public static final int STATUSCODE_GROUP_ID_FORMAT_ERROR = 3;
public static final int STATUSCODE_RECIPIENT_FORMAT_ERROR = 4;
public static final int STATUSCODE_REQUEST_FORMAT_ERROR = 5;
public static final int STATUSCODE_JSON_PARSE_ERROR = 6;
public static final int STATUSCODE_USER_NOT_REGISTERED_ERROR = 7;
// members
@JsonInclude(Include.ALWAYS)
public String respType; public String respType;
@JsonInclude(Include.ALWAYS) @JsonInclude(Include.ALWAYS)
public JsonNode reqID; public JsonNode reqID;
public String status; @JsonInclude(Include.ALWAYS)
public String message; public int statusCode = STATUSCODE_UNKNOWN;
@JsonInclude(Include.NON_NULL)
public String errorMessage;
@JsonInclude(Include.NON_NULL)
public JsonMessageEnvelope envelope; public JsonMessageEnvelope envelope;
@JsonInclude(Include.NON_NULL)
public JsonNode data; public JsonNode data;
@JsonInclude(Include.NON_DEFAULT)
public int apiVer = 0;
@JsonInclude(Include.NON_NULL)
public String attachmentsPath;
/** /**
@ -32,13 +54,13 @@ public class JsonEventLoopStatusReport {
public JsonEventLoopStatusReport( JsonMessageEnvelope en) { public JsonEventLoopStatusReport( JsonMessageEnvelope en) {
this.envelope = en; this.envelope = en;
this.respType = "envelope"; this.respType = "envelope";
this.status = "ok"; this.statusCode = STATUSCODE_OK;
this.reqID = null; this.reqID = null;
} }
public JsonEventLoopStatusReport( String respType, JsonNode reqID, JsonNode data) { public JsonEventLoopStatusReport( String respType, JsonNode reqID, JsonNode data) {
this.respType = respType; this.respType = respType;
this.status = "ok"; this.statusCode = 0;
this.reqID = reqID; this.reqID = reqID;
this.data = data; this.data = data;
} }
@ -50,10 +72,10 @@ public class JsonEventLoopStatusReport {
* @param reqID Request ID object as present in the request * @param reqID Request ID object as present in the request
* @param status Status of the operation, should be "ok" or "error" * @param status Status of the operation, should be "ok" or "error"
*/ */
public JsonEventLoopStatusReport( String respType, JsonNode reqID, String status) { public JsonEventLoopStatusReport( String respType, JsonNode reqID, int statusCode) {
this.respType = respType; this.respType = respType;
this.reqID = reqID; this.reqID = reqID;
this.status = status; this.statusCode = statusCode;
} }
/** /**
@ -64,11 +86,11 @@ public class JsonEventLoopStatusReport {
* @param status Status of the operation, should be "ok" or "error" * @param status Status of the operation, should be "ok" or "error"
* @param message Message explaining what went wrong in case of status="error" * @param message Message explaining what went wrong in case of status="error"
*/ */
public JsonEventLoopStatusReport( String respType, JsonNode reqID, String status, String message) { public JsonEventLoopStatusReport( String respType, JsonNode reqID, int statusCode, String message) {
this.respType = respType; this.respType = respType;
this.reqID = reqID; this.reqID = reqID;
this.status = status; this.statusCode = statusCode;
this.message = message; this.errorMessage = message;
} }
/** /**