Giter Site home page Giter Site logo

error-code's Introduction

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.

error-code's People

Contributors

vasil-pashov avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.