fix: Improve error handling for malformed records in SQLite storage

This commit is contained in:
Alexandr Stelnykovych
2025-09-03 12:44:43 +03:00
parent ce52869945
commit c14eb43605
3 changed files with 9 additions and 8 deletions

View File

@@ -4,5 +4,6 @@ import "errors"
// Errors for storages.
var (
ErrNotFound = errors.New("storage entry not found")
ErrNotFound = errors.New("storage entry not found")
ErrRecordMalformed = errors.New("record is malformed")
)

View File

@@ -2,16 +2,16 @@ package sqlite
import (
"context"
"errors"
"fmt"
"strconv"
"github.com/safing/portmaster/base/database/record"
"github.com/safing/portmaster/base/database/storage"
"github.com/safing/portmaster/base/database/storage/sqlite/models"
"github.com/safing/structures/dsd"
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/dialect/sqlite/im"
"github.com/stephenafamo/bob/expr"
"github.com/safing/portmaster/base/database/record"
"github.com/safing/portmaster/base/database/storage/sqlite/models"
"github.com/safing/structures/dsd"
)
var UsePreparedStatements bool = true
@@ -91,7 +91,7 @@ func writeWithPreparedStatement(ctx context.Context, pStmt *bob.StdPrepared, r r
if r.IsWrapped() {
wrapper, ok := r.(*record.Wrapper)
if !ok {
return errors.New("record is malformed (reports to be wrapped but is not of type *record.Wrapper)")
return fmt.Errorf("%w: reports to be wrapped but is not of type *record.Wrapper", storage.ErrRecordMalformed)
}
format, ok = dsd.ValidateSerializationFormat(wrapper.Format)
if !ok {

View File

@@ -168,7 +168,7 @@ func (db *SQLite) putRecord(r record.Record, tx *bob.Tx) (record.Record, error)
if r.IsWrapped() {
wrapper, ok := r.(*record.Wrapper)
if !ok {
return nil, errors.New("record is malformed (reports to be wrapped but is not of type *record.Wrapper)")
return nil, fmt.Errorf("%w: reports to be wrapped but is not of type *record.Wrapper", storage.ErrRecordMalformed)
}
format, ok = dsd.ValidateSerializationFormat(wrapper.Format)
if !ok {