Ensure errors are properly propagated to callers rather than handled locally with exits or fatal errors. Library functions should return error information to allow callers to make appropriate decisions about error handling and recovery.
Ensure errors are properly propagated to callers rather than handled locally with exits or fatal errors. Library functions should return error information to allow callers to make appropriate decisions about error handling and recovery.
Key principles:
llvm::Expected
instead of calling exit()
or llvm::report_fatal_error()
handleAllErrors()
instead of just converting errors to strings with toString()
Example of proper error propagation:
// Bad: Fatal error in library function
void parseDataAccessPerfTraces(StringRef File) {
auto BufferOrErr = MemoryBuffer::getFile(File);
if (auto EC = BufferOrErr.getError())
llvm::report_fatal_error(StringRef(Error)); // Don't do this
}
// Good: Return error to caller
std::error_code parseDataAccessPerfTraces(StringRef File) {
auto BufferOrErr = MemoryBuffer::getFile(File);
if (auto EC = BufferOrErr.getError())
return EC; // Let caller decide how to handle
// ... continue processing
return std::error_code{};
}
// Good: Handle multiple errors properly
if (!RSDOrErr) {
handleAllErrors(RSDOrErr.takeError(), [&](ErrorInfoBase &EIB) {
Ctx->emitError(EIB.message());
});
} // Instead of: toString(std::move(Err))
This approach enables better error recovery, testing, and allows different callers to handle errors appropriately for their context.
Enter the URL of a public GitHub repository