Skip to content

Commit

Permalink
Make parser return throw an exception
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Mar 3, 2024
1 parent 84083b6 commit 10fac09
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
31 changes: 20 additions & 11 deletions src/main/java/org/opentripplanner/apis/gtfs/GraphQLScalars.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import graphql.schema.CoercingParseValueException;
import graphql.schema.CoercingSerializeException;
import graphql.schema.GraphQLScalarType;
import java.text.ParseException;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -79,26 +80,34 @@ public String serialize(@Nonnull Object dataFetcherResult)
@Override
public OffsetDateTime parseValue(Object input) throws CoercingParseValueException {
if (input instanceof CharSequence cs) {
return OffsetDateTimeParser.parseLeniently(cs).orElseThrow(() -> valueException(input));
try {
return OffsetDateTimeParser.parseLeniently(cs);
} catch (ParseException e) {
int errorOffset = e.getErrorOffset();
throw new CoercingParseValueException(
"Cannot parse %s into an OffsetDateTime. Error at character index %s".formatted(
input,
errorOffset
)
);
}
}
throw valueException(input);
throw new CoercingParseValueException(
"Cannot parse %s into an OffsetDateTime. Must be a string."
);
}

@Override
public OffsetDateTime parseLiteral(Object input) throws CoercingParseLiteralException {
if (input instanceof StringValue sv) {
return OffsetDateTimeParser
.parseLeniently(sv.getValue())
.orElseThrow(CoercingParseLiteralException::new);
try {
return OffsetDateTimeParser.parseLeniently(sv.getValue());
} catch (ParseException e) {
throw new CoercingSerializeException();
}
}
throw new CoercingParseLiteralException();
}

private static CoercingParseValueException valueException(Object input) {
return new CoercingParseValueException(
"Cannot parse %s into an OffsetDateTime.".formatted(input)
);
}
}
)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.opentripplanner.framework.time;

import java.text.ParseException;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.util.Optional;

public class OffsetDateTimeParser {

Expand All @@ -30,13 +30,13 @@ public class OffsetDateTimeParser {
/**
* Parses a ISO-8601 string into am OffsetDateTime instance allowing the offset to be both in
* '02:00' and '0200' format.
* @throws ParseException if the string cannot be parsed
*/
public static Optional<OffsetDateTime> parseLeniently(CharSequence input) {
public static OffsetDateTime parseLeniently(CharSequence input) throws ParseException {
try {
var result = OffsetDateTime.parse(input, LENIENT_PARSER);
return Optional.of(result);
return OffsetDateTime.parse(input, LENIENT_PARSER);
} catch (DateTimeParseException e) {
return Optional.empty();
throw new ParseException(e.getParsedString(), e.getErrorIndex());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.text.ParseException;
import java.time.OffsetDateTime;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

Expand All @@ -27,9 +29,9 @@ static List<String> successfulCases() {

@ParameterizedTest
@MethodSource("successfulCases")
void parse(String input) {
void parse(String input) throws ParseException {
var res = OffsetDateTimeParser.parseLeniently(input);
assertTrue(res.get().isEqual(TIME));
assertTrue(res.isEqual(TIME));
}

static List<String> failedCases() {
Expand All @@ -39,7 +41,11 @@ static List<String> failedCases() {
@ParameterizedTest
@MethodSource("failedCases")
void failed(String input) {
var res = OffsetDateTimeParser.parseLeniently(input);
assertTrue(res.isEmpty());
Assertions.assertThrows(
ParseException.class,
() -> {
OffsetDateTimeParser.parseLeniently(input);
}
);
}
}

0 comments on commit 10fac09

Please sign in to comment.