Skip to content

Commit

Permalink
Make loadAprilTagFieldLayout throw an unchecked exception instead
Browse files Browse the repository at this point in the history
  • Loading branch information
rzblue committed Sep 14, 2023
1 parent 57b2d6f commit 4be2a0f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package edu.wpi.first.apriltag;

import java.io.IOException;
import java.io.UncheckedIOException;

public enum AprilTagFields {
k2022RapidReact("2022-rapidreact.json"),
Expand All @@ -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);
}
}
}

0 comments on commit 4be2a0f

Please sign in to comment.