Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duration parsing error handling #5947

Merged

Conversation

vpaturet
Copy link
Contributor

@vpaturet vpaturet commented Jul 4, 2024

Summary

A request that specifies a time penalty with an incorrect duration format:

accessEgressPenalty: [{
      streetMode: flexible
      timePenalty: "600ss + 1.3 t"
      costFactor: 1.3
    }],

fails with an HTTP 500 error:


"Text cannot be parsed to a Duration: '600ss' java.time.format.DateTimeParseException: Text cannot be parsed to a Duration: '600ss'
	at org.opentripplanner.framework.time.DurationUtils.duration(DurationUtils.java:121)
	at org.opentripplanner.routing.api.request.framework.LinearFunctionSerialization.parse(LinearFunctionSerialization.java:68)
	at org.opentripplanner.apis.transmodel.model.scalars.DoubleFunctionFactory$1.parseValue(DoubleFunctionFactory.java:59)
	at org.opentripplanner.apis.transmodel.model.scalars.DoubleFunctionFactory$1.parseLiteral(DoubleFunctionFactory.java:73)
	at org.opentripplanner.apis.transmodel.model.scalars.DoubleFunctionFactory$1.parseLiteral(DoubleFunctionFactory.java:40)

This PR fixes exception handling so that the parsing error is reported as a client error:


{
  "errors": [
    {
      "message": "Unable to parse duration: '600ss'",
      "locations": [],
      "extensions": {
        "classification": "BadRequestError"
      }
    }
  ]
}

Note: the parsing logic is moved to a private method LinearFunctionSerialization.parseDecimalSecondsOrDuration(). This method should probably not be promoted to the class DurationUtils: it is used solely to fix a backward compatibility issue (see #5304).

Issue

No

Documentation

No

Copy link

codecov bot commented Jul 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 69.46%. Comparing base (2a5c8be) to head (4c5d1ea).
Report is 8 commits behind head on dev-2.x.

Additional details and impacted files
@@            Coverage Diff             @@
##             dev-2.x    #5947   +/-   ##
==========================================
  Coverage      69.45%   69.46%           
- Complexity     17070    17076    +6     
==========================================
  Files           1937     1937           
  Lines          73692    73714   +22     
  Branches        7540     7543    +3     
==========================================
+ Hits           51184    51202   +18     
- Misses         19880    19882    +2     
- Partials        2628     2630    +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@vpaturet vpaturet marked this pull request as ready for review July 4, 2024 13:09
@vpaturet vpaturet requested a review from a team as a code owner July 4, 2024 13:09
@leonardehrenfried leonardehrenfried self-requested a review July 4, 2024 14:24
@vpaturet vpaturet requested a review from t2gran July 4, 2024 14:27
Copy link
Member

@leonardehrenfried leonardehrenfried left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to write a test for this behaviour?

@vpaturet vpaturet force-pushed the fix_duration_parsing_error_handling branch from 15f1461 to 4c5d1ea Compare July 5, 2024 07:34
@vpaturet
Copy link
Contributor Author

vpaturet commented Jul 5, 2024

I've added a couple of test cases to test the behavior with decimal seconds.
I see now that the corner case with invalid duration was not covered, so I added a test case for this as well.

@vpaturet vpaturet merged commit 967fe64 into opentripplanner:dev-2.x Jul 8, 2024
5 checks passed
@t2gran t2gran added this to the 2.6 (next release) milestone Jul 23, 2024
@t2gran t2gran deleted the fix_duration_parsing_error_handling branch October 9, 2024 21:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants