Improve listener failures and share configuration diagnostics
This commit is contained in:
@@ -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