package cli import ( "errors" "net" "net/url" "syscall" ) // unreachableError reports a connection that never reached a Pipelines server // in plain words, with how to start one. The transport error stays wrapped. type unreachableError struct { endpoint, reason, start string err error } func (e *unreachableError) Error() string { return "cannot reach the Pipelines server at " + e.endpoint + " (" + e.reason + ").\n" + "Is it running? " + e.start + ".\n" + "Using another server? Pass --url or set PIPELINES_URL." } func (e *unreachableError) Unwrap() error { return e.err } // serverUnreachable explains a refused or unresolvable server address. Other // errors (a rejected token, a protocol mismatch) are returned unchanged. func serverUnreachable(err error, endpoint string, installed bool) error { var dnsErr *net.DNSError reason := "" switch { case err == nil: return nil case errors.Is(err, syscall.ECONNREFUSED): reason = "connection refused" case errors.As(err, &dnsErr): reason = "unknown host " + dnsErr.Name default: return err } start := "Start it with `pipelines serve` (foreground), or install the service with ./install from the source checkout" if installed { start = "Start it with `pipelines start`; `pipelines doctor` and `pipelines logs` show why it stopped" } return &unreachableError{endpoint: displayEndpoint(endpoint), reason: reason, start: start, err: err} } // displayEndpoint drops credentials, query and fragment from a server URL. func displayEndpoint(endpoint string) string { u, err := url.Parse(endpoint) if err != nil || u.Host == "" { return "the configured address" } return (&url.URL{Scheme: u.Scheme, Host: u.Host, Path: u.Path}).String() }