Implement intelligent failure handling that makes contextual decisions about retry, cleanup, and error propagation based on the type and likelihood of recovery from specific failures.
Consider these key principles:
set -euo pipefail
in bash scriptsExample implementation:
#!/usr/bin/env bash
set -euo pipefail
retry_with_context() {
local url="$1"
local temp_file="$2"
local max_attempts=3
for attempt in $(seq 1 $max_attempts); do
if curl --fail --location "$url" > "$temp_file"; then
return 0
fi
# Get HTTP code to decide if retry makes sense
http_code=$(curl -s -o /dev/null -w "%{http_code}" "$url")
case $http_code in
404|403)
echo "Resource not found (HTTP $http_code), not retrying"
return 1
;;
429|5*)
echo "Temporary failure (HTTP $http_code), retrying in 2s..."
sleep 2
;;
esac
done
# Cleanup on final failure
rm -f "$temp_file"
return 1
}
This approach balances user experience with system reliability by making informed decisions about when failures are recoverable versus when they indicate fundamental problems that shouldn’t be retried.
Enter the URL of a public GitHub repository