mirror of https://github.com/grafana/grafana.git
				
				
				
			Postgres: Improve invalid port specifier error during health check (#105536)
normalize error message in postgres invalid port specifier error
This commit is contained in:
		
							parent
							
								
									778563223b
								
							
						
					
					
						commit
						fcb1e9c9e5
					
				|  | @ -187,7 +187,7 @@ func (s *Service) generateConnectionString(dsInfo sqleng.DataSourceInfo) (string | ||||||
| 				var err error | 				var err error | ||||||
| 				port, err = strconv.Atoi(sp[1]) | 				port, err = strconv.Atoi(sp[1]) | ||||||
| 				if err != nil { | 				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) | 				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 | 				var err error | ||||||
| 				port, err = strconv.Atoi(dsInfo.URL[index+1:]) | 				port, err = strconv.Atoi(dsInfo.URL[index+1:]) | ||||||
| 				if err != nil { | 				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) | 				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) { | func (s *Service) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) { | ||||||
| 	dsHandler, err := s.getDSInfo(ctx, req.PluginContext) | 	dsHandler, err := s.getDSInfo(ctx, req.PluginContext) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return &backend.CheckHealthResult{Status: backend.HealthStatusError, Message: err.Error()}, nil | 		return sqleng.ErrToHealthCheckResult(err) | ||||||
| 	} | 	} | ||||||
| 	return dsHandler.CheckHealth(ctx, req) | 	return dsHandler.CheckHealth(ctx, req) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -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 | 			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) | 	detailBytes, marshalErr := json.Marshal(details) | ||||||
| 	if marshalErr != nil { | 	if marshalErr != nil { | ||||||
| 		return res, nil | 		return res, nil | ||||||
|  |  | ||||||
|  | @ -2,6 +2,7 @@ package sqleng | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"errors" | 	"errors" | ||||||
|  | 	"fmt" | ||||||
| 	"net" | 	"net" | ||||||
| 	"testing" | 	"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"}`), | 				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 { | 	for _, tt := range tests { | ||||||
| 		t.Run(tt.name, func(t *testing.T) { | 		t.Run(tt.name, func(t *testing.T) { | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue