Skip to content

Allow errors returned by queries to be wrapped with method name information #1612

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

Open
Hades32 opened this issue May 11, 2022 · 1 comment
Open
Labels
enhancement New feature or request 🔧 golang

Comments

@Hades32
Copy link

Hades32 commented May 11, 2022

What do you want to change?

Free unicorns! And...

Currently, when I use a bunch of query methods (like q.SelectUnicorns()) and then just do the usual if err!=nil { return err }, then I'll have a hard time figuring out which of those caused the SQL error.

Therefore, I typically do

err := q.SelectUnicorns()`
if err!= nil {
  return fmt.Errorf("SelectUnicorns %w", err)
}

It would be nice if there was a feature toggle to enable this wrapping right within the generated code.

What database engines need to be changed?

No response

What programming language backends need to be changed?

Go

@Hades32 Hades32 added enhancement New feature or request triage New issues that hasn't been reviewed labels May 11, 2022
@kyleconroy kyleconroy added 🔧 golang and removed triage New issues that hasn't been reviewed labels May 12, 2022
@jgustie
Copy link

jgustie commented Apr 5, 2023

It would be nice to have the option to enable error wrapping under gen.go in the config; tracking down errors within complex services (especially something like a "no rows" error which otherwise has no other context) can be difficult.

I was able to hack together my own solution by enabling gen.go.emit_interface and using GoWrap to create a decorator with a template similar to this:

...

// {{$decorator}} implements {{.Interface.Type}} and wraps errors with the associated query name.
type {{$decorator}} struct{ {{.Interface.Type}} }

{{range $method := .Interface.Methods}}
func (w *{{$decorator}}) {{$method.Declaration}} {
	{{$method.ResultsNames}} = w.{{$.Interface.Name}}.{{$method.Call}}
	if err != nil {
		err = &SqlcError{QueryName: "{{$method.Name}}", wrapped: err}
	}
	return
}
{{end}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request 🔧 golang
Projects
None yet
Development

No branches or pull requests

3 participants