Add error handling utils (v1) #3
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds several interrelated classes for error-handling.
ErrorCodeprovides a mechanism for type-erasure of error code enumeration types, allowing for interoperability of different error codes. It stores an integer value and a pointer to an object of typeErrorCategory.ErrorCategoryis an abstract base class for specific error categories. Each derived class provides support for a unique error code enum type toErrorCode.Errorstores an error code along with information about the source location where the error occurred (filename and line number).Expected<T>is a wrapper that may contain an object of typeTor anError. It provides an alternative to traditional error handling mechanisms (such as exceptions and error codes) when used as a return type for operations which may fail. On success, the returned object contains the expected result. In case of failure, it instead contains an object that describes the error encountered.I'm posting this implementation for posterity, but note that there are some significant compatibility issues with CUDA code that I'd initially overlooked. Check out the (forthcoming) v2 implementation instead.
The fallback mechanism -- whereby invalid access to an
Expected<T>::value()causes an exception to be thrown -- doesn't work in device code (obviously, since exceptions aren't available on the device).The best you can do is something like
ErrorCategoryrelies on dynamic polymorphism which has significant limitations in CUDA code. Instances of derived error categories created on the host may not be safely passed to the device and vice versa. As an alternative,ErrorCodecould achieve runtime polymorphic behavior using a variant/visitor idiom, which has no such limitations. However, this makes extendingErrorCodemuch more intrusive -- you need to modify the class definition to plug in additional error code types.