domains / / tensorflow/tensorflow
Use consistent error handling
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.
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:
- Use
abslerror types withabsl::StrCatfor error messages - Perform validation checks early in functions
- Use appropriate error handling mechanisms for your function’s return type
- Return
StatusOr<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;
}