The error composite of go-composites. An
Error is the value carried by a
Result: it follows the
Null-Object grammar, so an "absence of error" is itself a real object
(NullError) rather than a bare Go nil. Result.HasError() is simply
!Error.IsNull(), which is why a fresh Result — whose error is a
NullError — reports no error.
go get github.com/go-composites/errorError.Interface embeds the reflective Object.Interface (Kind, RespondTo,
Methods) and adds:
| method | returns | notes |
|---|---|---|
Message() |
string |
the error text |
IsNull() |
bool |
true only for NullError; false for a real error |
Kind() |
string |
the interface kind, e.g. Error.Interface (from Object) |
RespondTo(name) |
bool |
whether the value exposes a method named name |
Methods() |
[]string |
the value's method names, via reflection |
New(message string) Interface builds a concrete error with the given message
(its IsNull() is false).
error/src/object(Object) — the reflective base shared by every error type. ProvidesKind,RespondToandMethods, both as package functions over anyinterface{}and as the methods of the embeddedObject.Interface.error/src/null(NullError) — the Null-Object error.New()yields an error whoseMessage()is""and whoseIsNull()istrue; it is the default error of a freshResult.error/src/method_not_implemented(MethodNotImplementedError) — a sentinel error.New(name string)formats"The method <name> is not implemented"; itsIsNull()isfalse.
package main
import (
"fmt"
Error "github.com/go-composites/error/src"
NullError "github.com/go-composites/error/src/null"
NotImplemented "github.com/go-composites/error/src/method_not_implemented"
)
func main() {
e := Error.New("key not found")
fmt.Println(e.Message()) // key not found
fmt.Println(e.IsNull()) // false
none := NullError.New()
fmt.Println(none.IsNull()) // true -> a Result holding this HasError() == false
nim := NotImplemented.New("Frobnicate")
fmt.Println(nim.Message()) // The method Frobnicate is not implemented
}BSD-3-Clause © the go-composites/error authors.
