Skip to content

Add error wrapping to provide better error context for debugging #12177

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion bindings/go/src/fdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import "C"

import (
"errors"
"fmt"
"runtime"
)

Expand Down Expand Up @@ -96,7 +97,7 @@ func (d Database) CreateTransaction() (Transaction, error) {
var outt *C.FDBTransaction

if err := C.fdb_database_create_transaction(d.ptr, &outt); err != 0 {
return Transaction{}, Error{int(err)}
return Transaction{}, fmt.Errorf("failed to create transaction: %w", Error{int(err)})
}

t := &transaction{outt, d}
Expand Down
6 changes: 3 additions & 3 deletions bindings/go/src/fdb/fdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ type ReadTransactor interface {

func setOpt(setter func(*C.uint8_t, C.int) C.fdb_error_t, param []byte) error {
if err := setter(byteSliceToPtr(param), C.int(len(param))); err != 0 {
return Error{int(err)}
return fmt.Errorf("failed to set option: %w", Error{int(err)})
}

return nil
Expand Down Expand Up @@ -157,7 +157,7 @@ func APIVersion(version int) error {
}
return fmt.Errorf("API version %d is not supported by the installed FoundationDB C library.", version)
}
return Error{int(e)}
return fmt.Errorf("failed to select API version: %w", Error{int(e)})
}
}

Expand Down Expand Up @@ -428,7 +428,7 @@ func OpenWithConnectionString(connectionString string) (Database, error) {
var createErr error
if err := executeWithRunningNetworkThread(func() {
if err := C.fdb_create_database_from_connection_string(cf, &outdb); err != 0 {
createErr = Error{int(err)}
createErr = fmt.Errorf("failed to create database from connection string: %w", Error{int(err)})
}
}); err != nil {
return Database{}, err
Expand Down
15 changes: 8 additions & 7 deletions bindings/go/src/fdb/futures.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ package fdb
import "C"

import (
"fmt"
"runtime"
"sync"
"unsafe"
Expand Down Expand Up @@ -165,7 +166,7 @@ func (f *futureByteSlice) Get() ([]byte, error) {
f.BlockUntilReady()

if err := C.fdb_future_get_value(f.ptr, &present, &value, &length); err != 0 {
f.e = Error{int(err)}
f.e = fmt.Errorf("failed to get future value: %w", Error{int(err)})
return
}

Expand Down Expand Up @@ -221,7 +222,7 @@ func (f *futureKey) Get() (Key, error) {
f.BlockUntilReady()

if err := C.fdb_future_get_key(f.ptr, &value, &length); err != 0 {
f.e = Error{int(err)}
f.e = fmt.Errorf("failed to get future key: %w", Error{int(err)})
return
}

Expand Down Expand Up @@ -266,7 +267,7 @@ func (f *futureNil) Get() error {

f.BlockUntilReady()
if err := C.fdb_future_get_error(f.ptr); err != 0 {
return Error{int(err)}
return fmt.Errorf("failed to get future error: %w", Error{int(err)})
}

return nil
Expand Down Expand Up @@ -305,7 +306,7 @@ func (f *futureKeyValueArray) Get() ([]KeyValue, bool, error) {
var more C.fdb_bool_t

if err := C.fdb_future_get_keyvalue_array(f.ptr, &kvs, &count, &more); err != 0 {
return nil, false, Error{int(err)}
return nil, false, fmt.Errorf("failed to get key-value array: %w", Error{int(err)})
}

ret := make([]KeyValue, int(count))
Expand Down Expand Up @@ -349,7 +350,7 @@ func (f *futureKeyArray) Get() ([]Key, error) {
var count C.int

if err := C.fdb_future_get_key_array(f.ptr, &ks, &count); err != 0 {
return nil, Error{int(err)}
return nil, fmt.Errorf("failed to get key array: %w", Error{int(err)})
}

ret := make([]Key, int(count))
Expand Down Expand Up @@ -399,7 +400,7 @@ func (f *futureInt64) Get() (int64, error) {

var ver C.int64_t
if err := C.fdb_future_get_int64(f.ptr, &ver); err != 0 {
return 0, Error{int(err)}
return 0, fmt.Errorf("failed to get int64 value: %w", Error{int(err)})
}

return int64(ver), nil
Expand Down Expand Up @@ -444,7 +445,7 @@ func (f *futureStringSlice) Get() ([]string, error) {
var count C.int

if err := C.fdb_future_get_string_array(f.ptr, (***C.char)(unsafe.Pointer(&strings)), &count); err != 0 {
return nil, Error{int(err)}
return nil, fmt.Errorf("failed to get string array: %w", Error{int(err)})
}

ret := make([]string, int(count))
Expand Down
4 changes: 2 additions & 2 deletions bindings/go/src/fdb/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func (t Transaction) GetCommittedVersion() (int64, error) {
var version C.int64_t

if err := C.fdb_transaction_get_committed_version(t.ptr, &version); err != 0 {
return 0, Error{int(err)}
return 0, fmt.Errorf("failed to get committed version: %w", Error{int(err)})
}

return int64(version), nil
Expand Down Expand Up @@ -515,7 +515,7 @@ func addConflictRange(t *transaction, er ExactRange, crtype conflictRangeType) e
C.int(len(ekb)),
C.FDBConflictRangeType(crtype),
); err != 0 {
return Error{int(err)}
return fmt.Errorf("failed to add conflict range: %w", Error{int(err)})
}

return nil
Expand Down