opentripplanner / OpenTripPlanner

An open source multi-modal trip planner
http://www.opentripplanner.org
Other
2.21k stars 1.03k forks source link

Error code design #5070

Open t2gran opened 1 year ago

t2gran commented 1 year ago

We don´t want to create a global enum with all error codes in the system. This creates an unstable(change every time a new error code is added) central component. Also, it does not communicate which errors may happen in each component. By component, I mean a logical group of related packages and classes like the stop-model or the transit-model.

One way to do it is to create an interface which all error code enums inherit. For example:

package org.opentripplanner.transit.model.timetable;

/**
 * Details about why a {@link TripTimes} instance is invalid.
 */
public enum TimeTableError implement OtpErrorCode {
    NEGATIVE_DWELL_TIME,
    NEGATIVE_HOP_TIME
  }
}  
package org.opentripplanner.framework.application;

interface OtpErrorCode {
  default String code() {
      return getClass().getSimpleName() + "." + ((Enum<?>)this).name();
  }
}

Returning a Result type with a potential error is nice if the caller needs to take action, but not if the caller must pass the result up the call chain. Many possible errors in a call-three will make the results ugly. Most of the time the caller do not care what the error is, it will just abort. A nicely designed exception hierarchy is much easier to maintain.

From @jtorin:

I second this approach. The enums are still nicely comparable, what you loose is that it's not possible to enumerate all values. Not really an issue in this case I think.

TODOs

t2gran commented 1 year ago

We need to take these goals into acount: