Skip to content

Small library used to wrap up status codes and messages from functions. Used as substitute for C++ exceptions.

License

Notifications You must be signed in to change notification settings

vasil-pashov/error-code

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

error-code

CMake workflow

Small library used to wrap up status codes and messages from functions. Used as substitute for C++ exceptions.

The struct EC::ErrorCode has two main components the status of the error and an optional message which describes the error. Status code with value 0 indicates that there are no errors and EC::ErrorCode::hasError() will return false.

Example usage

EC::ErrorCode foo(int n) {
  if(n < 0) {
    // Set the status code to 1, which indicates an error. Add message with string formating. 
    return EC::ErrorCode(1, "An error has occured. Expected number larger than 0, got %d.", n);
  } else if(n > 100) {
    // If the status code was not set explicitly in the constructor it will take the value of -1.
    // This is equivalent to EC::ErrorCode(-1, "Another error has occured");
    return EC::ErrorCode("Another error has occured");
  } else if(n > 50) {
    // Call a function which returns an error code, check the status and if it indicates an error
    // return from the function.
    const EC::ErrorCode err = bar(n);
    if(err.hasError()) {
      return err;
    }
  } else if(n > 25) {
    // Equivalent to the previous case, but the checking of the status and the return are wrapped
    // in a macro for convenience.
    RETURN_ON_ERROR_CODE(baz(n));
  }
  // The default constructor initializes object with status code 0, i.e. no error. EC::ErrorCode::hasError()
  // will return false.
  return EC::ErrorCode();
}

CMake options

  • ERROR_CODE_WITH_TESTS - Builds tests for the project. Currently doctest is used for testing. This will try to find doctest via find_package, if it fails it will download doctest via FetchContent. Default is OFF.
  • ERROR_CODE_WITH_INSTALL - Generate install target. Default is OFF.

About

Small library used to wrap up status codes and messages from functions. Used as substitute for C++ exceptions.

Resources

License

Stars

Watchers

Forks

Packages

No packages published