From 72c56f0e6c2309a13eb79441a6fbf515adfc4fe9 Mon Sep 17 00:00:00 2001 From: Ryan Blue Date: Thu, 10 Oct 2024 16:03:00 -0400 Subject: [PATCH] Radio LED: Close opened file on error Improve error messages --- hal/src/main/native/athena/LEDs.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/hal/src/main/native/athena/LEDs.cpp b/hal/src/main/native/athena/LEDs.cpp index 8fa603e0fa0..0091602f4c9 100644 --- a/hal/src/main/native/athena/LEDs.cpp +++ b/hal/src/main/native/athena/LEDs.cpp @@ -8,8 +8,11 @@ #include +#include +#include #include +#include "HALInternal.h" #include "hal/Errors.h" namespace hal::init { @@ -34,13 +37,20 @@ void HAL_SetRadioLEDState(HAL_RadioLEDState state, int32_t* status) { fs::file_t greenFile = fs::OpenFileForWrite(radioLEDGreenFilePath, ec, fs::CD_OpenExisting, fs::OF_Text); if (ec) { + // not opened, nothing to clean up *status = INCOMPATIBLE_STATE; + hal::SetLastError(status, fmt::format("Could not open '{}' for write: {}", + greenFile, ec.message())); return; } fs::file_t redFile = fs::OpenFileForWrite(radioLEDRedFilePath, ec, fs::CD_OpenExisting, fs::OF_Text); if (ec) { + // green file opened successfully, need to close it + fs::CloseFile(greenFile); *status = INCOMPATIBLE_STATE; + hal::SetLastError(status, fmt::format("Could not open '{}' for write: {}", + greenFile, ec.message())); return; } @@ -55,6 +65,8 @@ bool ReadStateFromFile(fs::path path, int32_t* status) { std::error_code ec; fs::file_t file = fs::OpenFileForRead(path, ec, fs::OF_Text); if (ec) { + hal::SetLastError(status, fmt::format("Could not open '{}' for read: {}", + path, ec.message())); *status = INCOMPATIBLE_STATE; return false; }