Improve listener failures and share configuration diagnostics
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -358,6 +359,9 @@ func TestServeListenAndConfigurationFailures(t *testing.T) {
|
||||
if outcome.Status != 1 || outcome.Cause == nil || output != "" || diagnostics == "" {
|
||||
t.Fatalf("serve failure: %+v %q %q", outcome, output, diagnostics)
|
||||
}
|
||||
if strings.Contains(args[len(args)-1], "invalid:address:syntax") && (!strings.Contains(diagnostics, "invalid:address:syntax") || !strings.Contains(diagnostics, "address")) {
|
||||
t.Fatalf("missing listener context: %q", diagnostics)
|
||||
}
|
||||
}
|
||||
writeNativeFile(t, "invalid.yml", "server:\n max_request_bytes: -1\n")
|
||||
outcome, _, diagnostics := executeNative(t, "serve", "--config=invalid.yml")
|
||||
@@ -379,6 +383,35 @@ func TestServeListenAndConfigurationFailures(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestServeListenerFailureDetails(t *testing.T) {
|
||||
nativePromptFixture(t, "http://127.0.0.1:1/v1")
|
||||
for _, tc := range []struct {
|
||||
name, category string
|
||||
cause error
|
||||
}{
|
||||
{"address", "address", &net.AddrError{Err: "private detail", Addr: "bad"}},
|
||||
{"dns", "address", &net.DNSError{Err: "private detail", Name: "bad"}},
|
||||
{"in use", "in use", fmt.Errorf("private detail: %w", syscall.EADDRINUSE)},
|
||||
{"permission", "permission", fmt.Errorf("private detail: %w", syscall.EACCES)},
|
||||
{"other", "listener setup", errors.New("private detail")},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
listen := func(context.Context, string, string) (net.Listener, error) { return nil, tc.cause }
|
||||
app, err := commandkit.New(commandkit.Identity{Name: "scriptorium"}, commandkit.NewCommand(commandkit.CommandSpec[serveInput]{Name: "serve", Flags: serveFlags(), Run: func(inv commandkit.Invocation, input serveInput) commandkit.Outcome {
|
||||
return serveWithListener(inv, input, listen)
|
||||
}}))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var output, diagnostics bytes.Buffer
|
||||
outcome := app.Execute(context.Background(), []string{"serve", "--config=app.yml", "--addr=127.0.0.1:1234", "--quiet"}, commandkit.Streams{Out: &output, Err: &diagnostics})
|
||||
if outcome.Status != 1 || !errors.Is(outcome.Cause, tc.cause) || output.Len() != 0 || !strings.Contains(diagnostics.String(), "127.0.0.1:1234") || !strings.Contains(diagnostics.String(), tc.category) || strings.Contains(diagnostics.String(), "private detail") {
|
||||
t.Fatalf("listener failure: %+v output=%q diagnostics=%q", outcome, output.String(), diagnostics.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestServeCaptureFailureDoesNotListen(t *testing.T) {
|
||||
nativePromptFixture(t, "http://127.0.0.1:1/v1")
|
||||
writeNativeFile(t, "framework.yml", `prompts:
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"go.maximumdirect.net/ckconfig"
|
||||
"go.maximumdirect.net/ckprompt"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
"go.maximumdirect.net/configkit"
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
@@ -75,17 +73,7 @@ func requestFromRunInput(input runInput) promptkit.RunRequest {
|
||||
|
||||
func runPrompt(invocation commandkit.Invocation, input runInput) commandkit.Outcome {
|
||||
result, err := loadApplication(invocation.Context, input.Selection, applicationInput{}, struct{}{})
|
||||
var sources []configkit.SourceInfo
|
||||
var records []configkit.Diagnostic
|
||||
if result != nil {
|
||||
sources, records = result.Sources(), result.Diagnostics()
|
||||
}
|
||||
var native *configkit.LoadError
|
||||
if errors.As(err, &native) && native != nil {
|
||||
sources, records = native.Sources(), native.Diagnostics()
|
||||
}
|
||||
diagnostics, explanation := ckconfig.AdaptDiagnostics("Application configuration:\n", sources, records)
|
||||
queueDiagnostics(invocation.Reporter(), diagnostics)
|
||||
explanation := applicationConfigurationDiagnostics(invocation.Reporter(), result, err)
|
||||
if err != nil {
|
||||
return requestFailure(err, explanation)
|
||||
}
|
||||
|
||||
@@ -4,12 +4,28 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
appconfig "gitea.maximumdirect.net/eric/scriptorium/internal/config"
|
||||
"go.maximumdirect.net/ckconfig"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
"go.maximumdirect.net/configkit"
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
func applicationConfigurationDiagnostics(reporter *commandkit.Reporter, result *configkit.Result[appconfig.Document], err error) commandkit.Explanation {
|
||||
var sources []configkit.SourceInfo
|
||||
var records []configkit.Diagnostic
|
||||
if result != nil {
|
||||
sources, records = result.Sources(), result.Diagnostics()
|
||||
}
|
||||
var native *configkit.LoadError
|
||||
if errors.As(err, &native) && native != nil {
|
||||
sources, records = native.Sources(), native.Diagnostics()
|
||||
}
|
||||
diagnostics, explanation := ckconfig.AdaptDiagnostics("Application configuration:\n", sources, records)
|
||||
queueDiagnostics(reporter, diagnostics)
|
||||
return explanation
|
||||
}
|
||||
|
||||
func completedRunOutcome(result *promptkit.RunResult) commandkit.Outcome {
|
||||
if result == nil {
|
||||
return commandkit.Failure("Prompt execution returned no result.").WithCause(errors.New("engine returned neither a result nor an error"))
|
||||
@@ -39,30 +55,26 @@ func requestFailure(err error, explanation commandkit.Explanation) commandkit.Ou
|
||||
message = "The operation was canceled."
|
||||
case errors.Is(err, context.DeadlineExceeded):
|
||||
message = "A request deadline expired."
|
||||
default:
|
||||
if ckconfig.ClassifyConfigurationError(err) == commandkit.OutcomeUsage {
|
||||
return commandkit.Usage("Invalid configuration.", explanation.Details...).WithCause(err)
|
||||
}
|
||||
switch {
|
||||
case errors.Is(err, promptkit.ErrCapacityExceeded):
|
||||
message = "Model backend capacity is exhausted."
|
||||
case errors.Is(err, promptkit.ErrLLMGenerate):
|
||||
message = "Model generation failed."
|
||||
case errors.Is(err, promptkit.ErrValidation):
|
||||
message = "The output validation operation failed."
|
||||
case errors.Is(err, promptkit.ErrPromptNotFound):
|
||||
message = "The selected prompt was not found."
|
||||
case errors.Is(err, promptkit.ErrProfileNotFound):
|
||||
message = "The selected profile was not found."
|
||||
case errors.Is(err, promptkit.ErrArtifactLoad):
|
||||
message = "Could not load request input artifacts."
|
||||
case errors.Is(err, promptkit.ErrPromptRender):
|
||||
message = "Could not render the selected prompt."
|
||||
case errors.Is(err, promptkit.ErrInvalidRequest):
|
||||
message = "The prompt request is invalid."
|
||||
case errors.Is(err, promptkit.ErrInvalidConfig):
|
||||
message = "Could not initialize PromptKit configuration."
|
||||
}
|
||||
case ckconfig.ClassifyConfigurationError(err) == commandkit.OutcomeUsage:
|
||||
return commandkit.Usage("Invalid configuration.", explanation.Details...).WithCause(err)
|
||||
case errors.Is(err, promptkit.ErrCapacityExceeded):
|
||||
message = "Model backend capacity is exhausted."
|
||||
case errors.Is(err, promptkit.ErrLLMGenerate):
|
||||
message = "Model generation failed."
|
||||
case errors.Is(err, promptkit.ErrValidation):
|
||||
message = "The output validation operation failed."
|
||||
case errors.Is(err, promptkit.ErrPromptNotFound):
|
||||
message = "The selected prompt was not found."
|
||||
case errors.Is(err, promptkit.ErrProfileNotFound):
|
||||
message = "The selected profile was not found."
|
||||
case errors.Is(err, promptkit.ErrArtifactLoad):
|
||||
message = "Could not load request input artifacts."
|
||||
case errors.Is(err, promptkit.ErrPromptRender):
|
||||
message = "Could not render the selected prompt."
|
||||
case errors.Is(err, promptkit.ErrInvalidRequest):
|
||||
message = "The prompt request is invalid."
|
||||
case errors.Is(err, promptkit.ErrInvalidConfig):
|
||||
message = "Could not initialize PromptKit configuration."
|
||||
}
|
||||
return commandkit.Failure(message, explanation.Details...).WithCause(err)
|
||||
}
|
||||
|
||||
@@ -6,12 +6,11 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"syscall"
|
||||
|
||||
httpadapter "gitea.maximumdirect.net/eric/scriptorium/internal/adapter/http"
|
||||
"gitea.maximumdirect.net/eric/scriptorium/internal/defaults"
|
||||
"go.maximumdirect.net/ckconfig"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
"go.maximumdirect.net/configkit"
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
@@ -21,17 +20,7 @@ func serveHTTP(invocation commandkit.Invocation, input serveInput) commandkit.Ou
|
||||
|
||||
func serveWithListener(invocation commandkit.Invocation, input serveInput, listen func(context.Context, string, string) (net.Listener, error)) commandkit.Outcome {
|
||||
result, err := loadApplication(invocation.Context, input.Selection, input.Application, struct{}{})
|
||||
var sources []configkit.SourceInfo
|
||||
var records []configkit.Diagnostic
|
||||
if result != nil {
|
||||
sources, records = result.Sources(), result.Diagnostics()
|
||||
}
|
||||
var native *configkit.LoadError
|
||||
if errors.As(err, &native) && native != nil {
|
||||
sources, records = native.Sources(), native.Diagnostics()
|
||||
}
|
||||
diagnostics, explanation := ckconfig.AdaptDiagnostics("Application configuration:\n", sources, records)
|
||||
queueDiagnostics(invocation.Reporter(), diagnostics)
|
||||
explanation := applicationConfigurationDiagnostics(invocation.Reporter(), result, err)
|
||||
if err != nil {
|
||||
return requestFailure(err, explanation)
|
||||
}
|
||||
@@ -51,7 +40,7 @@ func serveWithListener(invocation commandkit.Invocation, input serveInput, liste
|
||||
}
|
||||
listener, err := listen(invocation.Context, "tcp", settings.Addr)
|
||||
if err != nil {
|
||||
return commandkit.Failure("Could not listen for HTTP requests.").WithCause(err)
|
||||
return commandkit.Failure(fmt.Sprintf("Could not listen on %q: %s.", settings.Addr, listenerFailureCategory(err))).WithCause(err)
|
||||
}
|
||||
_ = invocation.Reporter().Info(fmt.Sprintf("Serving HTTP on %s.", listener.Addr()))
|
||||
closed := make(chan struct{})
|
||||
@@ -67,3 +56,18 @@ func serveWithListener(invocation commandkit.Invocation, input serveInput, liste
|
||||
}
|
||||
return commandkit.Failure("HTTP server stopped unexpectedly.").WithCause(errors.Join(serveErr, closeErr))
|
||||
}
|
||||
|
||||
func listenerFailureCategory(err error) string {
|
||||
var address *net.AddrError
|
||||
var dns *net.DNSError
|
||||
switch {
|
||||
case errors.As(err, &address), errors.As(err, &dns):
|
||||
return "invalid or unresolvable address"
|
||||
case errors.Is(err, syscall.EADDRINUSE):
|
||||
return "address already in use"
|
||||
case errors.Is(err, syscall.EACCES), errors.Is(err, syscall.EPERM):
|
||||
return "permission denied"
|
||||
default:
|
||||
return "listener setup failed"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user