WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
Errors.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
7#include <stdint.h>
8
9#include <memory>
10#include <stdexcept>
11#include <string>
12
13#include <fmt/format.h>
14
15namespace frc {
16
17/**
18 * Runtime error exception.
19 */
20class RuntimeError : public std::runtime_error {
21 public:
22 RuntimeError(int32_t code, std::string&& loc, std::string&& stack,
23 std::string&& message);
24 RuntimeError(int32_t code, const char* fileName, int lineNumber,
25 const char* funcName, std::string&& stack,
26 std::string&& message);
28 int32_t code() const noexcept { return m_data->code; }
29 const char* loc() const noexcept { return m_data->loc.c_str(); }
30 const char* stack() const noexcept { return m_data->stack.c_str(); }
31
32 /**
33 * Reports error to Driver Station (using HAL_SendError).
34 */
35 void Report() const;
36
37 private:
38 struct Data {
39 int32_t code;
40 std::string loc;
41 std::string stack;
42 };
43 std::shared_ptr<Data> m_data;
44};
45
46/**
47 * Gets error message string for an error code.
48 */
49const char* GetErrorMessage(int32_t* code);
50
51/**
52 * Reports an error to the driver station (using HAL_SendError).
53 * Generally the FRC_ReportError wrapper macro should be used instead.
54 *
55 * @param[out] status error code
56 * @param[in] fileName source file name
57 * @param[in] lineNumber source line number
58 * @param[in] funcName source function name
59 * @param[in] format error message format
60 * @param[in] args error message format args
61 */
62void ReportErrorV(int32_t status, const char* fileName, int lineNumber,
63 const char* funcName, fmt::string_view format,
64 fmt::format_args args);
65
66/**
67 * Reports an error to the driver station (using HAL_SendError).
68 * Generally the FRC_ReportError wrapper macro should be used instead.
69 *
70 * @param[out] status error code
71 * @param[in] fileName source file name
72 * @param[in] lineNumber source line number
73 * @param[in] funcName source function name
74 * @param[in] format error message format
75 * @param[in] args error message format args
76 */
77template <typename... Args>
78inline void ReportError(int32_t status, const char* fileName, int lineNumber,
79 const char* funcName, fmt::string_view format,
80 Args&&... args) {
81 ReportErrorV(status, fileName, lineNumber, funcName, format,
82 fmt::make_format_args(args...));
83}
84
85/**
86 * Makes a runtime error exception object. This object should be thrown
87 * by the caller. Generally the FRC_MakeError wrapper macro should be used
88 * instead.
89 *
90 * @param[out] status error code
91 * @param[in] fileName source file name
92 * @param[in] lineNumber source line number
93 * @param[in] funcName source function name
94 * @param[in] format error message format
95 * @param[in] args error message format args
96 * @return runtime error object
97 */
98[[nodiscard]]
99RuntimeError MakeErrorV(int32_t status, const char* fileName, int lineNumber,
100 const char* funcName, fmt::string_view format,
101 fmt::format_args args);
102
103template <typename... Args>
104[[nodiscard]]
105inline RuntimeError MakeError(int32_t status, const char* fileName,
106 int lineNumber, const char* funcName,
107 fmt::string_view format, Args&&... args) {
108 return MakeErrorV(status, fileName, lineNumber, funcName, format,
109 fmt::make_format_args(args...));
110}
111
112namespace err {
113#define S(label, offset, message) inline constexpr int label = offset;
114#include "frc/WPIErrors.mac"
115#undef S
116} // namespace err
117
118namespace warn {
119#define S(label, offset, message) inline constexpr int label = offset;
120#include "frc/WPIWarnings.mac"
121#undef S
122} // namespace warn
123} // namespace frc
124
125// C++20 relaxed the number of arguments to variadics, but Apple Clang's
126// warnings haven't caught up yet: https://stackoverflow.com/a/67996331
127#ifdef __clang__
128#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
129#endif
130
131/**
132 * Reports an error to the driver station (using HAL_SendError).
133 *
134 * @param[out] status error code
135 * @param[in] format error message format
136 */
137#define FRC_ReportError(status, format, ...) \
138 do { \
139 if ((status) != 0) { \
140 ::frc::ReportError(status, __FILE__, __LINE__, __FUNCTION__, \
141 format __VA_OPT__(, ) __VA_ARGS__); \
142 } \
143 } while (0)
144
145/**
146 * Reports a warning to the driver station (using HAL_SendError).
147 *
148 * @param[in] format error message format
149 */
150#define FRC_ReportWarning(format, ...) \
151 do { \
152 ::frc::ReportError(::frc::warn::Warning, __FILE__, __LINE__, __FUNCTION__, \
153 format __VA_OPT__(, ) __VA_ARGS__); \
154 } while (0)
155
156/**
157 * Makes a runtime error exception object. This object should be thrown
158 * by the caller.
159 *
160 * @param[out] status error code
161 * @param[in] format error message format
162 * @return runtime error object
163 */
164#define FRC_MakeError(status, format, ...) \
165 ::frc::MakeError(status, __FILE__, __LINE__, __FUNCTION__, \
166 format __VA_OPT__(, ) __VA_ARGS__)
167
168/**
169 * Checks a status code and depending on its value, either throws a
170 * RuntimeError exception, calls ReportError, or does nothing (if no error).
171 *
172 * @param[out] status error code
173 * @param[in] format error message format
174 */
175#define FRC_CheckErrorStatus(status, format, ...) \
176 do { \
177 if ((status) < 0) { \
178 throw ::frc::MakeError(status, __FILE__, __LINE__, __FUNCTION__, \
179 format __VA_OPT__(, ) __VA_ARGS__); \
180 } else if ((status) > 0) { \
181 ::frc::ReportError(status, __FILE__, __LINE__, __FUNCTION__, \
182 format __VA_OPT__(, ) __VA_ARGS__); \
183 } \
184 } while (0)
185
186#define FRC_AssertMessage(condition, format, ...) \
187 do { \
188 if (!(condition)) { \
189 throw ::frc::MakeError(::frc::err::AssertionFailure, __FILE__, __LINE__, \
190 __FUNCTION__, format __VA_OPT__(, ) __VA_ARGS__); \
191 } \
192 } while (0)
193
194#define FRC_Assert(condition) FRC_AssertMessage(condition, #condition)
and restrictions which apply to each piece of software is included later in this file and or inside of the individual applicable source files The disclaimer of warranty in the WPILib license above applies to all code in and nothing in any of the other licenses gives permission to use the names of FIRST nor the names of the WPILib contributors to endorse or promote products derived from this software The following pieces of software have additional or alternate and or and nanopb were all modified for use in Google Inc All rights reserved Redistribution and use in source and binary with or without are permitted provided that the following conditions are this list of conditions and the following disclaimer *Redistributions in binary form must reproduce the above copyright this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution *Neither the name of Google Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source code
Definition ThirdPartyNotices.txt:123
Runtime error exception.
Definition Errors.h:20
RuntimeError(int32_t code, const char *fileName, int lineNumber, const char *funcName, std::string &&stack, std::string &&message)
const char * stack() const noexcept
Definition Errors.h:30
const char * loc() const noexcept
Definition Errors.h:29
int32_t code() const noexcept
Definition Errors.h:28
RuntimeError(int32_t code, std::string &&loc, std::string &&stack, std::string &&message)
void Report() const
Reports error to Driver Station (using HAL_SendError).
FMT_INLINE auto format(detail::locale_ref loc, format_string< T... > fmt, T &&... args) -> std::string
Definition format.h:4146
Definition CAN.h:11
RuntimeError MakeError(int32_t status, const char *fileName, int lineNumber, const char *funcName, fmt::string_view format, Args &&... args)
Definition Errors.h:105
RuntimeError MakeErrorV(int32_t status, const char *fileName, int lineNumber, const char *funcName, fmt::string_view format, fmt::format_args args)
Makes a runtime error exception object.
void ReportError(int32_t status, const char *fileName, int lineNumber, const char *funcName, fmt::string_view format, Args &&... args)
Reports an error to the driver station (using HAL_SendError).
Definition Errors.h:78
void ReportErrorV(int32_t status, const char *fileName, int lineNumber, const char *funcName, fmt::string_view format, fmt::format_args args)
Reports an error to the driver station (using HAL_SendError).
const char * GetErrorMessage(int32_t *code)
Gets error message string for an error code.