Skip to content

Commit

Permalink
Clean up status codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
lthibault committed Nov 16, 2024
1 parent c9f67e3 commit 8000daa
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
20 changes: 11 additions & 9 deletions examples/call/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion examples/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down
36 changes: 35 additions & 1 deletion std/system/system.go
Original file line number Diff line number Diff line change
@@ -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)
// }
2 changes: 1 addition & 1 deletion ww.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 8000daa

Please sign in to comment.