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
Error Handling
Error Methods
message()
Returns the error message string:
unwrap()
Returns the error message or panics if the error is None:
isError()
Returns true if the value is an error:
Error Handling Patterns
Function That Returns Error
Error Propagation
Best Practices
- Always check errors - Never ignore returned errors
- Provide context - When wrapping errors, add context
- Use descriptive messages - Make error messages helpful for debugging
- Return
Nonefor success - UseNoneto indicate no error - Document error conditions - In function documentation, specify possible error returns
Common Error Patterns
Custom Error Types
Error Chaining
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
The fatal builtin accepts any value:
- If you pass a
string, it is used as-is. - If you pass an
errororErrorValue, 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.