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

Catch and rethrow an exception with an advertised type, #336. #338

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion wres-config/src/wres/config/yaml/DeclarationFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.constructor.DuplicateKeyException;
import org.yaml.snakeyaml.nodes.Tag;
import org.yaml.snakeyaml.parser.ParserException;
import org.yaml.snakeyaml.representer.Representer;
Expand Down Expand Up @@ -418,7 +419,7 @@ static JsonNode deserialize( String yaml ) throws IOException
// Deserialize with Jackson now that any anchors/references are resolved
return DESERIALIZER.readTree( resolvedYamlString );
}
catch ( ScannerException | ParserException | JsonProcessingException e )
catch ( ScannerException | ParserException | JsonProcessingException | DuplicateKeyException e )
{
throw new IOException( "Failed to deserialize a YAML string.", e );
}
Expand Down
1 change: 1 addition & 0 deletions wres-config/src/wres/config/yaml/components/Features.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public record Features( Set<GeometryTuple> geometries, Map<GeometryTuple,Offset>
/**
* Sets the default values.
* @param geometries the geometries
* @param offsets the offset values, such as a datum offset, if any
*/
public Features
{
Expand Down
2 changes: 2 additions & 0 deletions wres-config/src/wres/config/yaml/components/Threshold.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public record Threshold( wres.statistics.generated.Threshold threshold,
* @param type the threshold type to help identify the declaration context
* @param feature a feature
* @param featureNameFrom the orientation of the data from which the named feature is taken
* @param generated true if this threshold was generated by the software, false if declared or read from a source
*/
public Threshold
{
Expand Down Expand Up @@ -65,6 +66,7 @@ public String toString()
.append( "thresholdType", this.type() )
.append( "featureName", featureString )
.append( "featureNameFrom", this.featureNameFrom() )
.append( "generated", this.generated() )
.build();
}
}
31 changes: 31 additions & 0 deletions wres-config/test/wres/config/yaml/DeclarationFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import wres.config.MetricConstants;
import wres.config.yaml.components.AnalysisTimes;
Expand Down Expand Up @@ -2274,6 +2275,36 @@ void testDeserializeWithCovariates() throws IOException
assertEquals( expected, actual );
}

@Test
void testDeserializeThrowsExpectedExceptionWhenDuplicateKeysEncountered()
{
// GitHub issue #336
String yaml = """
observed:
- some_file.csv
predicted:
sources: another_file.csv
probability_thresholds: [0.01,0.1,0.5,0.9,0.95,0.99,0.995,0.999]
probability_thresholds: [0.01,0.1,0.5,0.9,0.95,0.99,0.995,0.999]
""";

assertThrows( IOException.class, () -> DeclarationFactory.from( yaml ) );
}

@Test
void testDeserializeThrowsExpectedExceptionWhenSpacingIncorrect()
{
String yaml = """
observed:
- some_file.csv
predicted:
sources: another_file.csv
probability_thresholds: [0.01,0.1,0.5,0.9,0.95,0.99,0.995,0.999]
""";

assertThrows( IOException.class, () -> DeclarationFactory.from( yaml ) );
}

@Test
void testSerializeWithShortSources() throws IOException
{
Expand Down
Loading