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

Include the function name in generic datastore errors #2041

Merged
merged 3 commits into from
Jul 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions server/store/datastore/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
package datastore

import (
"fmt"
"runtime"
"strings"

"xorm.io/xorm"

"github.com/woodpecker-ci/woodpecker/server/model"
Expand All @@ -23,22 +27,26 @@ import (

// wrapGet return error if err not nil or if requested entry do not exist
func wrapGet(exist bool, err error) error {
if err != nil {
return err
}
if !exist {
return types.RecordNotExist
err = types.RecordNotExist
}
if err != nil {
// we only ask for the function's name if needed, as it's not as preformatted as to just execute it
fnName := callerName(2)
return fmt.Errorf("%s: %w", fnName, err)
}
return nil
}

// wrapDelete return error if err not nil or if requested entry do not exist
func wrapDelete(c int64, err error) error {
if err != nil {
return err
}
if c == 0 {
return types.RecordNotExist
err = types.RecordNotExist
}
if err != nil {
// we only ask for the function's name if needed, as it's not as preformatted as to just execute it
fnName := callerName(2)
return fmt.Errorf("%s: %w", fnName, err)
}
return nil
}
Expand All @@ -55,3 +63,16 @@ func (s storage) paginate(p *model.ListOptions) *xorm.Session {
}
return s.engine.Limit(p.PerPage, p.PerPage*(p.Page-1))
}

func callerName(skip int) string {
pc, _, _, ok := runtime.Caller(skip)
if !ok {
return ""
}
fnName := runtime.FuncForPC(pc).Name()
pIndex := strings.LastIndex(fnName, ".")
if pIndex != -1 {
fnName = fnName[pIndex+1:]
}
return fnName
}