From 4be2a0fa1dba60f931ff6dee9bd1f6f614a20281 Mon Sep 17 00:00:00 2001 From: Ryan Blue Date: Wed, 13 Sep 2023 21:45:37 -0400 Subject: [PATCH] Make loadAprilTagFieldLayout throw an unchecked exception instead --- .../edu/wpi/first/apriltag/AprilTagFieldLayout.java | 11 +++++++++-- .../java/edu/wpi/first/apriltag/AprilTagFields.java | 11 ++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFieldLayout.java b/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFieldLayout.java index 2c9bc4769c5..834540d0eb0 100644 --- a/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFieldLayout.java +++ b/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFieldLayout.java @@ -199,9 +199,16 @@ public void serialize(Path path) throws IOException { * @throws IOException If the resource could not be loaded */ public static AprilTagFieldLayout loadFromResource(String resourcePath) throws IOException { - try (InputStream stream = AprilTagFieldLayout.class.getResourceAsStream(resourcePath); - InputStreamReader reader = new InputStreamReader(stream)) { + InputStream stream = AprilTagFieldLayout.class.getResourceAsStream(resourcePath); + if(stream == null) { + // Class.getResourceAsStream() returns null if the resource does not exist. + throw new IOException("Could not locate resource: " + resourcePath); + } + InputStreamReader reader = new InputStreamReader(stream); + try { return new ObjectMapper().readerFor(AprilTagFieldLayout.class).readValue(reader); + } catch (IOException e) { + throw new IOException("Failed to load AprilTagFieldLayout: " + resourcePath); } } diff --git a/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFields.java b/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFields.java index be9caecb9ee..89d256e1a4a 100644 --- a/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFields.java +++ b/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFields.java @@ -5,6 +5,7 @@ package edu.wpi.first.apriltag; import java.io.IOException; +import java.io.UncheckedIOException; public enum AprilTagFields { k2022RapidReact("2022-rapidreact.json"), @@ -25,9 +26,13 @@ public enum AprilTagFields { * Get a {@link AprilTagFieldLayout} from the resource JSON. * * @return AprilTagFieldLayout of the field - * @throws IOException If the layout does not exist + * @throws UncheckedIOException If the layout does not exist */ - public AprilTagFieldLayout loadAprilTagLayoutField() throws IOException { - return AprilTagFieldLayout.loadFromResource(m_resourceFile); + public AprilTagFieldLayout loadAprilTagLayoutField() { + try { + return AprilTagFieldLayout.loadFromResource(m_resourceFile); + } catch (IOException e) { + throw new UncheckedIOException("Could not load AprilTagFieldLayout from " + m_resourceFile, e); + } } }