Skip to content

Commit de53083

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

File tree

19 files changed

+470
-150
lines changed

19 files changed

+470
-150
lines changed

go/messages.go

+10-5
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+
Type string `json:"type"`
41+
Message string `json:"message,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 {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 type;
14+
private final String message;
15+
16+
public Exception(
17+
String type,
18+
String message
19+
) {
20+
this.type = requireNonNull(type, "Exception.type cannot be null");
21+
this.message = message;
22+
}
23+
24+
public String getType() {
25+
return type;
26+
}
27+
28+
public Optional<String> getMessage() {
29+
return Optional.ofNullable(message);
30+
}
31+
32+
@Override
33+
public boolean equals(Object o) {
34+
if (this == o) return true;
35+
if (o == null || getClass() != o.getClass()) return false;
36+
Exception that = (Exception) o;
37+
return
38+
type.equals(that.type) &&
39+
Objects.equals(message, that.message);
40+
}
41+
42+
@Override
43+
public int hashCode() {
44+
return Objects.hash(
45+
type,
46+
message
47+
);
48+
}
49+
50+
@Override
51+
public String toString() {
52+
return "Exception{" +
53+
"type=" + type +
54+
", message=" + message +
55+
'}';
56+
}
57+
}

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

+13-3
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

+8-18
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
}
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.getClass().getName(), t.getMessage());
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

+6-4
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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.assertAll;
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
class ConvertorTest {
14+
15+
@Test
16+
void convertsExceptionToMessage() {
17+
Exception e = Convertor.toMessage(new RuntimeException("Hello world!"));
18+
Exception e2 = Convertor.toMessage(new RuntimeException());
19+
assertAll(
20+
() -> assertEquals(Optional.of("Hello world!"), e.getMessage()),
21+
() -> assertEquals(Optional.empty(), e2.getMessage()),
22+
() -> assertEquals("java.lang.RuntimeException", e.getType()),
23+
() -> assertEquals("java.lang.RuntimeException", e2.getType())
24+
);
25+
}
26+
27+
@Test
28+
void convertsToAndFromTimestamp() {
29+
java.time.Instant javaInstant = java.time.Instant.now();
30+
Timestamp timestamp = Convertor.toMessage(javaInstant);
31+
java.time.Instant javaInstantAgain = Convertor.toInstant(timestamp);
32+
33+
assertEquals(javaInstant, javaInstantAgain);
34+
}
35+
36+
@Test
37+
void convertsToAndFromDuration() {
38+
java.time.Duration javaDuration = java.time.Duration.ofSeconds(3, 161000);
39+
Duration duration = Convertor.toMessage(javaDuration);
40+
java.time.Duration javaDurationAgain = Convertor.toDuration(duration);
41+
42+
assertEquals(javaDuration, javaDurationAgain);
43+
}
44+
45+
}

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

-33
This file was deleted.

0 commit comments

Comments
 (0)