Skip to content
Snippets Groups Projects
Commit bb636cca authored by Gabriel Zachmann's avatar Gabriel Zachmann
Browse files

refactor db error norows check

parent 0fa84ddf
No related branches found
No related tags found
No related merge requests found
......@@ -130,3 +130,14 @@ func Transact(rlog log.Ext1FieldLogger, fn func(*sqlx.Tx) error) error {
func RunWithinTransaction(rlog log.Ext1FieldLogger, tx *sqlx.Tx, fn func(*sqlx.Tx) error) error {
return db.RunWithinTransaction(rlog, tx, fn)
}
// ParseError parses the passed error for a sql.ErrNoRows
func ParseError(err error) (bool, error) {
if err == nil {
return true, nil
}
if errors.Is(err, sql.ErrNoRows) {
err = nil
}
return false, err
}
......@@ -7,7 +7,6 @@ import (
"github.com/oidc-mytoken/server/internal/db"
"github.com/oidc-mytoken/server/internal/db/dbrepo/encryptionkeyrepo"
helper "github.com/oidc-mytoken/server/internal/db/dbrepo/mytokenrepo/mytokenrepohelper"
"github.com/oidc-mytoken/server/shared/mytoken/pkg/mtid"
"github.com/oidc-mytoken/server/shared/utils/cryptUtils"
)
......@@ -43,7 +42,7 @@ func UpdateRefreshToken(rlog log.Ext1FieldLogger, tx *sqlx.Tx, tokenID mtid.MTID
// GetRefreshToken returns the refresh token for a mytoken id
func GetRefreshToken(rlog log.Ext1FieldLogger, tx *sqlx.Tx, myid mtid.MTID, jwt string) (string, bool, error) {
var rt encryptionkeyrepo.RTCryptKeyDBRes
found, err := helper.ParseError(
found, err := db.ParseError(
db.RunWithinTransaction(
rlog, tx, func(tx *sqlx.Tx) error {
return errors.WithStack(tx.Get(&rt, `CALL EncryptionKeys_GetRTKeyForMT(?)`, myid))
......
......@@ -14,18 +14,6 @@ import (
"github.com/oidc-mytoken/server/shared/mytoken/pkg/mtid"
)
// ParseError parses the passed error for a sql.ErrNoRows
func ParseError(err error) (bool, error) {
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return false, nil
} else {
return false, err
}
}
return true, nil
}
// recursiveRevokeMT revokes the passed mytoken as well as all children
func recursiveRevokeMT(rlog log.Ext1FieldLogger, tx *sqlx.Tx, id mtid.MTID) error {
return db.RunWithinTransaction(
......
......@@ -218,7 +218,7 @@ func createMytokenEntry(
networkData api.ClientMetaData,
) (*mytokenrepo.MytokenEntry, *model.Response) {
rtID, dbErr := refreshtokenrepo.GetRTID(rlog, nil, parent.ID)
rtFound, err := dbhelper.ParseError(dbErr)
rtFound, err := db.ParseError(dbErr)
if err != nil {
rlog.WithError(dbErr).Error()
return nil, model.ErrorToInternalServerErrorResponse(dbErr)
......@@ -289,7 +289,7 @@ func RevokeMytoken(
rlog, tx, func(tx *sqlx.Tx) error {
rtID, err := refreshtokenrepo.GetRTID(rlog, tx, id)
if err != nil {
_, err = dbhelper.ParseError(err) // sets err to nil if token was not found;
_, err = db.ParseError(err) // sets err to nil if token was not found;
// this is no error and we are done, since the token is already revoked
return err
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment