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
@@ -0,0 +1,7 @@
package sqleng
import "errors"
var (
ErrInvalidPortSpecified error = errors.New("invalid port in host specifier")
)
@@ -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
@@ -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) {