cat / 012
clone

a spreadsheet for the terminal: lotus 1-2-3 looks, google sheets keys and formulas, one go binary

cat/module-path
012 / internal / jev / check.go
check.go 1.9K
1package jev23import (4 "context"5 "errors"6 "fmt"7 "net"8 "strings"9 "syscall"1011 typesafe "github.com/FelineStateMachine/typesafe-go"12)1314// CheckQuestion is the question Check asks: fixed, trivial and about15// nothing the user owns, so checking a key costs one small request and16// sends no data.17const CheckQuestion = "Is this arithmetic correct?"1819// checkState is the value CheckQuestion is asked about.20const checkState = "2 + 2 = 4"2122// Check asks the service CheckQuestion with client, to see that its key23// works. The error says why not, in words for the context line, and24// never holds the key.25func Check(ctx context.Context, client Client) error {26 resp, err := client.SystemOne(ctx, typesafe.SystemOneRequest{27 State: map[string]any{"value": checkState},28 Questions: map[string]typesafe.Question{"answer": typesafe.NoulQuestion{Instructions: CheckQuestion}},29 })30 if err != nil {31 return checkError(err)32 }33 if _, err := resp.Noul("answer"); err != nil {34 return checkError(err)35 }36 return nil37}3839// checkError words err for the context line.40func checkError(err error) error {41 var (42 api *typesafe.APIError43 errno syscall.Errno44 dns *net.DNSError45 op *net.OpError46 )47 switch {48 case errors.As(err, &api) && (api.StatusCode == 401 || api.StatusCode == 403):49 return fmt.Errorf("the service refused the key (HTTP %d)", api.StatusCode)50 case errors.As(err, &api):51 return fmt.Errorf("the service answered HTTP %d", api.StatusCode)52 case errors.Is(err, context.DeadlineExceeded):53 return errors.New("the service didn't answer in time")54 case errors.As(err, &errno):55 return fmt.Errorf("couldn't reach the service: %v", errno)56 case errors.As(err, &dns):57 return fmt.Errorf("couldn't reach the service: no such host %s", dns.Name)58 case errors.As(err, &op):59 return fmt.Errorf("couldn't reach the service: %v", op.Err)60 }61 return errors.New(strings.TrimPrefix(err.Error(), "typesafe: "))62}