🐚 Error Handling in TuskLang - Bash Guide

Bash Documentation

Error Handling in TuskLang - Bash Guide

⚠️ Revolutionary Error Handling Configuration

Error handling in TuskLang transforms your configuration files into resilient, fault-tolerant systems. No more silent failures or cryptic error messagesβ€”everything lives in your TuskLang configuration with dynamic error detection, intelligent recovery, and comprehensive error reporting.

> "We don't bow to any king" – TuskLang error handling breaks free from traditional error management constraints and brings modern resilience to your Bash applications.

πŸš€ Core Error Handling Directives

Basic Error Handling Setup

#error-handling: enabled              # Enable error handling
#error-enabled: true                 # Alternative syntax
#error-logging: true                 # Enable error logging
#error-recovery: true                # Enable error recovery
#error-notification: true            # Enable error notifications
#error-retry: true                   # Enable retry mechanisms

Advanced Error Handling Configuration

#error-classification: true          # Enable error classification
#error-escalation: true              # Enable error escalation
#error-circuit-breaker: true         # Enable circuit breaker
#error-timeout: 30                   # Error timeout (seconds)
#error-max-retries: 3                # Maximum retry attempts
#error-backoff: exponential          # Retry backoff strategy

πŸ”§ Bash Error Handling Implementation

Basic Error Handler

#!/bin/bash

Load error handling configuration

source <(tsk load error-handling.tsk)

Error handling configuration

ERROR_ENABLED="${error_enabled:-true}" ERROR_LOGGING="${error_logging:-true}" ERROR_RECOVERY="${error_recovery:-true}" ERROR_NOTIFICATION="${error_notification:-true}" ERROR_RETRY="${error_retry:-true}"

Error handler

class ErrorHandler { constructor() { this.enabled = ERROR_ENABLED this.logging = ERROR_LOGGING this.recovery = ERROR_RECOVERY this.notification = ERROR_NOTIFICATION this.retry = ERROR_RETRY this.errors = [] this.stats = { errors_caught: 0, errors_recovered: 0, errors_escalated: 0 } } handleError(error, context = {}) { if (!this.enabled) return this.stats.errors_caught++ // Log error if (this.logging) { this.logError(error, context) } // Attempt recovery if (this.recovery) { const recovered = this.attemptRecovery(error, context) if (recovered) { this.stats.errors_recovered++ return true } } // Send notification if (this.notification) { this.sendNotification(error, context) } // Escalate if needed if (this.shouldEscalate(error)) { this.escalateError(error, context) this.stats.errors_escalated++ } return false } logError(error, context) { const errorEntry = { timestamp: new Date().toISOString(), error: error.message, context, stack: error.stack } this.errors.push(errorEntry) this.writeErrorLog(errorEntry) } attemptRecovery(error, context) { // Implementation for error recovery return false } sendNotification(error, context) { // Implementation for error notification } shouldEscalate(error) { // Implementation for escalation logic return false } escalateError(error, context) { // Implementation for error escalation } writeErrorLog(errorEntry) { // Write to error log file } getStats() { return { ...this.stats } } getErrors() { return [...this.errors] } }

Initialize error handler

const errorHandler = new ErrorHandler()

Dynamic Error Detection

#!/bin/bash

Dynamic error detection

detect_and_handle_error() { local command="$1" local context="${2:-}" # Execute command and capture error local output local exit_code output=$(eval "$command" 2>&1) exit_code=$? if [[ $exit_code -ne 0 ]]; then handle_error "$exit_code" "$output" "$context" return $exit_code fi echo "$output" return 0 }

handle_error() { local exit_code="$1" local error_message="$2" local context="${3:-}" # Classify error local error_type=$(classify_error "$exit_code" "$error_message") # Log error log_error "$error_type" "$error_message" "$context" # Attempt recovery if attempt_error_recovery "$error_type" "$context"; then echo "βœ“ Error recovered: $error_type" return 0 fi # Send notification send_error_notification "$error_type" "$error_message" "$context" # Escalate if needed if should_escalate_error "$error_type"; then escalate_error "$error_type" "$error_message" "$context" fi return 1 }

classify_error() { local exit_code="$1" local error_message="$2" case "$exit_code" in 1) echo "general_error" ;; 2) echo "misuse_error" ;; 126) echo "command_not_executable" ;; 127) echo "command_not_found" ;; 128) echo "invalid_argument" ;; 130) echo "interrupted" ;; 137) echo "killed" ;; *) if echo "$error_message" | grep -q "permission denied"; then echo "permission_error" elif echo "$error_message" | grep -q "no space left"; then echo "disk_space_error" elif echo "$error_message" | grep -q "connection refused"; then echo "connection_error" else echo "unknown_error" fi ;; esac }

Intelligent Error Recovery

#!/bin/bash

Intelligent error recovery

