Skip to content

Commit 266aa3e

Browse files
authored
java: include stacktrace in Convertor.toMessage(Throwable) (#213)
1 parent c64e85e commit 266aa3e

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
9+
### Added
10+
- java: include stacktrace in Convertor.toMessage(Throwable) ([#213](https://github.com/cucumber/messages/pull/213))
911

1012
## [24.0.1] - 2023-12-21
1113
### Fixed

java/pom.xml

+8
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@
8787
</resource>
8888
</resources>
8989
<plugins>
90+
<plugin>
91+
<groupId>org.apache.maven.plugins</groupId>
92+
<artifactId>maven-resources-plugin</artifactId>
93+
<configuration>
94+
<propertiesEncoding>UTF-8</propertiesEncoding>
95+
</configuration>
96+
</plugin>
97+
9098
<plugin>
9199
<groupId>org.codehaus.mojo</groupId>
92100
<artifactId>build-helper-maven-plugin</artifactId>

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

+20-2
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,47 @@
44
import io.cucumber.messages.types.Exception;
55
import io.cucumber.messages.types.Timestamp;
66

7+
import java.io.PrintWriter;
8+
import java.io.StringWriter;
9+
10+
import static java.util.Objects.requireNonNull;
11+
712
public final class Convertor {
813

914
private Convertor(){
1015

1116
}
1217

13-
public static Exception toMessage(Throwable t) {
14-
return new Exception(t.getClass().getName(), t.getMessage(), null);
18+
public static Exception toMessage(Throwable throwable) {
19+
requireNonNull(throwable, "throwable may not be null");
20+
return new Exception(throwable.getClass().getName(), throwable.getMessage(), extractStackTrace(throwable));
21+
}
22+
23+
private static String extractStackTrace(Throwable throwable) {
24+
StringWriter stringWriter = new StringWriter();
25+
try (PrintWriter printWriter = new PrintWriter(stringWriter)) {
26+
throwable.printStackTrace(printWriter);
27+
}
28+
return stringWriter.toString();
1529
}
1630

1731
public static Timestamp toMessage(java.time.Instant instant) {
32+
requireNonNull(instant, "instant may not be null");
1833
return new Timestamp(instant.getEpochSecond(), (long) instant.getNano());
1934
}
2035

2136
public static Duration toMessage(java.time.Duration duration) {
37+
requireNonNull(duration, "duration may not be null");
2238
return new Duration(duration.getSeconds(), (long) duration.getNano());
2339
}
2440

2541
public static java.time.Instant toInstant(Timestamp timestamp) {
42+
requireNonNull(timestamp, "timestamp may not be null");
2643
return java.time.Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos());
2744
}
2845

2946
public static java.time.Duration toDuration(Duration duration) {
47+
requireNonNull(duration, "duration may not be null");
3048
return java.time.Duration.ofSeconds(duration.getSeconds(), duration.getNanos());
3149
}
3250

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

+12-3
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,27 @@
99

1010
import static org.junit.jupiter.api.Assertions.assertAll;
1111
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static org.junit.jupiter.api.Assertions.assertTrue;
1213

1314
class ConvertorTest {
1415

1516
@Test
1617
void convertsExceptionToMessage() {
18+
Exception e = Convertor.toMessage(new RuntimeException());
19+
assertAll(
20+
() -> assertEquals(Optional.empty(), e.getMessage()),
21+
() -> assertEquals("java.lang.RuntimeException", e.getType()),
22+
() -> assertTrue(e.getStackTrace().isPresent())
23+
);
24+
}
25+
26+
@Test
27+
void convertsExceptionWithMessageToMessage() {
1728
Exception e = Convertor.toMessage(new RuntimeException("Hello world!"));
18-
Exception e2 = Convertor.toMessage(new RuntimeException());
1929
assertAll(
2030
() -> assertEquals(Optional.of("Hello world!"), e.getMessage()),
21-
() -> assertEquals(Optional.empty(), e2.getMessage()),
2231
() -> assertEquals("java.lang.RuntimeException", e.getType()),
23-
() -> assertEquals("java.lang.RuntimeException", e2.getType())
32+
() -> assertTrue(e.getStackTrace().isPresent())
2433
);
2534
}
2635

0 commit comments

Comments
 (0)