Skip to content

Commit d5193f7

Browse files
committed
catalog: use NewReferencedDescriptorNotFoundError
This is easier to remember than having to wrap the error at each call site. Release note: None
1 parent d6f6de2 commit d5193f7

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

pkg/sql/catalog/errors.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
1717
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
1818
"github.com/cockroachdb/errors"
19+
"github.com/cockroachdb/redact"
1920
)
2021

2122
// ValidateName validates a name.
@@ -93,6 +94,10 @@ var ErrDescriptorNotFound = errors.New("descriptor not found")
9394
// descriptors referenced within another descriptor.
9495
var ErrReferencedDescriptorNotFound = errors.New("referenced descriptor not found")
9596

97+
func NewReferencedDescriptorNotFoundError(descType string, id descpb.ID) error {
98+
return errors.Wrapf(ErrReferencedDescriptorNotFound, "referenced %s ID %d", redact.SafeString(descType), errors.Safe(id))
99+
}
100+
96101
// ErrDescriptorWrongType is returned to signal that a descriptor was found but
97102
// that it wasn't of the expected type.
98103
var ErrDescriptorWrongType = errors.New("unexpected descriptor type")

pkg/sql/catalog/internal/validate/validate.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ var _ catalog.ValidationDescGetter = (*validationDescGetterImpl)(nil)
294294
func (vdg *validationDescGetterImpl) GetDescriptor(id descpb.ID) (catalog.Descriptor, error) {
295295
desc, found := vdg.descriptors[id]
296296
if !found || desc == nil {
297-
return nil, catalog.WrapDescRefErr(id, catalog.ErrReferencedDescriptorNotFound)
297+
return nil, catalog.NewReferencedDescriptorNotFoundError("object", id)
298298
}
299299
return desc, nil
300300
}
@@ -305,7 +305,7 @@ func (vdg *validationDescGetterImpl) GetDatabaseDescriptor(
305305
) (catalog.DatabaseDescriptor, error) {
306306
desc, found := vdg.descriptors[id]
307307
if !found || desc == nil {
308-
return nil, catalog.WrapDatabaseDescRefErr(id, catalog.ErrReferencedDescriptorNotFound)
308+
return nil, catalog.NewReferencedDescriptorNotFoundError("database", id)
309309
}
310310
return catalog.AsDatabaseDescriptor(desc)
311311
}
@@ -316,7 +316,7 @@ func (vdg *validationDescGetterImpl) GetSchemaDescriptor(
316316
) (catalog.SchemaDescriptor, error) {
317317
desc, found := vdg.descriptors[id]
318318
if !found || desc == nil {
319-
return nil, catalog.WrapSchemaDescRefErr(id, catalog.ErrReferencedDescriptorNotFound)
319+
return nil, catalog.NewReferencedDescriptorNotFoundError("schema", id)
320320
}
321321
return catalog.AsSchemaDescriptor(desc)
322322
}
@@ -327,7 +327,7 @@ func (vdg *validationDescGetterImpl) GetTableDescriptor(
327327
) (catalog.TableDescriptor, error) {
328328
desc, found := vdg.descriptors[id]
329329
if !found || desc == nil {
330-
return nil, catalog.WrapTableDescRefErr(id, catalog.ErrReferencedDescriptorNotFound)
330+
return nil, catalog.NewReferencedDescriptorNotFoundError("table", id)
331331
}
332332
return catalog.AsTableDescriptor(desc)
333333
}
@@ -338,7 +338,7 @@ func (vdg *validationDescGetterImpl) GetTypeDescriptor(
338338
) (catalog.TypeDescriptor, error) {
339339
desc, found := vdg.descriptors[id]
340340
if !found || desc == nil {
341-
return nil, catalog.WrapTypeDescRefErr(id, catalog.ErrReferencedDescriptorNotFound)
341+
return nil, catalog.NewReferencedDescriptorNotFoundError("type", id)
342342
}
343343
descriptor, err := catalog.AsTypeDescriptor(desc)
344344
if err != nil {
@@ -352,7 +352,7 @@ func (vdg *validationDescGetterImpl) GetFunctionDescriptor(
352352
) (catalog.FunctionDescriptor, error) {
353353
desc, found := vdg.descriptors[id]
354354
if !found || desc == nil {
355-
return nil, catalog.WrapFunctionDescRefErr(id, catalog.ErrReferencedDescriptorNotFound)
355+
return nil, catalog.NewReferencedDescriptorNotFoundError("function", id)
356356
}
357357
return catalog.AsFunctionDescriptor(desc)
358358
}

pkg/sql/catalog/nstree/catalog.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func (c Catalog) ValidateNamespaceEntry(key catalog.NameKey) error {
283283
// Compare the namespace entry with the referenced descriptor.
284284
desc := c.LookupDescriptor(ne.GetID())
285285
if desc == nil {
286-
return catalog.ErrReferencedDescriptorNotFound
286+
return catalog.NewReferencedDescriptorNotFoundError("schema", ne.GetID())
287287
}
288288
if desc.Dropped() {
289289
return catalog.ErrDescriptorDropped

0 commit comments

Comments
 (0)