Postgres: Improve invalid port specifier error during health check (#105536)

normalize error message in postgres invalid port specifier error
This commit is contained in:
Sriram
2025-05-16 11:51:12 +01:00
committed by GitHub
parent 778563223b
commit fcb1e9c9e5
4 changed files with 26 additions and 3 deletions

View File

@ -187,7 +187,7 @@ func (s *Service) generateConnectionString(dsInfo sqleng.DataSourceInfo) (string
var err error
port, err = strconv.Atoi(sp[1])
if err != nil {
return "", fmt.Errorf("invalid port in host specifier %q: %w", sp[1], err)
return "", fmt.Errorf("%w %q: %w", sqleng.ErrInvalidPortSpecified, sp[1], err)
}
logger.Debug("Generating connection string with network host/port pair", "host", host, "port", port)
@ -200,7 +200,7 @@ func (s *Service) generateConnectionString(dsInfo sqleng.DataSourceInfo) (string
var err error
port, err = strconv.Atoi(dsInfo.URL[index+1:])
if err != nil {
return "", fmt.Errorf("invalid port in host specifier %q: %w", dsInfo.URL[index+1:], err)
return "", fmt.Errorf("%w %q: %w", sqleng.ErrInvalidPortSpecified, dsInfo.URL[index+1:], err)
}
logger.Debug("Generating ipv6 connection string with network host/port pair", "host", host, "port", port)
@ -260,7 +260,7 @@ func (t *postgresQueryResultTransformer) TransformQueryError(_ log.Logger, err e
func (s *Service) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
dsHandler, err := s.getDSInfo(ctx, req.PluginContext)
if err != nil {
return &backend.CheckHealthResult{Status: backend.HealthStatusError, Message: err.Error()}, nil
return sqleng.ErrToHealthCheckResult(err)
}
return dsHandler.CheckHealth(ctx, req)
}

View File

@ -0,0 +1,7 @@
package sqleng
import "errors"
var (
ErrInvalidPortSpecified error = errors.New("invalid port in host specifier")
)

View File

@ -81,6 +81,12 @@ func ErrToHealthCheckResult(err error) (*backend.CheckHealthResult, error) {
details["verboseMessage"] = pqErr.Message
}
}
if errors.Is(err, ErrInvalidPortSpecified) {
res.Message = fmt.Sprintf("Connection string error: %s", ErrInvalidPortSpecified.Error())
if unwrappedErr := errors.Unwrap(err); unwrappedErr != nil {
details["verboseMessage"] = unwrappedErr.Error()
}
}
detailBytes, marshalErr := json.Marshal(details)
if marshalErr != nil {
return res, nil

View File

@ -2,6 +2,7 @@ package sqleng
import (
"errors"
"fmt"
"net"
"testing"
@ -48,6 +49,15 @@ func TestErrToHealthCheckResult(t *testing.T) {
JSONDetails: []byte(`{"errorDetailsLink":"https://grafana.com/docs/grafana/latest/datasources/postgres","verboseMessage":"internal server error"}`),
},
},
{
name: "invalid port specifier error",
err: fmt.Errorf("%w %q: %w", ErrInvalidPortSpecified, `"foo.bar.co"`, errors.New(`strconv.Atoi: parsing "foo.bar.co": invalid syntax`)),
want: &backend.CheckHealthResult{
Status: backend.HealthStatusError,
Message: "Connection string error: invalid port in host specifier",
JSONDetails: []byte(`{"errorDetailsLink":"https://grafana.com/docs/grafana/latest/datasources/postgres","verboseMessage":"invalid port in host specifier \"\\\"foo.bar.co\\\"\": strconv.Atoi: parsing \"foo.bar.co\": invalid syntax"}`),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {