๐Ÿน Error Handling in TuskLang - Go Guide

Go Documentation

Error Handling in TuskLang - Go Guide

๐Ÿ›‘ No Excuses: Bulletproof Error Handling

TuskLang doesnโ€™t bow to any kingโ€”especially not to silent failures or cryptic stack traces. This guide shows you how to handle errors in TuskLang-powered Go projects with clarity, confidence, and zero tolerance for ambiguity.

๐Ÿ“‹ Table of Contents

- Types of Errors - Config Parsing Errors - Runtime Errors - Go Integration - Error Patterns - Best Practices

โš ๏ธ Types of Errors

- Syntax Errors: Malformed config, missing brackets, invalid keys - Validation Errors: Required fields missing, type mismatches - Runtime Errors: Failed queries, missing files, permission denied - Operator Errors: @query, @env, @file failures

๐Ÿ“ Config Parsing Errors

TuskLang Example

// TuskLang - Syntax error
[database
url: "postgres://localhost"  # Missing closing bracket
// Go - Handling parse errors
config, err := tusklang.LoadConfig("broken.tsk")
if err != nil {
    log.Fatalf("Config parse error: %v", err)
}

Validation Error Example

// TuskLang - Validation error
[api]
api_key: 12345  # Should be a string, not an int
// Go - Handling validation errors
err = config.Validate()
if err != nil {
    log.Printf("Validation failed: %v", err)
}

๐Ÿ’ฅ Runtime Errors

Operator Failures

// TuskLang - Operator error
[metrics]
user_count: @query("SELECT COUNT(*) FROM missing_table")  # Table does not exist
// Go - Handling operator errors
userCount, err := config.GetInt("user_count")
if err != nil {
    log.Printf("Query failed: %v", err)
    // Fallback or alert
}

File/Env Errors

// TuskLang - File error
[ssl]
private_key: @file.read("/missing/key.pem")
// Go - Handling file errors
key, err := config.GetString("private_key")
if err != nil {
    log.Printf("File read error: %v", err)
}

๐Ÿ”— Go Integration

Idiomatic Error Handling

// Go - Idiomatic error handling
val, err := config.GetString("api_key")
if err != nil {
    log.Printf("Missing api_key: %v", err)
    // Provide default or exit
}

Custom Error Types

type ConfigError struct {
    Section string
    Key     string
    Err     error
}

func (e *ConfigError) Error() string { return fmt.Sprintf("[%s] %s: %v", e.Section, e.Key, e.Err) }

// Usage return &ConfigError{Section: "database", Key: "url", Err: err}

Error Wrapping

// Go - Error wrapping
if err != nil {
    return fmt.Errorf("failed to load config: %w", err)
}

๐Ÿงฉ Error Patterns

- Always check errors from config accessors - Use descriptive error messages - Wrap errors with context - Log and alert on critical failures - Provide fallbacks for non-fatal errors

๐Ÿฅ‡ Best Practices

- Never ignore errorsโ€”handle or escalate - Use custom error types for clarity - Log all config and runtime errors - Fail fast on critical config issues - Document error cases in your code

---

TuskLang: No silent failures. No surprises. Just bulletproof error handling.