Skip to content

Error Type

error is a built-in type used to represent error conditions in Harneet, following Go's error handling patterns.

Overview

  • Creation: Error("error message")
  • Type: error
  • Usage:
  • Returned from functions that can fail
  • Used in tuple returns: (result, error)
  • Supports error chaining and wrapping

Basic Usage

Creating Errors

1
2
3
4
5
6
// Simple error
var err = Error("something went wrong")

// Error with formatted message
var name = "test"
var err2 = Error(`Failed to process "${name}"`)

Error Handling

1
2
3
4
5
var result, err = someOperation()
if err != None {
    // Handle error
    print("Error:", err.message())
}

Error Methods

message()

Returns the error message string:

var err = Error("file not found")
print(err.message())  // "file not found"

unwrap()

Returns the error message or panics if the error is None:

1
2
3
4
var err = Error("oops")
print(err.unwrap())  // "oops"

// var safe = None.unwrap()  // This would panic

isError()

Returns true if the value is an error:

1
2
3
var err = Error("failed")
print(err.isError())  // true
print(None.isError()) // false

Error Handling Patterns

Function That Returns Error

1
2
3
4
5
6
function divide(a float64, b float64) (float64, error) {
    if b == 0 {
        return 0, Error("division by zero")
    }
    return a / b, None
}

Error Propagation

function process() (result, error) {
    var x, err = operation1()
    if err != None {
        return None, err  // Early return on error
    }

    var y, err2 = operation2(x)
    if err2 != None {
        return None, Error("operation2 failed: " + err2.message())
    }

    return y, None
}

Best Practices

  1. Always check errors - Never ignore returned errors
  2. Provide context - When wrapping errors, add context
  3. Use descriptive messages - Make error messages helpful for debugging
  4. Return None for success - Use None to indicate no error
  5. Document error conditions - In function documentation, specify possible error returns

Common Error Patterns

Custom Error Types

function FileNotFoundError(filename string) error {
    return Error(`file not found: ${filename}`)
}

function processFile(filename string) error {
    if !fileExists(filename) {
        return FileNotFoundError(filename)
    }
    // Process file
    return None
}

Error Chaining

1
2
3
4
5
6
7
function process() (result, error) {
    var data, err = readData()
    if err != None {
        return None, Error("failed to process: " + err.message())
    }
    // ...
}

Fatal Errors with fatal

For unrecoverable situations where the program must abort immediately, use the global fatal builtin. It behaves like a panic:

  • Available in the global scope (no import required)
  • Prints a clear FATAL: message with a stack trace
  • Terminates the program with a non-zero exit code
function main() {
    var config, err = loadConfig()
    if err != None {
        fatal("failed to load config: " + err.message())
    }

    if !db.IsHealthy() {
        fatal("database is not healthy; aborting")
    }
}

The fatal builtin accepts any value:

  • If you pass a string, it is used as-is.
  • If you pass an error or ErrorValue, its message is used.
  • Any other value is converted to a string using the same formatting rules as fmt.

Once fatal is called, execution does not continue.