IABTechLab / iabgpp-java

Apache License 2.0
11 stars 12 forks source link

* throws Error #22

Closed ym-corey closed 2 months ago

ym-corey commented 1 year ago

In Java, throwing Error is reserved for the JVM/the most catastrophic of problems in the system. Things that are truly unrecoverable. This code throws Error (maybe more code than just this, did not look deeper). If a malformed string is attempted to be decoded by the library, it would be more correct to throw Exception or something which extends Exception (maybe re-use the DecodingException?). A malformed consent string is a recoverable issue.

  public void decodeSegmentsFromBitStrings(List<String> segmentBitStrings) throws DecodingException {
    for (int i = 0; i < this.segments.length && i < segmentBitStrings.size(); i++) {
      String segmentBitString = segmentBitStrings.get(i);
      if (segmentBitString != null && segmentBitString.length() > 0) {
        int index = 0;
        for (int j = 0; j < this.segments[i].length; j++) {
          String fieldName = this.segments[i][j];
          if (this.fields.containsKey(fieldName)) {
            try {
              AbstractEncodableBitStringDataType<?> field = this.fields.get(fieldName);
              String substring = field.substring(segmentBitString, index);
              field.decode(substring);
              index += substring.length();
            } catch (Exception e) {
              throw new Error("Unable to decode " + fieldName, e);
            }
          } else {
            throw new Error("Field not found: '" + fieldName + "'");
          }
        }
      }
    }
  }
chuff commented 1 year ago

Good catch. Introduced an InvalidFieldException in 3.0.9