Skip to content

Commit 83d35a3

Browse files
Add withSourceLocation param to ErrorStr (#33369)
* Add withSourceLocation param to ErrorStr This allows callers to choose at runtime whether or not to include source locations in error strings. * Add tests * Restyled by whitespace * Restyled by clang-format * Remove iostream import This got forgotten from debugging. * Restyled by whitespace --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 8c9ef98 commit 83d35a3

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

src/lib/core/CHIPError.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
#include <limits>
3636
#include <type_traits>
3737

38-
#if __cplusplus >= 202002L
38+
#if CHIP_CONFIG_ERROR_SOURCE && __cplusplus >= 202002L
3939
#include <source_location>
40-
#endif // __cplusplus >= 202002L
40+
#endif // CHIP_CONFIG_ERROR_SOURCE && __cplusplus >= 202002L
4141

4242
namespace chip {
4343

@@ -239,10 +239,10 @@ class ChipError
239239
* @note
240240
* Normally, prefer to use Format()
241241
*/
242-
const char * AsString() const
242+
const char * AsString(bool withSourceLocation = true) const
243243
{
244-
extern const char * ErrorStr(ChipError);
245-
return ErrorStr(*this);
244+
extern const char * ErrorStr(ChipError, bool);
245+
return ErrorStr(*this, withSourceLocation);
246246
}
247247

248248
/**

src/lib/core/ErrorStr.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,21 @@ static ErrorFormatter * sErrorFormatterList = nullptr;
4141
* describing the provided error.
4242
*
4343
* @param[in] err The error for format and describe.
44+
* @param[in] withSourceLocation Whether or not to include the source
45+
* location in the output string. Only used if CHIP_CONFIG_ERROR_SOURCE &&
46+
* !CHIP_CONFIG_SHORT_ERROR_STR. Defaults to true.
4447
*
4548
* @return A pointer to a NULL-terminated C string describing the
4649
* provided error.
4750
*/
48-
DLL_EXPORT const char * ErrorStr(CHIP_ERROR err)
51+
DLL_EXPORT const char * ErrorStr(CHIP_ERROR err, bool withSourceLocation)
4952
{
5053
char * formattedError = sErrorStr;
5154
uint16_t formattedSpace = sizeof(sErrorStr);
5255

5356
#if CHIP_CONFIG_ERROR_SOURCE && !CHIP_CONFIG_SHORT_ERROR_STR
5457

55-
const char * const file = err.GetFile();
56-
if (file != nullptr)
58+
if (const char * const file = err.GetFile(); withSourceLocation && file != nullptr)
5759
{
5860
int n = snprintf(formattedError, formattedSpace, "%s:%u: ", file, err.GetLine());
5961
if (n > formattedSpace)

src/lib/core/ErrorStr.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct ErrorFormatter
4848
ErrorFormatter * Next;
4949
};
5050

51-
extern const char * ErrorStr(CHIP_ERROR err);
51+
extern const char * ErrorStr(CHIP_ERROR err, bool withSourceLocation = true);
5252
extern void RegisterErrorFormatter(ErrorFormatter * errFormatter);
5353
extern void DeregisterErrorFormatter(ErrorFormatter * errFormatter);
5454
extern void FormatError(char * buf, uint16_t bufSize, const char * subsys, CHIP_ERROR err, const char * desc);

src/lib/core/tests/TestCHIPErrorStr.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,40 @@ TEST(TestCHIPErrorStr, CheckCoreErrorStr)
195195
char const * const file = err.GetFile();
196196
ASSERT_NE(file, nullptr);
197197
EXPECT_EQ(strstr(file, "src/lib/core/"), file);
198+
199+
// File should be included in the error.
200+
EXPECT_NE(strstr(errStr, file), nullptr);
201+
#endif // CHIP_CONFIG_ERROR_SOURCE
202+
}
203+
}
204+
205+
TEST(TestCHIPErrorStr, CheckCoreErrorStrWithoutSourceLocation)
206+
{
207+
// Register the layer error formatter
208+
209+
RegisterCHIPLayerErrorFormatter();
210+
211+
// For each defined error...
212+
for (const auto & err : kTestElements)
213+
{
214+
const char * errStr = ErrorStr(err, /*withSourceLocation=*/false);
215+
char expectedText[9];
216+
217+
// Assert that the error string contains the error number in hex.
218+
snprintf(expectedText, sizeof(expectedText), "%08" PRIX32, static_cast<uint32_t>(err.AsInteger()));
219+
EXPECT_TRUE((strstr(errStr, expectedText) != nullptr));
220+
221+
#if !CHIP_CONFIG_SHORT_ERROR_STR
222+
// Assert that the error string contains a description, which is signaled
223+
// by a presence of a colon proceeding the description.
224+
EXPECT_TRUE((strchr(errStr, ':') != nullptr));
225+
#endif // !CHIP_CONFIG_SHORT_ERROR_STR
226+
227+
#if CHIP_CONFIG_ERROR_SOURCE
228+
char const * const file = err.GetFile();
229+
ASSERT_NE(file, nullptr);
230+
// File should not be included in the error.
231+
EXPECT_EQ(strstr(errStr, file), nullptr);
198232
#endif // CHIP_CONFIG_ERROR_SOURCE
199233
}
200234
}

0 commit comments

Comments
 (0)