Apply standardized error handling patterns across your codebase for improved reliability and debugging. For new code, use the absl
error types instead of tsl::errors
and include descriptive error messages with context.
Key practices:
absl
error types with absl::StrCat
for error messagesStatusOr<T>
for operations that can fail instead of raw values// Instead of:
return errors::InvalidArgument("Invalid shape: ", shape.DebugString());
// Use:
return absl::InvalidArgumentError(
absl::StrCat("Invalid shape: ", shape.DebugString()));
// For void functions, use the proper macro:
OP_REQUIRES(ctx, input.shape().dim_size(i) != 0,
absl::InvalidArgumentError("Invalid input: Dimension cannot be 0."));
// For functions that can fail, return StatusOr:
StatusOr<string> GetDeviceNameFromStreamDeviceName(const string& stream_name) {
if (!IsValidStreamName(stream_name)) {
return absl::InvalidArgumentError("Invalid stream device name");
}
return ParsedName;
}
Enter the URL of a public GitHub repository