Skip to content

Commit 7146e42

Browse files
committed
Extract exception details to its own message
1 parent a52dc6c commit 7146e42

File tree

19 files changed

+471
-150
lines changed

19 files changed

+471
-150
lines changed

go/messages.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ type Envelope struct {
3636
UndefinedParameterType *UndefinedParameterType `json:"undefinedParameterType,omitempty"`
3737
}
3838

39+
type Exception struct {
40+
Message string `json:"message,omitempty"`
41+
Type string `json:"type,omitempty"`
42+
}
43+
3944
type GherkinDocument struct {
4045
Uri string `json:"uri,omitempty"`
4146
Feature *Feature `json:"feature,omitempty"`
@@ -329,6 +334,7 @@ type TestRunFinished struct {
329334
Message string `json:"message,omitempty"`
330335
Success bool `json:"success"`
331336
Timestamp *Timestamp `json:"timestamp"`
337+
Exception *Exception `json:"exception,omitempty"`
332338
}
333339

334340
type TestRunStarted struct {
@@ -343,11 +349,10 @@ type TestStepFinished struct {
343349
}
344350

345351
type TestStepResult struct {
346-
Duration *Duration `json:"duration"`
347-
Message string `json:"message,omitempty"`
348-
Status TestStepResultStatus `json:"status"`
349-
ExceptionType string `json:"exceptionType,omitempty"`
350-
ExceptionMessage string `json:"exceptionMessage,omitempty"`
352+
Duration *Duration `json:"duration"`
353+
Message string `json:"message,omitempty"`
354+
Status TestStepResultStatus `json:"status"`
355+
Exception *Exception `json:"exception,omitempty"`
351356
}
352357

353358
type TestStepStarted struct {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package io.cucumber.messages.types;
2+
3+
import java.util.ArrayList;
4+
import java.util.Objects;
5+
import java.util.Optional;
6+
7+
import static java.util.Collections.unmodifiableList;
8+
import static java.util.Objects.requireNonNull;
9+
10+
// Generated code
11+
@SuppressWarnings("unused")
12+
public final class Exception {
13+
private final String message;
14+
private final String type;
15+
16+
public static Exception of(String message) {
17+
return new Exception(
18+
requireNonNull(message, "Exception.message cannot be null"),
19+
null
20+
);
21+
}
22+
23+
public static Exception of(String type) {
24+
return new Exception(
25+
null,
26+
requireNonNull(type, "Exception.type cannot be null")
27+
);
28+
}
29+
30+
public Exception(
31+
String message,
32+
String type
33+
) {
34+
this.message = message;
35+
this.type = type;
36+
}
37+
38+
public Optional<String> getMessage() {
39+
return Optional.ofNullable(message);
40+
}
41+
42+
public Optional<String> getType() {
43+
return Optional.ofNullable(type);
44+
}
45+
46+
@Override
47+
public boolean equals(Object o) {
48+
if (this == o) return true;
49+
if (o == null || getClass() != o.getClass()) return false;
50+
Exception that = (Exception) o;
51+
return
52+
Objects.equals(message, that.message) &&
53+
Objects.equals(type, that.type);
54+
}
55+
56+
@Override
57+
public int hashCode() {
58+
return Objects.hash(
59+
message,
60+
type
61+
);
62+
}
63+
64+
@Override
65+
public String toString() {
66+
return "Exception{" +
67+
"message=" + message +
68+
", type=" + type +
69+
'}';
70+
}
71+
}

java/src/generated/java/io/cucumber/messages/types/TestRunFinished.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@ public final class TestRunFinished {
1313
private final String message;
1414
private final Boolean success;
1515
private final Timestamp timestamp;
16+
private final Exception exception;
1617

1718
public TestRunFinished(
1819
String message,
1920
Boolean success,
20-
Timestamp timestamp
21+
Timestamp timestamp,
22+
Exception exception
2123
) {
2224
this.message = message;
2325
this.success = requireNonNull(success, "TestRunFinished.success cannot be null");
2426
this.timestamp = requireNonNull(timestamp, "TestRunFinished.timestamp cannot be null");
27+
this.exception = exception;
2528
}
2629

2730
public Optional<String> getMessage() {
@@ -36,6 +39,10 @@ public Timestamp getTimestamp() {
3639
return timestamp;
3740
}
3841

42+
public Optional<Exception> getException() {
43+
return Optional.ofNullable(exception);
44+
}
45+
3946
@Override
4047
public boolean equals(Object o) {
4148
if (this == o) return true;
@@ -44,15 +51,17 @@ public boolean equals(Object o) {
4451
return
4552
Objects.equals(message, that.message) &&
4653
success.equals(that.success) &&
47-
timestamp.equals(that.timestamp);
54+
timestamp.equals(that.timestamp) &&
55+
Objects.equals(exception, that.exception);
4856
}
4957

5058
@Override
5159
public int hashCode() {
5260
return Objects.hash(
5361
message,
5462
success,
55-
timestamp
63+
timestamp,
64+
exception
5665
);
5766
}
5867

@@ -62,6 +71,7 @@ public String toString() {
6271
"message=" + message +
6372
", success=" + success +
6473
", timestamp=" + timestamp +
74+
", exception=" + exception +
6575
'}';
6676
}
6777
}

java/src/generated/java/io/cucumber/messages/types/TestStepResult.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,18 @@ public final class TestStepResult {
1313
private final Duration duration;
1414
private final String message;
1515
private final TestStepResultStatus status;
16-
private final String exceptionType;
17-
private final String exceptionMessage;
16+
private final Exception exception;
1817

1918
public TestStepResult(
2019
Duration duration,
2120
String message,
2221
TestStepResultStatus status,
23-
String exceptionType,
24-
String exceptionMessage
22+
Exception exception
2523
) {
2624
this.duration = requireNonNull(duration, "TestStepResult.duration cannot be null");
2725
this.message = message;
2826
this.status = requireNonNull(status, "TestStepResult.status cannot be null");
29-
this.exceptionType = exceptionType;
30-
this.exceptionMessage = exceptionMessage;
27+
this.exception = exception;
3128
}
3229

3330
public Duration getDuration() {
@@ -42,12 +39,8 @@ public TestStepResultStatus getStatus() {
4239
return status;
4340
}
4441

45-
public Optional<String> getExceptionType() {
46-
return Optional.ofNullable(exceptionType);
47-
}
48-
49-
public Optional<String> getExceptionMessage() {
50-
return Optional.ofNullable(exceptionMessage);
42+
public Optional<Exception> getException() {
43+
return Optional.ofNullable(exception);
5144
}
5245

5346
@Override
@@ -59,8 +52,7 @@ public boolean equals(Object o) {
5952
duration.equals(that.duration) &&
6053
Objects.equals(message, that.message) &&
6154
status.equals(that.status) &&
62-
Objects.equals(exceptionType, that.exceptionType) &&
63-
Objects.equals(exceptionMessage, that.exceptionMessage);
55+
Objects.equals(exception, that.exception);
6456
}
6557

6658
@Override
@@ -69,8 +61,7 @@ public int hashCode() {
6961
duration,
7062
message,
7163
status,
72-
exceptionType,
73-
exceptionMessage
64+
exception
7465
);
7566
}
7667

@@ -80,8 +71,7 @@ public String toString() {
8071
"duration=" + duration +
8172
", message=" + message +
8273
", status=" + status +
83-
", exceptionType=" + exceptionType +
84-
", exceptionMessage=" + exceptionMessage +
74+
", exception=" + exception +
8575
'}';
8676
}
8777
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.cucumber.messages;
2+
3+
import io.cucumber.messages.types.Duration;
4+
import io.cucumber.messages.types.Exception;
5+
import io.cucumber.messages.types.Timestamp;
6+
7+
public final class Convertor {
8+
9+
private Convertor(){
10+
11+
}
12+
13+
public static Exception toMessage(Throwable t) {
14+
return new Exception(t.getMessage(), t.getClass().getName());
15+
}
16+
17+
public static Timestamp toMessage(java.time.Instant instant) {
18+
return new Timestamp(instant.getEpochSecond(), (long) instant.getNano());
19+
}
20+
21+
public static Duration toMessage(java.time.Duration duration) {
22+
return new Duration(duration.getSeconds(), (long) duration.getNano());
23+
}
24+
25+
public static java.time.Instant toInstant(Timestamp timestamp) {
26+
return java.time.Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos());
27+
}
28+
29+
public static java.time.Duration toDuration(Duration duration) {
30+
return java.time.Duration.ofSeconds(duration.getSeconds(), duration.getNanos());
31+
}
32+
33+
34+
}

java/src/main/java/io/cucumber/messages/TimeConversion.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@
33
import io.cucumber.messages.types.Duration;
44
import io.cucumber.messages.types.Timestamp;
55

6+
@Deprecated
7+
@SuppressWarnings("unused")
68
public final class TimeConversion {
79

810
private TimeConversion(){
911

1012
}
1113

1214
public static Timestamp javaInstantToTimestamp(java.time.Instant instant) {
13-
return new Timestamp(instant.getEpochSecond(), (long) instant.getNano());
15+
return Convertor.toMessage(instant);
1416
}
1517

1618
public static Duration javaDurationToDuration(java.time.Duration duration) {
17-
return new Duration(duration.getSeconds(), (long) duration.getNano());
19+
return Convertor.toMessage(duration);
1820
}
1921

2022
public static java.time.Instant timestampToJavaInstant(Timestamp timestamp) {
21-
return java.time.Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos());
23+
return Convertor.toInstant(timestamp);
2224
}
2325

2426
public static java.time.Duration durationToJavaDuration(Duration duration) {
25-
return java.time.Duration.ofSeconds(duration.getSeconds(), duration.getNanos());
27+
return Convertor.toDuration(duration);
2628
}
2729
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.cucumber.messages;
2+
3+
import io.cucumber.messages.types.Duration;
4+
import io.cucumber.messages.types.Exception;
5+
import io.cucumber.messages.types.Timestamp;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.util.Optional;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
12+
class ConvertorTest {
13+
14+
@Test
15+
void convertsExceptionToMessage(){
16+
Exception e = Convertor.toMessage(new RuntimeException("Hello world!"));
17+
assertEquals(Optional.of("Hello world!"), e.getMessage());
18+
assertEquals(Optional.of("java.lang.RuntimeException"), e.getType());
19+
}
20+
21+
@Test
22+
void convertsToAndFromTimestamp() {
23+
java.time.Instant javaInstant = java.time.Instant.now();
24+
Timestamp timestamp = Convertor.toMessage(javaInstant);
25+
java.time.Instant javaInstantAgain = Convertor.toInstant(timestamp);
26+
27+
assertEquals(javaInstant, javaInstantAgain);
28+
}
29+
30+
@Test
31+
void convertsToAndFromDuration() {
32+
java.time.Duration javaDuration = java.time.Duration.ofSeconds(3, 161000);
33+
Duration duration = Convertor.toMessage(javaDuration);
34+
java.time.Duration javaDurationAgain = Convertor.toDuration(duration);
35+
36+
assertEquals(javaDuration, javaDurationAgain);
37+
}
38+
39+
}

java/src/test/java/io/cucumber/messages/TimeConversionTest.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)