system: wrap op errors
All checks were successful
Test / Create distribution (push) Successful in 34s
Test / Sandbox (push) Successful in 1m51s
Test / Hakurei (push) Successful in 3m18s
Test / Hpkg (push) Successful in 3m41s
Test / Sandbox (race detector) (push) Successful in 4m7s
Test / Hakurei (race detector) (push) Successful in 5m18s
Test / Flake checks (push) Successful in 1m35s

This passes more information allowing for better error handling. This eliminates generic WrapErr from system.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2025-08-30 22:49:12 +09:00
parent ddb003e39b
commit f5abce9df5
11 changed files with 297 additions and 152 deletions

View File

@@ -1,6 +1,10 @@
package system
import (
"errors"
"net"
"os"
"hakurei.app/container"
)
@@ -14,9 +18,59 @@ func SetOutput(v container.Msg) {
}
}
func wrapErrSuffix(err error, a ...any) error {
// 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)):
return 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 msg.WrapErr(err, append(a, err)...)
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())
}
}
}