Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pgerror: use a different type for pgcodes #49694

Closed
knz opened this issue May 29, 2020 · 0 comments · Fixed by #49858
Closed

pgerror: use a different type for pgcodes #49694

knz opened this issue May 29, 2020 · 0 comments · Fixed by #49858
Assignees
Labels
A-sql-pgwire pgwire protocol issues. C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) E-easy Easy issue to tackle, requires little or no CockroachDB experience good first issue

Comments

@knz
Copy link
Contributor

knz commented May 29, 2020

tldr: we want to change the API signature of pgerror.Newf and pgerror.Wrapf and possibly other things that accept a pg error code as argument.

Background

As found in #49660, there was a mistake in the code

  stmt := "some string"
  return pgerror.Wrapf(err, "while running %s", stmt)

The second argument should be the pgcode, not the error string.

The Go type system does not catch this - the interface of Wrapf is (error, string, string, ...interface{})

It's too easy to mistakenly forget the pgcode in there.

We can't use type Code string or type Code = string because that doesn't block the error above.

Proposal

We can do this in package pgcode:

type Code struct {
  code string
}

func MakeCode(c string) Code { return Code{c} }

func (c Code) String() string { return c.code }

then change pgerror.Wrapf to take (error, pgcode.Code, string, ...interface{})

All the pre-defined code can be generated to use that struct instead of a simple string (the're a generation script already, so we can automate the conversion).

suggested by @dt @tbg

@knz knz added C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) E-easy Easy issue to tackle, requires little or no CockroachDB experience good first issue A-sql-pgwire pgwire protocol issues. labels May 29, 2020
@knz knz added this to Triage in BACKLOG, NO NEW ISSUES: SQL Execution via automation May 29, 2020
@asubiotto asubiotto moved this from Triage to [GENERAL BACKLOG] Enhancements/Features/Investigations in BACKLOG, NO NEW ISSUES: SQL Execution Jun 2, 2020
@jordanlewis jordanlewis removed this from [GENERAL BACKLOG] Enhancements/Features/Investigations in BACKLOG, NO NEW ISSUES: SQL Execution Jun 3, 2020
craig bot pushed a commit that referenced this issue Jun 6, 2020
49858: sql: use a separate type for pgcodes r=knz a=rohany

Fixes #49694.

This PR uses a separate wrapped type for pgcodes to ensure that
arbitrary strings are not passed to the pgerror functions in place of
pgcodes.

Release note: None

Co-authored-by: Rohan Yadav <rohany@alumni.cmu.edu>
@craig craig bot closed this as completed in c4307d9 Jun 6, 2020
SQL Features (Deprecated - use SQL Experience board) automation moved this from 20.2.c to Done Jun 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-pgwire pgwire protocol issues. C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) E-easy Easy issue to tackle, requires little or no CockroachDB experience good first issue
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

2 participants