Skip to content

Commit 032b571

Browse files
authored
fix: log intent response data when external app fails (#410)
1 parent aeda3bf commit 032b571

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed

src/main/java/org/medicmobile/webapp/mobile/ChtExternalApp.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ public Intent createIntent() {
8585

8686
//> PRIVATE
8787

88+
private Serializable getSerializableValue(Object value) {
89+
// ODK does not have boolean data type
90+
if (value instanceof String strValue && ("true".equals(strValue) || "false".equals(strValue))) {
91+
return Boolean.parseBoolean(strValue);
92+
}
93+
return (Serializable) value;
94+
}
95+
8896
private void setIntentExtras(Intent intent, String key, JSONObject data) {
8997
try {
9098
Object value = data.get(key);
@@ -99,7 +107,7 @@ private void setIntentExtras(Intent intent, String key, JSONObject data) {
99107
return;
100108
}
101109

102-
intent.putExtra(key, (Serializable) value);
110+
intent.putExtra(key, getSerializableValue(value));
103111

104112
} catch (Exception exception) {
105113
error(exception, "ChtExternalApp :: Problem setting intent extras. Key=%s, Data=%s", key, data);

src/main/java/org/medicmobile/webapp/mobile/ChtExternalAppHandler.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,21 @@ public class ChtExternalAppHandler {
2727
}
2828

2929
String processResult(int resultCode, Intent intent) {
30-
if (resultCode != RESULT_OK) {
31-
String message = "ChtExternalAppHandler :: Bad result code: %s. The external app either: " +
32-
"explicitly returned this result, didn't return any result or crashed during the operation.";
33-
34-
warn(this, message, resultCode);
35-
return safeFormat("console.error('" + message + "')", resultCode);
36-
}
37-
3830
try {
3931
Optional<JSONObject> json = new ChtExternalApp
4032
.Response(intent, this.context)
4133
.getData();
4234
String data = json.map(JSONObject::toString).orElse(null);
35+
36+
if (resultCode != RESULT_OK) {
37+
String message = "ChtExternalAppHandler :: Bad result code: %s. The external app either: " +
38+
"explicitly returned this result, did not return any result or crashed during the operation. " +
39+
"[" + data + "]";
40+
41+
warn(this, message, resultCode);
42+
return safeFormat("console.error('" + message + "')", resultCode);
43+
}
44+
4345
return makeJavaScript(data);
4446

4547
} catch (Exception exception) {

src/test/java/org/medicmobile/webapp/mobile/ChtExternalAppHandlerTest.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,30 @@ public void processResult_withBadResultCode_logError() {
135135
Intent intent = mock(Intent.class);
136136
ChtExternalAppHandler chtExternalAppHandler = new ChtExternalAppHandler(contextMock);
137137
String expectedMessageWarn = "ChtExternalAppHandler :: Bad result code: %s. The external app either: " +
138-
"explicitly returned this result, didn't return any result or crashed during the operation.";
138+
"explicitly returned this result, did not return any result or crashed during the operation. [null]";
139139
String expectedMessageConsole = "ChtExternalAppHandler :: Bad result code: " + RESULT_CANCELED + ". The external app either: " +
140-
"explicitly returned this result, didn't return any result or crashed during the operation.";
140+
"explicitly returned this result, did not return any result or crashed during the operation. [null]";
141+
142+
//> WHEN
143+
String script = chtExternalAppHandler.processResult(RESULT_CANCELED, intent);
144+
145+
//> THEN
146+
assertEquals("console.error('" + expectedMessageConsole + "')", script);
147+
medicLogMock.verify(() -> MedicLog.warn(eq(chtExternalAppHandler), eq(expectedMessageWarn), eq(RESULT_CANCELED)));
148+
}
149+
}
150+
151+
@Test
152+
public void processResult_withBadResultCodeAndData_logError() {
153+
try (MockedStatic<MedicLog> medicLogMock = mockStatic(MedicLog.class)) {
154+
//> GIVEN
155+
Intent intent = new Intent();
156+
intent.putExtra("name", "Eric");
157+
ChtExternalAppHandler chtExternalAppHandler = new ChtExternalAppHandler(contextMock);
158+
String expectedMessageWarn = "ChtExternalAppHandler :: Bad result code: %s. The external app either: " +
159+
"explicitly returned this result, did not return any result or crashed during the operation. [{\"name\":\"Eric\"}]";
160+
String expectedMessageConsole = "ChtExternalAppHandler :: Bad result code: " + RESULT_CANCELED + ". The external app either: " +
161+
"explicitly returned this result, did not return any result or crashed during the operation. [{\"name\":\"Eric\"}]";
141162

142163
//> WHEN
143164
String script = chtExternalAppHandler.processResult(RESULT_CANCELED, intent);

src/test/java/org/medicmobile/webapp/mobile/ChtExternalAppTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void createIntent_withSimpleData_setExtrasCorrectly() throws JSONExceptio
126126
"\"an.int\": 5," +
127127
"\"a.long\": 2147483649," +
128128
"\"a.double\": 2.8," +
129-
"\"a.boolean\": true," +
129+
"\"a.boolean\": \"true\"," +
130130
"\"a.string\": \"some text\"" +
131131
"}";
132132
String arraysJson = "{" +

0 commit comments

Comments
 (0)