Skip to content

sql: flattening error objects into other errors erases telemetry data and user hints #42510

@knz

Description

@knz

I found this while looking for something else:

 func NewInvalidSchemaDefinitionError(err error) error {
       return pgerror.New(pgcode.InvalidSchemaDefinition, err.Error())
 }

By flattening the error with err.Error() this constructor strips the error object of all its details, including stack traces but also user-directed hints, links to documentation issues and telemetry data.

Other examples (non exhaustive):

execute.go:                     return nil, pgerror.New(pgcode.WrongObjectType, err.Error())
export.go:                      return nil, pgerror.New(pgcode.InvalidParameterValue, err.Error())
rowexec/hashjoiner.go:                                  err = pgerror.Wrapf(addErr, pgcode.OutOfMemory, "while spilling: %v", err)
set_var.go:             return wrapSetVarError("statement_timeout", s, "%v", err)

There are multiple ways to fix it:

  • prefer errors.Wrap[f] when no need to add a pg code
  • use pgerror.WithCandidateCode when need to add a pg code if there wasn't one already to start with
  • use errors.Combine or errors.WithSecondaryError to combine errors together

I am fixing the first example at the top in #42509, but the other cases remain to be fixed.

cc @cockroachdb/sql-execution

Metadata

Metadata

Labels

A-error-handlingError messages, error propagations/annotationsC-bugCode not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.T-sql-foundationsSQL Foundations Team (formerly SQL Schema + SQL Sessions)T-sql-queriesSQL Queries Team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions