Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: refine error message if table not found (#9509)

h3n4l63271420688c
diff --git a/backend/api/v1/sql_service_v2.go b/backend/api/v1/sql_service_v2.go
index 5eadb5b1c2..429c051988 100644
--- a/backend/api/v1/sql_service_v2.go
+++ b/backend/api/v1/sql_service_v2.go
@@ -113,7 +113,7 @@ func (s *SQLService) QueryV2(ctx context.Context, request *v1pb.QueryRequest) (*
// Get query span.
spans, err := base.GetQuerySpan(ctx, instance.Engine, statement, request.ConnectionDatabase, s.buildGetDatabaseMetadataFunc(instance))
if err != nil {
- return nil, err
+ return nil, errors.Wrapf(err, "failed to get query span")
}
if s.licenseService.IsFeatureEnabled(api.FeatureAccessControl) == nil {
diff --git a/backend/plugin/parser/base/interface.go b/backend/plugin/parser/base/interface.go
index b76929d732..7b04389380 100644
--- a/backend/plugin/parser/base/interface.go
+++ b/backend/plugin/parser/base/interface.go
@@ -7,6 +7,7 @@ import (
"github.com/pkg/errors"
+ parsererror "github.com/bytebase/bytebase/backend/plugin/parser/errors"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
@@ -188,6 +189,11 @@ func GetQuerySpan(ctx context.Context, engine storepb.Engine, statement, databas
for _, stmt := range statements {
result, err := f(ctx, stmt.Text, database, getMetadataFunc)
if err != nil {
+ // Try to unwrap the error to see if it's a ResourceNotFoundError to decrease the error noise.
+ var e *parsererror.ResourceNotFoundError
+ if errors.As(err, &e) {
+ return nil, e
+ }
return nil, err
}
results = append(results, result)
diff --git a/backend/plugin/parser/errors/resources.go b/backend/plugin/parser/errors/resources.go
new file mode 100644
index 0000000000..9402d0cabd
--- /dev/null
+++ b/backend/plugin/parser/errors/resources.go
@@ -0,0 +1,49 @@
+package parser
+
+import (
+ "fmt"
+ "strings"
+)
+
+// ResourceNotFoundError is returned when a resource is not found in the
+// processing the query.
+type ResourceNotFoundError struct {
+ Err error
+ Server *string
+ Database *string
+ Schema *string
+ Table *string
+ Column *string
+}
+
+func (e *ResourceNotFoundError) Error() string {
+ var resourceParts []string
+ if e.Server != nil {
+ resourceParts = append(resourceParts, fmt.Sprintf("server: %s", *e.Server))
+ }
+ if e.Database != nil {
+ resourceParts = append(resourceParts, fmt.Sprintf("database: %s", *e.Database))
+ }
+ if e.Schema != nil {
+ resourceParts = append(resourceParts, fmt.Sprintf("schema: %s", *e.Schema))
+ }
+ if e.Table != nil {
+ resourceParts = append(resourceParts, fmt.Sprintf("table: %s", *e.Table))
+ }
+ if e.Column != nil {
+ resourceParts = append(resourceParts, fmt.Sprintf("column: %s", *e.Column))
+ }
+ parts := []string{
+ fmt.Sprintf("resource not found: %s", strings.Join(resourceParts, ", ")),
+ }
+
+ if e.Err != nil {
+ parts = append(parts, fmt.Sprintf("err: %s", e.Err.Error()))
+ }
+
+ return strings.Join(parts, ", ")
+}
+
+func (e *ResourceNotFoundError) Unwrap() error {
+ return e.Err
+}
diff --git a/backend/plugin/parser/pg/v2/query_span_extractor.go b/backend/plugin/parser/pg/v2/query_span_extractor.go
index d5c0bc3357..abef6eac08 100644
--- a/backend/plugin/parser/pg/v2/query_span_extractor.go
+++ b/backend/plugin/parser/pg/v2/query_span_extractor.go
@@ -10,6 +10,7 @@ import (
pgquery "github.com/pganalyze/pg_query_go/v4"
+ parsererror "github.com/bytebase/bytebase/backend/plugin/parser/errors"
"github.com/bytebase/bytebase/backend/plugin/parser/pg"
"github.com/bytebase/bytebase/backend/plugin/parser/sql/ast"
pgrawparser "github.com/bytebase/bytebase/backend/plugin/parser/sql/engine/pg"
@@ -793,16 +794,28 @@ func (q *querySpanExtractor) findTableSchema(schemaName string, tableName string
if err != nil {
return nil, errors.Wrapf(err, "failed to get database metadata for database: %s", q.connectedDB)
}
+ if dbSchema == nil {
+ return nil, &parsererror.ResourceNotFoundError{
+ Database: &q.connectedDB,
+ }
+ }
if schemaName == "" {
schemaName = "public"
}
schema := dbSchema.GetSchema(schemaName)
if schema == nil {
- return nil, errors.Errorf("schema %s not found", schemaName)
+ return nil, &parsererror.ResourceNotFoundError{
+ Database: &q.connectedDB,
+ Schema: &schemaName,
+ }
}
table := schema.GetTable(tableName)
if table == nil {
- return nil, errors.Errorf("table %s.%s not found", schemaName, tableName)
+ return nil, &parsererror.ResourceNotFoundError{
+ Database: &q.connectedDB,
+ Schema: &schemaName,
+ Table: &tableName,
+ }
}
var columns []string