Skip to content

Commit

Permalink
Cleanup error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Morten Haraldsen committed Feb 19, 2025
1 parent 82f97e3 commit bf10bf4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 35 deletions.
73 changes: 39 additions & 34 deletions src/main/java/com/ethlo/time/internal/ItuDurationParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,7 @@ private static int readUntilNonDigit(final String text, final int offset, final
error("Value too large for unit '" + (char) unit + "'", text, index);

Check warning on line 118 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L118

Added line #L118 was not covered by tests
}

if (unit == 0)
{
error("No unit defined for value " + value, text, index);
}

final int length = index - offset;
if (length == 0 && (unit == UNIT_WEEK || unit == UNIT_DAY || unit == UNIT_HOUR || unit == UNIT_MINUTE || unit == UNIT_SECOND || unit == DOT))
{
error("Zero-length value prior to unit '" + ((char) unit) + "'", text, index);
}

consumer.accept(text, index, length, (char) unit, (int) value);

Expand Down Expand Up @@ -162,7 +153,7 @@ private DurationPartsConsumer(final int startOffset, boolean negative)
this.negative = negative;
}

public final void accept(final String chars, final int index, final int length, final char unit, final int value)
public final void accept(final String text, final int index, final int length, final char unit, final int value)
{
final int relIndex = index - startOffset;

Expand All @@ -171,15 +162,15 @@ public final void accept(final String chars, final int index, final int length,
{
if (unit != 'P')
{
error("Duration must start with 'P'", chars, index);
error("Duration must start with 'P'", text, index);

Check warning on line 165 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L165

Added line #L165 was not covered by tests
}
pFound = true;
return;
}

if (!pFound)
{
error("Duration must start with 'P'", chars, index);
error("Duration must start with 'P'", text, index);

Check warning on line 173 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L173

Added line #L173 was not covered by tests
}

/*
Expand All @@ -188,76 +179,90 @@ public final void accept(final String chars, final int index, final int length,
* 64-bit long's limits.
*/

if (unit == 0)
{
if (!dotFound)
{
error("No unit defined for value " + value, text, index);

Check warning on line 186 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L186

Added line #L186 was not covered by tests
}
error("No unit defined for value " + seconds + DOT + value, text, index);

Check warning on line 188 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L188

Added line #L188 was not covered by tests
}

if (length == 0 && (unit == UNIT_WEEK || unit == UNIT_DAY || unit == UNIT_HOUR || unit == UNIT_MINUTE || unit == UNIT_SECOND || unit == DOT))
{
error("Zero-length value prior to unit '" + unit + "'", text, index);

Check warning on line 193 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L193

Added line #L193 was not covered by tests
}

switch (unit)
{
case SEP_T:

if (length != 0)
{
// We have a number in front of the T
error("There is no unit for the number prior to the 'T'", chars, index);
error("There is no unit for the number prior to the 'T'", text, index);

Check warning on line 203 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L203

Added line #L203 was not covered by tests
}

assertNonFractional('T', chars, index);
assertNonFractional('T', text, index);
if (afterT)
{
error("Only one 'T' is allowed and must precede time units", chars, index);
error("Only one 'T' is allowed and must precede time units", text, index);

Check warning on line 209 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L209

Added line #L209 was not covered by tests
}
afterT = true;
break;

case UNIT_WEEK:
assertNonFractional(UNIT_WEEK, chars, index);
assertNonFractional(UNIT_WEEK, text, index);
if (wFound > 0)
{
error("'W' (week) can only appear once", chars, index);
error("'W' (week) can only appear once", text, index);

Check warning on line 218 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L218

Added line #L218 was not covered by tests
}

if (afterT)
{
error("'W' (week) must appear before 'T' in the duration", chars, index);
error("'W' (week) must appear before 'T' in the duration", text, index);

Check warning on line 223 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L223

Added line #L223 was not covered by tests
}
seconds += value * 604800L; // 7 * 86400
wFound = index;
break;

case UNIT_DAY:
assertNonFractional('D', chars, index);
assertNonFractional('D', text, index);
if (dFound > 0)
{
error("'D' (days) can only appear once", chars, index);
error("'D' (days) can only appear once", text, index);

Check warning on line 233 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L233

Added line #L233 was not covered by tests
}
if (afterT)
{
error("'D' (days) must appear before 'T' in the duration", chars, index);
error("'D' (days) must appear before 'T' in the duration", text, index);

Check warning on line 237 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L237

Added line #L237 was not covered by tests
}
seconds += value * 86400L;
dFound = index;
break;

case UNIT_HOUR:
assertNonFractional('H', chars, index);
assertNonFractional('H', text, index);
if (hFound > 0)
{
error("'H' (hours) can only appear once", chars, index);
error("'H' (hours) can only appear once", text, index);

Check warning on line 247 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L247

Added line #L247 was not covered by tests
}
if (!afterT)
{
error("'H' (hours) must appear after 'T' in the duration", chars, index);
error("'H' (hours) must appear after 'T' in the duration", text, index);

Check warning on line 251 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L251

Added line #L251 was not covered by tests
}
seconds += value * 3600L;
hFound = index;
break;

case UNIT_MINUTE:
assertNonFractional(UNIT_MINUTE, chars, index);
assertNonFractional(UNIT_MINUTE, text, index);
if (mFound > 0)
{
error("'M' (minutes) can only appear once", chars, index);
error("'M' (minutes) can only appear once", text, index);

Check warning on line 261 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L261

Added line #L261 was not covered by tests
}
if (!afterT)
{
error("'M' (minutes) must appear after 'T' in the duration", chars, index);
error("'M' (minutes) must appear after 'T' in the duration", text, index);

Check warning on line 265 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L265

Added line #L265 was not covered by tests
}
seconds += value * 60L;
mFound = index;
Expand All @@ -266,24 +271,24 @@ public final void accept(final String chars, final int index, final int length,
case UNIT_SECOND:
if (sFound > 0)
{
error("'S' (seconds) can only appear once", chars, index);
error("'S' (seconds) can only appear once", text, index);

Check warning on line 274 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L274

Added line #L274 was not covered by tests
}
if (!afterT)
{
error("'S' (seconds) must appear after 'T' in the duration", chars, index);
error("'S' (seconds) must appear after 'T' in the duration", text, index);

Check warning on line 278 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L278

Added line #L278 was not covered by tests
}
sFound = index;

if (readingFractionalPart)
{
if (length > 9)
{
error("Maximum allowed is 9 fraction digits", chars, index);
error("Maximum allowed is 9 fraction digits", text, index);

Check warning on line 286 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L286

Added line #L286 was not covered by tests
}

if (length == 0)
{
error("Must have at least one fraction digit after the '.''", chars, index);
error("Must have at least one fraction digit after the '.''", text, index);

Check warning on line 291 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L291

Added line #L291 was not covered by tests
}

nano = value;
Expand All @@ -310,19 +315,19 @@ public final void accept(final String chars, final int index, final int length,
case DOT:
if (dotFound)
{
error("'.' can only appear once", chars, index);
error("'.' can only appear once", text, index);

Check warning on line 318 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L318

Added line #L318 was not covered by tests
}
if (!afterT)
{
error("Fractional seconds (.) must come after 'T'", chars, index);
error("Fractional seconds (.) must come after 'T'", text, index);

Check warning on line 322 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L322

Added line #L322 was not covered by tests
}
readingFractionalPart = true;
seconds += value; // Assume integer part of seconds before fraction
dotFound = true;
break;

default:
error("Invalid unit: " + unit, chars, index);
error("Invalid unit: " + unit, text, index);

Check warning on line 330 in src/main/java/com/ethlo/time/internal/ItuDurationParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/ItuDurationParser.java#L330

Added line #L330 was not covered by tests
}
}

Expand Down
14 changes: 13 additions & 1 deletion src/test/java/com/ethlo/time/ItuDurationParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,19 @@ void testNothingAfterDot()
@Test
void testNoUnitAfterFractions()
{
assertThrows(DateTimeParseException.class, () -> ItuDurationParser.parse("PT1.5"));
final DateTimeParseException exc = assertThrows(DateTimeParseException.class, () -> ItuDurationParser.parse("PT1.5"));
assertThat(exc).hasMessage("No unit defined for value 1.5: PT1.5");
}

@Test
void testWeirdInputs()
{
final String[] inputs = {"-PTHHHH6.2S.6S2.62.2S2.0S.", "PT..0.S.", "PT1SP"};
for (String input : inputs)
{
final DateTimeParseException exc = assertThrows(DateTimeParseException.class, () -> ITU.parseDuration(input));
System.out.println(exc.getMessage());
}
}

@Test
Expand Down

0 comments on commit bf10bf4

Please sign in to comment.