From 8000daa9d9ef15dcedbaa0a0f3260e51246b9c73 Mon Sep 17 00:00:00 2001 From: Louis Thibault Date: Sat, 16 Nov 2024 01:33:48 -0500 Subject: [PATCH] Clean up status codes. --- examples/call/main.go | 20 +++++++++++--------- examples/echo/main.go | 2 +- std/system/system.go | 36 +++++++++++++++++++++++++++++++++++- ww.go | 2 +- 4 files changed, 48 insertions(+), 12 deletions(-) diff --git a/examples/call/main.go b/examples/call/main.go index 1485f78..32eef16 100644 --- a/examples/call/main.go +++ b/examples/call/main.go @@ -7,39 +7,41 @@ import ( "io" "log/slog" "os" + + "github.com/wetware/go/std/system" ) func main() { - if len(os.Args) < 1 { - slog.Error("expected 1 argument, got 0", - "status", 1) - os.Exit(1) + if nargs := len(os.Args); nargs < 1 { + slog.Error("wrong number of arguments", + "want", 1, + "got", nargs, + "args", os.Args) + os.Exit(system.StatusInvalidArgs) } f, err := os.Open(os.Args[0]) if err != nil { slog.Error("failed to open file", "reason", err, - "status", 2, "name", os.Args[0]) - os.Exit(2) + os.Exit(system.StatusInvalidArgs) } defer f.Close() var n int64 var status int if n, err = io.Copy(f, os.Stdin); err != nil { - status = 3 + status = system.StatusFailed err = fmt.Errorf("request: %w", err) } else if n, err = io.Copy(os.Stdout, f); err != nil { - status = 4 + status = system.StatusFailed err = fmt.Errorf("response: %w", err) } if err != nil { slog.Error("failed to read message from stdin", "reason", err, - "status", status, "read", n) } diff --git a/examples/echo/main.go b/examples/echo/main.go index c296da0..e7f8e20 100644 --- a/examples/echo/main.go +++ b/examples/echo/main.go @@ -45,7 +45,7 @@ func main() { if serve() { // Yield control to the scheduler. - os.Exit(system.StatusAwaiting) + os.Exit(system.StatusAsync) // The caller will intercept interface{ExitCode() uint32} and // check if e.ExitCode() == system.StatusAwaiting. // diff --git a/std/system/system.go b/std/system/system.go index 2b60aee..e00e31b 100644 --- a/std/system/system.go +++ b/std/system/system.go @@ -1,3 +1,37 @@ package system -const StatusAwaiting = 0x00ff0000 +const ( + StatusAsync = 0x00ff0000 + iota + StatusInvalidArgs + StatusFailed +) + +// type StatusCode uint + +// func (status StatusCode) Error() string { +// switch status { +// case StatusAsync: +// return "awaiting method calls" +// case StatusInvalidArgs: +// return "invalid arguments" +// case StatusFailed: +// return "application failed" +// } + +// return status.Unwrap().Error() +// } + +// func (status StatusCode) Unwrap() error { +// switch status.ExitCode() { +// case sys.ExitCodeContextCanceled: +// return context.Canceled +// case sys.ExitCodeDeadlineExceeded: +// return context.DeadlineExceeded +// } + +// return sys.NewExitError(status.ExitCode()) +// } + +// func (status StatusCode) ExitCode() uint32 { +// return uint32(status) +// } diff --git a/ww.go b/ww.go index 2d342a2..0ee9d93 100644 --- a/ww.go +++ b/ww.go @@ -75,7 +75,7 @@ func (env Env) Bind(ctx context.Context, r wazero.Runtime) error { switch e.ExitCode() { case 0: return nil - case guest.StatusAwaiting: + case guest.StatusAsync: return env.Net.ServeProc(ctx, p) } }