Skip to content

Refactor/better error codes#18

Open
Katze719 wants to merge 13 commits intomainfrom
refactor/better-error-codes
Open

Refactor/better error codes#18
Katze719 wants to merge 13 commits intomainfrom
refactor/better-error-codes

Conversation

@Katze719
Copy link
Member

No description provided.

- Introduced a new structure for status codes in status_codes.h, categorizing errors into Configuration, Connection, Io, Control, and Monitor.
- Removed the obsolete status_codes.hpp file, consolidating functionality into status_codes.h.
- Updated validation functions in validation.hpp to utilize the new status code structure for improved error handling.
- Adjusted documentation in serial_abort_read.h and serial_abort_write.h to reflect changes in status code usage.
- Changed ValueType from std::int32_t to std::int64_t for improved range.
- Replaced the make function with a GenCode template structure for generating error codes, enhancing clarity and maintainability.
- Updated error codes in Configuration, Connection, Io, Control, and Monitor categories to utilize the new GenCode structure.
- Added an explicit conversion operator to the GenCode template for improved usability.
- Updated status code definitions in Configuration, Connection, Io, Control, and Monitor categories to utilize the GenCode structure directly, enhancing clarity and maintainability.
- Moved status code definitions into a new cpp_core::status_codes namespace for better modularity.
- Introduced a detail namespace to encapsulate internal structures, enhancing code clarity.
- Updated references to ValueType and CategoryBase to reflect the new namespace structure, improving maintainability.
@Katze719 Katze719 marked this pull request as ready for review March 18, 2026 08:29
@Katze719 Katze719 requested a review from Mqxx March 18, 2026 08:29
- Introduced a new header file status_code.h to define a structured approach for status codes within the cpp_core::status_codes namespace.
- Implemented a CategoryBase template for generating error codes, enhancing clarity and maintainability.
- Organized status codes into categories: Configuration, Connection, Io, and Control, each with specific error codes for improved usability.
- Changed the CategoryBase template to accept a derived type instead of a category value, enhancing type safety and clarity.
- Updated the Configuration, Connection, Io, and Control structs to define their category codes directly, improving maintainability and readability of status code definitions.
- Changed the initialization of kCategoryMultiplier from an assignment to a uniform initialization syntax, enhancing code consistency and clarity.
@Katze719 Katze719 marked this pull request as draft March 18, 2026 18:57
- Introduced CPP_CORE_STATUS_CATEGORY and CPP_CORE_CODE macros for streamlined status code definitions, improving maintainability and readability.
- Added isError and isSuccess functions for better error handling.
- Updated the CategoryBase template to support compile-time code generation, enhancing type safety and clarity in status code management.
@Katze719 Katze719 marked this pull request as ready for review March 18, 2026 19:11
@Katze719 Katze719 requested a review from Mqxx March 18, 2026 19:12
- Moved isError, isSuccess, and belongsTo functions into the StatusCode struct for better encapsulation and organization.
- Updated function signatures to use detail::ValueType for consistency with the new structure.
- Removed redundant definitions from the detail namespace, enhancing code clarity and maintainability.
@Katze719 Katze719 force-pushed the refactor/better-error-codes branch from 2c35c6e to 2b63c69 Compare March 18, 2026 19:27
- Refactored the Code template to accept a Category type, improving type safety and clarity.
- Introduced new member functions for retrieving the name and category of status codes.
- Replaced macro-based status code definitions with structured definitions for Configuration, Connection, and Io categories, enhancing maintainability and readability.
Comment on lines +136 to +165
// Test cases
static_assert(cpp_core::StatusCode::kSuccess == 0);
static_assert(cpp_core::StatusCode::isSuccess(cpp_core::StatusCode::kSuccess));
static_assert(!cpp_core::StatusCode::isError(cpp_core::StatusCode::kSuccess));

static_assert(cpp_core::StatusCode::Configuration::kSetBaudrateError == -100);
static_assert(cpp_core::StatusCode::Configuration::kSetDataBitsError == -101);
static_assert(cpp_core::StatusCode::Configuration::kSetParityError == -102);
static_assert(cpp_core::StatusCode::Configuration::kSetStopBitsError == -103);
static_assert(cpp_core::StatusCode::Configuration::kSetFlowControlError == -104);
static_assert(cpp_core::StatusCode::Configuration::kSetTimeoutError == -105);

static_assert(cpp_core::StatusCode::Connection::kNotFoundError == -200);
static_assert(cpp_core::StatusCode::Connection::kInvalidHandleError == -201);
static_assert(cpp_core::StatusCode::Connection::kCloseHandleError == -202);

static_assert(cpp_core::StatusCode::Io::kReadError == -300);
static_assert(cpp_core::StatusCode::Io::kWriteError == -301);
static_assert(cpp_core::StatusCode::Io::kAbortReadError == -302);
static_assert(cpp_core::StatusCode::Io::kAbortWriteError == -303);
static_assert(cpp_core::StatusCode::Io::kBufferError == -304);
static_assert(cpp_core::StatusCode::Io::kClearBufferInError == -305);
static_assert(cpp_core::StatusCode::Io::kClearBufferOutError == -306);

static_assert(cpp_core::StatusCode::Control::kSetDtrError == -400);
static_assert(cpp_core::StatusCode::Control::kSetRtsError == -401);
static_assert(cpp_core::StatusCode::Control::kGetModemStatusError == -402);
static_assert(cpp_core::StatusCode::Control::kSendBreakError == -403);
static_assert(cpp_core::StatusCode::Control::kGetStateError == -404);
static_assert(cpp_core::StatusCode::Control::kSetStateError == -405);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please refactor into separate test file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, because this part of the lib is header only, there can not be any "real" tests, for the tests with static_assert to work after i put them in a separate file, the cpp-linux and cpp-windows would have to include that test file, for the tests to be executed, the tests would be executed by the compiler

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these static_asserts are essentially compiler checks

Katze719 and others added 2 commits March 20, 2026 07:34
Co-authored-by: Mqx <62719703+Mqxx@users.noreply.github.com>
- Added static assertions to enforce constraints on category and local codes, improving safety against negative values and overflow.
- Introduced a new test category, FakeCategory, to validate the computation of status codes, ensuring correct behavior across edge cases and preventing overflow.
- Updated test cases to cover various scenarios, enhancing the robustness of the status code generation logic.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants