Skip to content

Commit 9d52f37

Browse files
committed
improved error message for incorrect timestamp format
1 parent 7876268 commit 9d52f37

4 files changed

Lines changed: 22 additions & 7 deletions

File tree

src/main/java/com/teragrep/pth10/ast/DefaultTimeFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public Date parse(String time) {
9797
catch (ParseException ignored) {
9898
}
9999
}
100-
throw new RuntimeException("TimeQualifier conversion error: <" + time + "> can't be parsed.");
100+
throw new RuntimeException("Check that the timestamp or the relative time value is in the correct format (Supported timestamp formats <>)");
101101
}
102102

103103
private Date parseDate(String time, String timeFormat) throws ParseException {

src/main/java/com/teragrep/pth10/ast/time/InstantTimestamp.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
import com.teragrep.pth10.ast.DefaultTimeFormat;
5050
import com.teragrep.pth10.ast.TextString;
5151
import com.teragrep.pth10.ast.UnquotedText;
52+
import org.slf4j.Logger;
53+
import org.slf4j.LoggerFactory;
5254

5355
import java.sql.Timestamp;
5456
import java.text.ParseException;
@@ -57,6 +59,7 @@
5759

5860
public final class InstantTimestamp implements DPLTimestamp {
5961

62+
private static final Logger LOGGER = LoggerFactory.getLogger(EpochTimestamp.class);
6063
private final String value;
6164
private final String timeformat;
6265

@@ -72,6 +75,7 @@ public Instant instant() {
7275
rv = relativeTimestamp.calculate(new Timestamp(System.currentTimeMillis()));
7376
}
7477
catch (NumberFormatException ne) {
78+
LOGGER.debug("Could not parse relative timestamp, trying default formats");
7579
rv = instantFromString(value, timeformat);
7680
}
7781

@@ -80,11 +84,19 @@ public Instant instant() {
8084

8185
// Uses defaultTimeFormat if timeformat is null and DPLTimeFormat if timeformat isn't null (which means that the
8286
// timeformat= option was used).
83-
private Instant instantFromString(final String value, final String timeFormatString) {
87+
private Instant instantFromString(final String value, final String timeFormatString, final NumberFormatException cause) {
8488
final String unquotedValue = new UnquotedText(new TextString(value)).read(); // erase the possible outer quotes
8589
final Instant timevalue;
8690
if (timeFormatString == null || timeFormatString.isEmpty()) {
87-
timevalue = new DefaultTimeFormat().parse(unquotedValue).toInstant();
91+
try {
92+
timevalue = new DefaultTimeFormat().parse(unquotedValue).toInstant();
93+
}
94+
catch (final RuntimeException ex) {
95+
throw new RuntimeException(
96+
"Error parsing <" + unquotedValue + ">. " + ex.getMessage() + ". " + cause.getMessage() + ".",
97+
cause
98+
);
99+
}
88100
}
89101
else {
90102
// TODO: should be included in DPLTimeFormat

src/test/java/com/teragrep/pth10/EarliestLatestTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,10 @@ public void defaultFormatInvalidInputTest() { // MM/dd/yyyy:HH:mm:ss 2013-07-15
358358
.performThrowingDPLTest(RuntimeException.class, query, this.testFile, res -> {
359359
});
360360
Assertions
361-
.assertEquals("TimeQualifier conversion error: <31/31/2014:00:00:00> can't be parsed.", sqe.getMessage());
361+
.assertEquals(
362+
"Error parsing <31/31/2014:00:00:00>. Check that the timestamp or the relative time value is in the correct format (Supported timestamp formats <MM/dd/yyyy:HH:mm:ss>, <yyyy-MM-dd'T'HH:mm:ss>, <yyyy-MM-dd'T'HH:mm:ssXXX>). Unknown relative time modifier string [31/31/2014:00:00:00].",
363+
sqe.getMessage()
364+
);
362365
}
363366

364367
@Test

src/test/java/com/teragrep/pth10/relativeTimeTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ public void parseTimestampLatestRelativeTestWithPlus() {
336336
)
337337
public void parseTimestampLatestRelativeTestWithoutSign() {
338338
String q = "index=cinnamon latest=3h ";
339-
String expected = "TimeQualifier conversion error: <3h> can't be parsed.";
339+
String expected = "Error parsing <3h>. Check that the timestamp or the relative time value is in the correct format (Supported timestamp formats <MM/dd/yyyy:HH:mm:ss>, <yyyy-MM-dd'T'HH:mm:ss>, <yyyy-MM-dd'T'HH:mm:ssXXX>). Unknown relative time modifier string [3h].";
340340

341341
RuntimeException exception = this.streamingTestUtil
342342
.performThrowingDPLTest(RuntimeException.class, q, this.testFile, res -> {
@@ -422,7 +422,7 @@ public void parseTimestampEarliestRelativeSnapToDayLatestNow() {
422422
String expectedLatestString = String.valueOf(expectedLatest).substring(0, 7); // don't check last 2 numbers as the query takes some time and the "now" is different
423423
String regex = "^.*_time >= from_unixtime\\(" + expectedEarliest + ".*_time < from_unixtime\\("
424424
+ expectedLatestString + ".*$";
425-
;
425+
426426
String result = this.streamingTestUtil.getCtx().getSparkQuery();
427427
Assertions.assertTrue(result.matches(regex));
428428
});
@@ -437,7 +437,7 @@ public void parseTimestampEarliestRelativeSnapToDayLatestNow() {
437437
public void parseTimestampRelativeInvalidSnapToTimeUnitTest() {
438438
// pth10 ticket #66 query: 'index=... sourcetype=... earliest=@-5h latest=@-3h'
439439
String query = "index=cinnamon earliest=\"@-5h\" latest=\"@-3h\"";
440-
String expected = "TimeQualifier conversion error: <@-5h> can't be parsed.";
440+
String expected = "Error parsing <@-5h>. Check that the timestamp or the relative time value is in the correct format (Supported timestamp formats <MM/dd/yyyy:HH:mm:ss>, <yyyy-MM-dd'T'HH:mm:ss>, <yyyy-MM-dd'T'HH:mm:ssXXX>). Unknown relative time modifier string [@-5h].";
441441

442442
RuntimeException exception = this.streamingTestUtil
443443
.performThrowingDPLTest(RuntimeException.class, query, this.testFile, res -> {

0 commit comments

Comments
 (0)