Skip to content

Commit 39c1342

Browse files
authored
Merge pull request #47 from exceptionless/exception-with-message
Added method to submit exception with message
2 parents 2a9d5e0 + 411e116 commit 39c1342

File tree

4 files changed

+53
-30
lines changed

4 files changed

+53
-30
lines changed

samples/example-app/src/main/java/com/exceptionless/example/app/ExampleApp.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,12 @@ public static void sampleEventSubmissions() {
2020

2121
public static void sampleUseOfSessions() {
2222
client.getConfigurationManager().useSessions();
23-
client.submitEvent(
24-
EventPluginContext.from(client.createSessionStart().userIdentity("test-user").build()));
23+
client.submitEvent(client.createSessionStart().userIdentity("test-user").build());
2524
client.submitSessionEnd("test-user");
2625
}
2726

2827
public static void sampleUseOfUpdatingEmailAndDescription() {
29-
client.submitEvent(
30-
EventPluginContext.from(
31-
client.createLog("test-log").referenceId("test-reference-id").build()));
28+
client.submitEvent(client.createLog("test-log").referenceId("test-reference-id").build());
3229
client.updateEmailAndDescription("test-reference-id", "[email protected]", "test-description");
3330
}
3431

src/main/java/com/exceptionless/exceptionlessclient/ExceptionlessClient.java

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,26 @@ public CompletableFuture<Void> submitExceptionAsync(Exception exception) {
9292
}
9393

9494
public void submitException(Exception exception) {
95-
Event event = createException().build();
95+
submitException(null, exception);
96+
}
97+
98+
public CompletableFuture<Void> submitExceptionAsync(String message, Exception exception) {
99+
return CompletableFuture.runAsync(() -> submitException(message, exception), executorService);
100+
}
101+
102+
public void submitException(String message, Exception exception) {
103+
Event event;
104+
if (message == null) {
105+
event = createError().build();
106+
} else {
107+
event = createError().message(message).build();
108+
}
96109
PluginContext pluginContext = PluginContext.builder().exception(exception).build();
97-
submitEvent(EventPluginContext.builder().event(event).context(pluginContext).build());
110+
submitEventWithContext(
111+
EventPluginContext.builder().event(event).context(pluginContext).build());
98112
}
99113

100-
public Event.EventBuilder createException() {
114+
public Event.EventBuilder createError() {
101115
return createEvent().type(EventType.ERROR.value());
102116
}
103117

@@ -108,23 +122,23 @@ public CompletableFuture<Void> submitUnhandledExceptionAsync(
108122
}
109123

110124
public void submitUnhandledException(Exception exception, String submissionMethod) {
111-
Event event = createException().build();
125+
Event event = createError().build();
112126
PluginContext pluginContext =
113127
PluginContext.builder()
114128
.exception(exception)
115129
.unhandledError(true)
116130
.submissionMethod(submissionMethod)
117131
.build();
118-
submitEvent(EventPluginContext.builder().event(event).context(pluginContext).build());
132+
submitEventWithContext(
133+
EventPluginContext.builder().event(event).context(pluginContext).build());
119134
}
120135

121136
public CompletableFuture<Void> submitFeatureUsageAsync(String feature) {
122137
return CompletableFuture.runAsync(() -> submitFeatureUsage(feature), executorService);
123138
}
124139

125140
public void submitFeatureUsage(String feature) {
126-
Event event = createFeatureUsage(feature).build();
127-
submitEvent(EventPluginContext.from(event));
141+
submitEvent(createFeatureUsage(feature).build());
128142
}
129143

130144
public Event.EventBuilder createFeatureUsage(String feature) {
@@ -152,8 +166,7 @@ public CompletableFuture<Void> submitLogAsync(String message, String source, Str
152166
}
153167

154168
public void submitLog(String message, String source, String level) {
155-
Event event = createLog(message, source, level).build();
156-
submitEvent(EventPluginContext.from(event));
169+
submitEvent(createLog(message, source, level).build());
157170
}
158171

159172
public Event.EventBuilder createLog(String message) {
@@ -166,13 +179,7 @@ public Event.EventBuilder createLog(String message, String source) {
166179

167180
public Event.EventBuilder createLog(String message, String source, String level) {
168181
if (source == null) {
169-
// Calling method
170-
StackTraceElement[] traceElements = Thread.currentThread().getStackTrace();
171-
source = traceElements[2].getMethodName();
172-
// Came from the overrided method
173-
if (source.equals("createLog")) {
174-
source = traceElements[3].getMethodName();
175-
}
182+
source = getCallingMethod();
176183
}
177184

178185
Event.EventBuilder builder =
@@ -184,13 +191,20 @@ public Event.EventBuilder createLog(String message, String source, String level)
184191
return builder.property(EventPropertyKey.LOG_LEVEL.value(), level);
185192
}
186193

194+
private String getCallingMethod() {
195+
StackTraceElement[] traceElements = Thread.currentThread().getStackTrace();
196+
String source = traceElements[3].getMethodName();
197+
boolean cameFromOverridenMethod = source.equals("createLog");
198+
199+
return cameFromOverridenMethod ? traceElements[4].getMethodName() : source;
200+
}
201+
187202
public CompletableFuture<Void> submitNotFoundAsync(String resource) {
188203
return CompletableFuture.runAsync(() -> submitNotFound(resource), executorService);
189204
}
190205

191206
public void submitNotFound(String resource) {
192-
Event event = createNotFound(resource).build();
193-
submitEvent(EventPluginContext.from(event));
207+
submitEvent(createNotFound(resource).build());
194208
}
195209

196210
public Event.EventBuilder createNotFound(String resource) {
@@ -202,8 +216,7 @@ public CompletableFuture<Void> submitSessionStartAsync() {
202216
}
203217

204218
public void submitSessionStart() {
205-
Event event = createSessionStart().build();
206-
submitEvent(EventPluginContext.from(event));
219+
submitEvent(createSessionStart().build());
207220
}
208221

209222
public Event.EventBuilder createSessionStart() {
@@ -216,11 +229,21 @@ public Event.EventBuilder createEvent() {
216229
.date(LocalDate.now());
217230
}
218231

219-
public CompletableFuture<Void> submitEventAsync(EventPluginContext eventPluginContext) {
220-
return CompletableFuture.runAsync(() -> submitEvent(eventPluginContext), executorService);
232+
public CompletableFuture<Void> submitEventAsync(Event event) {
233+
return CompletableFuture.runAsync(() -> submitEvent(event), executorService);
234+
}
235+
236+
public void submitEvent(Event event) {
237+
eventPluginRunner.run(EventPluginContext.from(event));
238+
}
239+
240+
public CompletableFuture<Void> submitEventWithContextAsync(
241+
EventPluginContext eventPluginContext) {
242+
return CompletableFuture.runAsync(
243+
() -> submitEventWithContext(eventPluginContext), executorService);
221244
}
222245

223-
public void submitEvent(EventPluginContext eventPluginContext) {
246+
public void submitEventWithContext(EventPluginContext eventPluginContext) {
224247
eventPluginRunner.run(eventPluginContext);
225248
}
226249

src/main/java/com/exceptionless/exceptionlessclient/settings/SettingsManager.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ private boolean shouldNotUpdate(SettingsResponse response) {
9797
return true;
9898
}
9999
if (!response.isSuccess()) {
100-
LOG.warn(String.format("Unable to update settings: %s", response.getBody()));
100+
LOG.warn(
101+
String.format(
102+
"Unable to update settings, body: %s, code: %s",
103+
response.getBody(), response.getCode()));
101104
return true;
102105
}
103106
if (response.getSettings() == null) {

src/test/java/com/exceptionless/exceptionlessclient/ExceptionlessClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void itCanSubmitAnExceptionEvent() {
8686
public void itCanCreateAnExceptionEvent() {
8787
doReturn(settingsStorage).when(storageProvider).getSettings();
8888

89-
Event event = client.createException().build();
89+
Event event = client.createError().build();
9090

9191
assertThat(event.getType()).isEqualTo(EventType.ERROR.value());
9292
assertThat(event.getDate()).isNotNull();

0 commit comments

Comments
 (0)