Skip to content

Commit

Permalink
Radio LED: Close opened file on error
Browse files Browse the repository at this point in the history
Improve error messages
  • Loading branch information
rzblue committed Oct 10, 2024
1 parent f150b36 commit 72c56f0
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions hal/src/main/native/athena/LEDs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@

#include <fstream>

#include <fmt/format.h>
#include <fmt/std.h>
#include <wpi/fs.h>

#include "HALInternal.h"
#include "hal/Errors.h"

namespace hal::init {
Expand All @@ -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;
}

Expand All @@ -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;
}
Expand Down

0 comments on commit 72c56f0

Please sign in to comment.