Skip to main content
2 of 2
made it more like a question
Abyx
  • 1.4k
  • 4
  • 14
  • 20

Error codes in a legacy C++ project

Background

I have a large C++ project which uses system error codes from errno.h, in C style.

int Cls::foo(A arg, O* out) { if (!validate(arg)) return -EINVAL; if (!out) return -EINVAL; return 0; } 

The code is not exception safe, and it's not covered by tests. (And this question is not about tests nor how to magically turn legacy code into non-legacy)

Existing functions of user-facing libraries (API) have to preserve their int return types, and the same values of error codes, for backward compatibility.

Some functions are performance-critical, so a refactoring shouldn't decrease their performance.

Toolchain supports C++20.

The problem

The errno codes aren't very helpful when it comes to error handling and manual debugging. E.g. half of return -Exxx; statements are EINVAL. Often it's not clear what actually happened.

It might be possible to add a new API, which would return another type, such as a better error code type (std::error_code), a monad-ish type (std::excepted), etc.

int Cls::foo(A arg, O* out) { return to_int_errno(NewCls::foo(arg, out)); } 

So the question is how to upgrade such legacy C++ project to have better error types/codes? What should I use for that?

Abyx
  • 1.4k
  • 4
  • 14
  • 20