Skip to content

Commit

Permalink
Ensure messages are non-null
Browse files Browse the repository at this point in the history
Checks
1. error.message()
2. logMessage
3. 'No message'

2nd commit
  • Loading branch information
Kiran Ryali authored and kiransp77 committed Jun 28, 2023
1 parent 5ee62f9 commit 57fc9d8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
Empty file added cherry.txt
Empty file.
19 changes: 16 additions & 3 deletions src/main/java/com/squareup/squash/SquashEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public class SquashEntry {
final String message;
final String log_message;

public SquashEntry(String client, String apiKey, String message, Throwable error,
public SquashEntry(String client, String apiKey, String logMessage, Throwable error,
String appVersion, int versionCode, String buildSha, String deviceId, String endpoint,
String userId, String environment) {
this.client = client;
this.log_message = message;
this.log_message = logMessage;
this.version = appVersion;
this.revision = buildSha;
this.build = "" + versionCode;
Expand All @@ -68,9 +68,22 @@ public SquashEntry(String client, String apiKey, String message, Throwable error
SquashBacktrace.populateNestedExceptions(parent_exceptions, error);
this.ivars = SquashBacktrace.getIvars(error);
this.class_name = error == null ? null : error.getClass().getName();
this.message = error == null ? null : error.getMessage();
this.message = createMessage(error, logMessage);
this.api_key = apiKey;
this.user_id = userId;
this.occurred_at = DATE_FORMAT_THREAD_LOCAL.get().format(new Date());
}

// Squash requires a non-empty message field.
private static String createMessage(Throwable error, String logMessage) {
String message;
if (error != null && error.getMessage() != null) {
message = error.getMessage();
} else if (logMessage != null) {
message = logMessage;
} else {
message = "No message";
}
return message;
}
}
35 changes: 35 additions & 0 deletions src/test/java/com/squareup/squash/SquashEntryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,41 @@ private SquashEntry serializeAndDeserialize(SquashEntry logEntry) throws IOExcep
assertThat(deserialized.class_name).isEqualTo(exception.getClass().getName());
}

@Test public void testExceptionWithNoMessage() throws Exception {
final Throwable exception = mock(Throwable.class);

StackTraceElement s0 = new StackTraceElement("com.jake", "CantProgram",
"core-android/src/com/jake/Brain.java", 50);
StackTraceElement s1 = new StackTraceElement("com.jake", "IsDrunk",
"core-android/src/com/jake/Status.java", 510);
StackTraceElement[] stackTrace = { s0, s1 };

when(exception.getMessage()).thenReturn(null);
when(exception.getStackTrace()).thenReturn(stackTrace);

String logMessage = "Jake can't program";
final SquashEntry logEntry = factory.create(logMessage, exception);
SquashEntry deserialized = serializeAndDeserialize(logEntry);
assertThat(deserialized.message).isEqualTo(logMessage);
}

@Test public void testExceptionWithNoMessageOrLogMessage() throws Exception {
final Throwable exception = mock(Throwable.class);

StackTraceElement s0 = new StackTraceElement("com.jake", "CantProgram",
"core-android/src/com/jake/Brain.java", 50);
StackTraceElement s1 = new StackTraceElement("com.jake", "IsDrunk",
"core-android/src/com/jake/Status.java", 510);
StackTraceElement[] stackTrace = { s0, s1 };

when(exception.getMessage()).thenReturn(null);
when(exception.getStackTrace()).thenReturn(stackTrace);

final SquashEntry logEntry = factory.create(null, exception);
SquashEntry deserialized = serializeAndDeserialize(logEntry);
assertThat(deserialized.message).isEqualTo("No message");
}

private void assertBacktracesMatch(StackTraceElement[] myLittleStackTrace,
List<SquashBacktrace.StackElement> stackElements) {
for (int i = 0, stackElementsSize = stackElements.size(); i < stackElementsSize; i++) {
Expand Down
3 changes: 3 additions & 0 deletions test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
this is our 3rd class on git
git is version control tool

0 comments on commit 57fc9d8

Please sign in to comment.