attempt_error_recovery() { local error_type="$1" local context="${2:-}" case "$error_type" in "permission_error") attempt_permission_recovery "$context" ;; "disk_space_error") attempt_disk_space_recovery "$context" ;; "connection_error") attempt_connection_recovery "$context" ;; "command_not_found") attempt_command_recovery "$context" ;; *) return 1 ;; esac }

attempt_permission_recovery() { local context="$1" echo "Attempting permission error recovery..." # Try to fix permissions if [[ -n "$context" ]]; then chmod +x "$context" 2>/dev/null if [[ $? -eq 0 ]]; then echo "βœ“ Permission fixed for: $context" return 0 fi fi return 1 }

attempt_disk_space_recovery() { local context="$1" echo "Attempting disk space error recovery..." # Clean up temporary files find /tmp -type f -atime +7 -delete 2>/dev/null find /var/tmp -type f -atime +7 -delete 2>/dev/null # Clear package cache if command -v apt-get >/dev/null 2>&1; then apt-get clean >/dev/null 2>&1 fi echo "βœ“ Disk space cleanup completed" return 0 }

attempt_connection_recovery() { local context="$1" echo "Attempting connection error recovery..." # Wait and retry sleep 5 # Test connection if ping -c 1 google.com >/dev/null 2>&1; then echo "βœ“ Connection recovered" return 0 fi return 1 }

attempt_command_recovery() { local context="$1" echo "Attempting command not found recovery..." # Try to install missing command if command -v apt-get >/dev/null 2>&1; then apt-get update >/dev/null 2>&1 apt-get install -y "$context" >/dev/null 2>&1 if [[ $? -eq 0 ]]; then echo "βœ“ Command installed: $context" return 0 fi fi return 1 }

Retry Mechanisms

#!/bin/bash

Retry mechanisms

retry_command() { local command="$1" local max_retries="${error_max_retries:-3}" local backoff_strategy="${error_backoff:-exponential}" local attempt=1 local delay=1 while [[ $attempt -le $max_retries ]]; do echo "Attempt $attempt of $max_retries: $command" if eval "$command"; then echo "βœ“ Command succeeded on attempt $attempt" return 0 fi if [[ $attempt -eq $max_retries ]]; then echo "βœ— Command failed after $max_retries attempts" return 1 fi # Calculate delay based on backoff strategy case "$backoff_strategy" in "exponential") delay=$((delay * 2)) ;; "linear") delay=$((delay + 1)) ;; "fixed") delay=1 ;; esac echo "Retrying in ${delay}s..." sleep "$delay" attempt=$((attempt + 1)) done }

exponential_backoff() { local command="$1" local max_retries="${2:-3}" local base_delay="${3:-1}" local attempt=1 local delay="$base_delay" while [[ $attempt -le $max_retries ]]; do if eval "$command"; then return 0 fi if [[ $attempt -eq $max_retries ]]; then return 1 fi echo "Retrying in ${delay}s (attempt $attempt/$max_retries)..." sleep "$delay" delay=$((delay * 2)) attempt=$((attempt + 1)) done }

Circuit Breaker Pattern

#!/bin/bash

Circuit breaker pattern

circuit_breaker() { local operation="$1" local command="$2" local failure_threshold="${3:-5}" local timeout="${4:-60}" local state_file="/tmp/circuit_breaker_${operation}.json" # Check circuit breaker state if [[ -f "$state_file" ]]; then local state=$(jq -r '.state' "$state_file" 2>/dev/null) local last_failure=$(jq -r '.last_failure' "$state_file" 2>/dev/null) local failure_count=$(jq -r '.failure_count' "$state_file" 2>/dev/null) case "$state" in "open") # Check if timeout has passed local current_time=$(date +%s) local failure_time=$(date -d "$last_failure" +%s 2>/dev/null || echo 0) if [[ $((current_time - failure_time)) -gt $timeout ]]; then echo "Circuit breaker timeout passed, attempting operation..." set_circuit_breaker_state "$operation" "half_open" else echo "Circuit breaker is open, operation blocked" return 1 fi ;; "half_open") echo "Circuit breaker is half-open, attempting operation..." ;; "closed") echo "Circuit breaker is closed, proceeding with operation..." ;; esac else # Initialize circuit breaker set_circuit_breaker_state "$operation" "closed" fi # Execute command if eval "$command"; then # Success - close circuit breaker set_circuit_breaker_state "$operation" "closed" echo "βœ“ Operation succeeded, circuit breaker closed" return 0 else # Failure - update circuit breaker local current_failures=$((failure_count + 1)) if [[ $current_failures -ge $failure_threshold ]]; then set_circuit_breaker_state "$operation" "open" echo "βœ— Circuit breaker opened due to $current_failures failures" else set_circuit_breaker_state "$operation" "closed" "$current_failures" echo "βœ— Operation failed, failure count: $current_failures" fi return 1 fi }

set_circuit_breaker_state() { local operation="$1" local state="$2" local failure_count="${3:-0}" local state_file="/tmp/circuit_breaker_${operation}.json" cat > "$state_file" << EOF { "operation": "$operation", "state": "$state", "failure_count": $failure_count, "last_failure": "$(date -Iseconds)", "last_updated": "$(date -Iseconds)" } EOF }

🎯 Real-World Configuration Examples

Complete Error Handling Configuration

error-handling-config.tsk

error_handling_config: enabled: true logging: true recovery: true notification: true retry: true

#error-handling: enabled #error-enabled: true #error-logging: true #error-recovery: true #error-notification: true #error-retry: true

#error-classification: true #error-escalation: true #error-circuit-breaker: true #error-timeout: 30 #error-max-retries: 3 #error-backoff: exponential

#error-config:

logging:

enabled: true

level: "error"

file: "/var/log/errors.log"

rotation: true

retention: "30d"

recovery:

enabled: true

strategies:

- "retry"

- "fallback"

- "circuit_breaker"

notification:

enabled: true

channels:

slack:

webhook: "${SLACK_WEBHOOK}"

channel: "#errors"

email:

recipients: ["ops@example.com"]

smtp_server: "smtp.example.com"

retry:

enabled: true

max_attempts: 3

backoff_strategy: "exponential"

base_delay: 1

classification:

enabled: true

types:

- "permission_error"

- "disk_space_error"

- "connection_error"

- "command_not_found"

escalation:

enabled: true

threshold: 5

timeout: 300

circuit_breaker:

enabled: true

failure_threshold: 5

timeout: 60

operations:

- "database_connection"

- "api_call"

- "file_operation"

Multi-Level Error Handling

multi-level-error-handling.tsk

multi_level_error_handling: levels: - name: application enabled: true recovery: true - name: system enabled: true recovery: true - name: network enabled: true recovery: false

#error-application: enabled #error-system: enabled #error-network: enabled

#error-config:

levels:

application:

enabled: true

recovery: true

notification: true

system:

enabled: true

recovery: true

notification: true

network:

enabled: true

recovery: false

notification: true

🚨 Troubleshooting Error Handling

Common Issues and Solutions

1. Error Recovery Issues

Debug error recovery

debug_error_recovery() { local error_type="$1" local context="${2:-}" echo "Debugging error recovery for: $error_type" attempt_error_recovery "$error_type" "$context" }

2. Circuit Breaker Issues

Debug circuit breaker

debug_circuit_breaker() { local operation="$1" echo "Debugging circuit breaker for: $operation" local state_file="/tmp/circuit_breaker_${operation}.json" if [[ -f "$state_file" ]]; then cat "$state_file" else echo "No circuit breaker state found for: $operation" fi }

πŸ”’ Security Best Practices

Error Handling Security Checklist

Security validation

validate_error_handling_security() { echo "Validating error handling security configuration..." # Check error log security if [[ "${error_log_security}" == "true" ]]; then echo "βœ“ Error log security enabled" else echo "⚠ Error log security not enabled" fi # Check sensitive data filtering if [[ "${error_sensitive_data_filtering}" == "true" ]]; then echo "βœ“ Sensitive data filtering enabled" else echo "⚠ Sensitive data filtering not enabled" fi # Check error notification security if [[ "${error_notification_security}" == "true" ]]; then echo "βœ“ Error notification security enabled" else echo "⚠ Error notification security not enabled" fi }

πŸ“ˆ Performance Optimization Tips

Error Handling Performance Checklist

Performance validation

validate_error_handling_performance() { echo "Validating error handling performance configuration..." # Check retry attempts local max_retries="${error_max_retries:-3}" if [[ "$max_retries" -le 5 ]]; then echo "βœ“ Reasonable max retries ($max_retries)" else echo "⚠ High max retries may impact performance ($max_retries)" fi # Check timeout local timeout="${error_timeout:-30}" # seconds if [[ "$timeout" -le 60 ]]; then echo "βœ“ Reasonable timeout ($timeout s)" else echo "⚠ Long timeout may impact responsiveness ($timeout s)" fi # Check circuit breaker if [[ "${error_circuit_breaker}" == "true" ]]; then echo "βœ“ Circuit breaker enabled" else echo "⚠ Circuit breaker not enabled" fi }

🎯 Next Steps

- Error Analysis: Learn about advanced error analysis - Error Visualization: Create error visualization dashboards - Error Correlation: Implement error correlation and alerting - Error Compliance: Set up error compliance and auditing

---

Error handling transforms your TuskLang configuration into a resilient, fault-tolerant system. It brings modern error management to your Bash applications with dynamic detection, intelligent recovery, and comprehensive error reporting!