Skip to content

grpc-labview ought to make better use of C++ exceptions #437

@bkeryan

Description

@bkeryan

The grpc-labview C++ codebase has a lot of duplicated error handling, and #436 adds more.

Instead of duplicating checks for failed casts, etc. in each exported function, the checks could be single-sourced:

  • Create a C++ exception class for an exception with an error code.
  • Change CastTo to throw an exception with the appropriate error code when it fails
  • Add a TryCastTo method for use cases that require casting to null, and update callers to use it when appropriate.
  • Add an exception dispatcher method like
    int32_t translateException() {
        try { 
            throw; 
        } catch (const MyExceptionWithErrorCode& e) {
            return e.code;
        } catch (const std::exception& e) {
            logException(e);
            return SOME_ERROR_CODE;
        }
    }
  • Add a try/catch block to each exported function that uses the exception dispatcher:
    try {
        ...
    } catch (const std::exception&) {
        return translateException();
    }
    It is possible to wrap this in a macro, but not necessary, since most of the logic is single sourced in the exception dispatcher function.

The code already does some of this, but only for invalid enum values.

Originally posted by @bkeryan in #436 (comment)

AB#3117529

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions