hakurei/system/output.go
Ophestra 9963e7eba2
Some checks failed
Test / Create distribution (push) Successful in 34s
Test / Hakurei (push) Failing after 1m5s
Test / Sandbox (push) Failing after 1m28s
Test / Hpkg (push) Failing after 1m53s
Test / Hakurei (race detector) (push) Failing after 2m8s
Test / Sandbox (race detector) (push) Failing after 3m33s
Test / Flake checks (push) Has been skipped
system: wrap op errors
This passes more information allowing for better error handling. This eliminates generic WrapErr from system.

Signed-off-by: Ophestra <cat@gensokyo.uk>
2025-08-30 23:27:22 +09:00

79 lines
1.5 KiB
Go

package system
import (
"errors"
"net"
"os"
"hakurei.app/container"
)
var msg container.Msg = new(container.DefaultMsg)
func SetOutput(v container.Msg) {
if v == nil {
msg = new(container.DefaultMsg)
} else {
msg = v
}
}
// OpError is returned by [I.Commit] and [I.Revert].
type OpError struct {
Op string
Err error
Message string
Revert bool
}
func (e *OpError) Unwrap() error { return e.Err }
func (e *OpError) Error() string {
if e.Message != "" {
return e.Message
}
switch {
case errors.As(e.Err, new(*os.PathError)),
errors.As(e.Err, new(*net.OpError)),
errors.As(e.Err, new(*container.StartError)):
return "cannot " + e.Err.Error()
default:
if !e.Revert {
return "cannot apply " + e.Op + ": " + e.Err.Error()
} else {
return "cannot revert " + e.Op + ": " + e.Err.Error()
}
}
}
// newOpError returns an [OpError] without a message string.
func newOpError(op string, err error, revert bool) error {
if err == nil {
return nil
}
return &OpError{op, err, "", revert}
}
// newOpErrorMessage returns an [OpError] with an overriding message string.
func newOpErrorMessage(op string, err error, message string, revert bool) error {
if err == nil {
return nil
}
return &OpError{op, err, message, revert}
}
func printJoinedError(println func(v ...any), fallback string, err error) {
var joinErr interface {
Unwrap() []error
error
}
if !errors.As(err, &joinErr) {
println(fallback, err)
} else {
for _, err = range joinErr.Unwrap() {
println(err.Error())
}
}
}