Error messages versus error codes
This commit is contained in:
11
style.md
11
style.md
@@ -303,9 +303,20 @@ arena.reset(); // Reset arena memory
|
|||||||
- **Return codes** for expected errors
|
- **Return codes** for expected errors
|
||||||
- **Exceptions** only for exceptional circumstances
|
- **Exceptions** only for exceptional circumstances
|
||||||
- **fprintf + abort()** for unrecoverable errors
|
- **fprintf + abort()** for unrecoverable errors
|
||||||
|
- **Error messages are for humans only** - never parse error message strings programmatically
|
||||||
|
- **Error codes are the contract** - use enums/codes for programmatic error handling
|
||||||
```cpp
|
```cpp
|
||||||
enum class ParseResult { Success, InvalidJson, MissingField };
|
enum class ParseResult { Success, InvalidJson, MissingField };
|
||||||
|
|
||||||
|
// Good: Test error codes (part of contract)
|
||||||
|
auto result = parser.parse(data);
|
||||||
|
if (result == ParseResult::InvalidJson) {
|
||||||
|
// Handle programmatically
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bad: Don't test or parse error message strings
|
||||||
|
// CHECK(parser.get_error() == "Expected '}' at line 5"); // BRITTLE!
|
||||||
|
|
||||||
if (!memory) {
|
if (!memory) {
|
||||||
std::fprintf(stderr, "ArenaAllocator: Failed to allocate memory\n");
|
std::fprintf(stderr, "ArenaAllocator: Failed to allocate memory\n");
|
||||||
std::abort();
|
std::abort();
|
||||||
|
|||||||
Reference in New Issue
Block a user