[wpilibc] Remove ErrorBase (#3306)

Replace with new exception-based error reporting, consistent with Java.
This also builds stacktraces into the reporting/exceptions.
This commit is contained in:
Peter Johnson
2021-04-18 20:35:29 -07:00
committed by GitHub
parent 0abf6c9045
commit 8d961dfd25
113 changed files with 993 additions and 2200 deletions

View File

@@ -6,9 +6,9 @@
#include <typeinfo>
#include "frc/Errors.h"
#include "frc/RobotState.h"
#include "frc/Timer.h"
#include "frc/WPIErrors.h"
#include "frc/commands/CommandGroup.h"
#include "frc/commands/Scheduler.h"
#include "frc/livewindow/LiveWindow.h"
@@ -32,7 +32,7 @@ Command::Command(Subsystem& subsystem) : Command("", -1.0) {
Command::Command(const wpi::Twine& name, double timeout) {
// We use -1.0 to indicate no timeout.
if (timeout < 0.0 && timeout != -1.0) {
wpi_setWPIErrorWithContext(ParameterOutOfRange, "timeout < 0.0");
throw FRC_MakeError(err::ParameterOutOfRange, "timeout < 0.0");
}
m_timeout = timeout;
@@ -77,15 +77,15 @@ void Command::Requires(Subsystem* subsystem) {
if (subsystem != nullptr) {
m_requirements.insert(subsystem);
} else {
wpi_setWPIErrorWithContext(NullParameter, "subsystem");
throw FRC_MakeError(err::NullParameter, "subsystem");
}
}
void Command::Start() {
LockChanges();
if (m_parent != nullptr) {
wpi_setWPIErrorWithContext(
CommandIllegalUse,
throw FRC_MakeError(
err::CommandIllegalUse,
"Can not start a command that is part of a command group");
}
@@ -115,8 +115,8 @@ bool Command::Run() {
void Command::Cancel() {
if (m_parent != nullptr) {
wpi_setWPIErrorWithContext(
CommandIllegalUse,
throw FRC_MakeError(
err::CommandIllegalUse,
"Can not cancel a command that is part of a command group");
}
@@ -173,7 +173,7 @@ int Command::GetID() const {
void Command::SetTimeout(double timeout) {
if (timeout < 0.0) {
wpi_setWPIErrorWithContext(ParameterOutOfRange, "timeout < 0.0");
throw FRC_MakeError(err::ParameterOutOfRange, "timeout < 0.0");
} else {
m_timeout = timeout;
}
@@ -185,21 +185,22 @@ bool Command::IsTimedOut() const {
bool Command::AssertUnlocked(const std::string& message) {
if (m_locked) {
std::string buf =
message + " after being started or being added to a command group";
wpi_setWPIErrorWithContext(CommandIllegalUse, buf);
return false;
throw FRC_MakeError(
err::CommandIllegalUse,
message +
wpi::Twine{
" after being started or being added to a command group"});
}
return true;
}
void Command::SetParent(CommandGroup* parent) {
if (parent == nullptr) {
wpi_setWPIErrorWithContext(NullParameter, "parent");
throw FRC_MakeError(err::NullParameter, "parent");
} else if (m_parent != nullptr) {
wpi_setWPIErrorWithContext(CommandIllegalUse,
"Can not give command to a command group after "
"already being put in a command group");
throw FRC_MakeError(err::CommandIllegalUse,
"Can not give command to a command group after "
"already being put in a command group");
} else {
LockChanges();
m_parent = parent;

View File

@@ -4,16 +4,15 @@
#include "frc/commands/CommandGroup.h"
#include "frc/WPIErrors.h"
#include "frc/Errors.h"
using namespace frc;
CommandGroup::CommandGroup(const wpi::Twine& name) : Command(name) {}
void CommandGroup::AddSequential(Command* command) {
if (command == nullptr) {
wpi_setWPIErrorWithContext(NullParameter, "command");
return;
if (!command) {
throw FRC_MakeError(err::NullParameter, "command");
}
if (!AssertUnlocked("Cannot add new command to command group")) {
return;
@@ -31,16 +30,14 @@ void CommandGroup::AddSequential(Command* command) {
}
void CommandGroup::AddSequential(Command* command, double timeout) {
if (command == nullptr) {
wpi_setWPIErrorWithContext(NullParameter, "command");
return;
if (!command) {
throw FRC_MakeError(err::NullParameter, "command");
}
if (!AssertUnlocked("Cannot add new command to command group")) {
return;
}
if (timeout < 0.0) {
wpi_setWPIErrorWithContext(ParameterOutOfRange, "timeout < 0.0");
return;
throw FRC_MakeError(err::ParameterOutOfRange, "timeout < 0.0");
}
m_commands.emplace_back(command, CommandGroupEntry::kSequence_InSequence,
@@ -56,8 +53,8 @@ void CommandGroup::AddSequential(Command* command, double timeout) {
}
void CommandGroup::AddParallel(Command* command) {
if (command == nullptr) {
wpi_setWPIErrorWithContext(NullParameter, "command");
if (!command) {
throw FRC_MakeError(err::NullParameter, "command");
return;
}
if (!AssertUnlocked("Cannot add new command to command group")) {
@@ -76,16 +73,14 @@ void CommandGroup::AddParallel(Command* command) {
}
void CommandGroup::AddParallel(Command* command, double timeout) {
if (command == nullptr) {
wpi_setWPIErrorWithContext(NullParameter, "command");
return;
if (!command) {
throw FRC_MakeError(err::NullParameter, "command");
}
if (!AssertUnlocked("Cannot add new command to command group")) {
return;
}
if (timeout < 0.0) {
wpi_setWPIErrorWithContext(ParameterOutOfRange, "timeout < 0.0");
return;
throw FRC_MakeError(err::ParameterOutOfRange, "timeout < 0.0");
}
m_commands.emplace_back(command, CommandGroupEntry::kSequence_BranchChild,

View File

@@ -13,7 +13,7 @@
#include <networktables/NetworkTableEntry.h>
#include <wpi/mutex.h>
#include "frc/WPIErrors.h"
#include "frc/Errors.h"
#include "frc/buttons/ButtonScheduler.h"
#include "frc/commands/Command.h"
#include "frc/commands/Subsystem.h"
@@ -64,9 +64,8 @@ void Scheduler::AddButton(ButtonScheduler* button) {
}
void Scheduler::RegisterSubsystem(Subsystem* subsystem) {
if (subsystem == nullptr) {
wpi_setWPIErrorWithContext(NullParameter, "subsystem");
return;
if (!subsystem) {
throw FRC_MakeError(err::NullParameter, "subsystem");
}
m_impl->subsystems.insert(subsystem);
}
@@ -109,8 +108,8 @@ void Scheduler::Run() {
for (auto& addition : m_impl->additions) {
// Check to make sure no adding during adding
if (m_impl->adding) {
wpi_setWPIErrorWithContext(IncompatibleState,
"Can not start command from cancel method");
FRC_ReportError(warn::IncompatibleState,
"Can not start command from cancel method");
} else {
m_impl->ProcessCommandAddition(addition);
}
@@ -122,8 +121,8 @@ void Scheduler::Run() {
for (auto& subsystem : m_impl->subsystems) {
if (subsystem->GetCurrentCommand() == nullptr) {
if (m_impl->adding) {
wpi_setWPIErrorWithContext(IncompatibleState,
"Can not start command from cancel method");
FRC_ReportError(warn::IncompatibleState,
"Can not start command from cancel method");
} else {
m_impl->ProcessCommandAddition(subsystem->GetDefaultCommand());
}
@@ -133,9 +132,8 @@ void Scheduler::Run() {
}
void Scheduler::Remove(Command* command) {
if (command == nullptr) {
wpi_setWPIErrorWithContext(NullParameter, "command");
return;
if (!command) {
throw FRC_MakeError(err::NullParameter, "command");
}
m_impl->Remove(command);

View File

@@ -4,7 +4,7 @@
#include "frc/commands/Subsystem.h"
#include "frc/WPIErrors.h"
#include "frc/Errors.h"
#include "frc/commands/Command.h"
#include "frc/commands/Scheduler.h"
#include "frc/livewindow/LiveWindow.h"
@@ -24,9 +24,8 @@ void Subsystem::SetDefaultCommand(Command* command) {
} else {
const auto& reqs = command->GetRequirements();
if (std::find(reqs.begin(), reqs.end(), this) == reqs.end()) {
wpi_setWPIErrorWithContext(
CommandIllegalUse, "A default command must require the subsystem");
return;
throw FRC_MakeError(err::CommandIllegalUse,
"A default command must require the subsystem");
}
m_defaultCommand = command;