diff --git a/api/errcode.proto b/api/errcode.proto new file mode 100644 index 0000000000..90c37dab78 --- /dev/null +++ b/api/errcode.proto @@ -0,0 +1,86 @@ +syntax = "proto3"; + +package berty.errcode; + +import "github.com/gogo/protobuf/gogoproto/gogo.proto"; + +option go_package = "berty.tech/go/pkg/errcode"; + +option (gogoproto.benchgen_all) = false; +option (gogoproto.compare_all) = false; +option (gogoproto.description_all) = false; +option (gogoproto.enum_stringer_all) = false; +option (gogoproto.enumdecl_all) = true; +option (gogoproto.equal_all) = false; +option (gogoproto.face_all) = false; +option (gogoproto.gogoproto_import) = false; +option (gogoproto.goproto_enum_prefix_all) = false; +option (gogoproto.goproto_enum_stringer_all) = false; +option (gogoproto.goproto_extensions_map_all) = false; +option (gogoproto.goproto_getters_all) = false; +option (gogoproto.goproto_registration) = false; +//option (gogoproto.goproto_sizecache_all) = false; +option (gogoproto.goproto_stringer_all) = false; +//option (gogoproto.goproto_unkeyed_all) = false; +option (gogoproto.goproto_unrecognized_all) = false; +option (gogoproto.gostring_all) = false; +option (gogoproto.marshaler_all) = false; +option (gogoproto.messagename_all) = false; +option (gogoproto.onlyone_all) = false; +option (gogoproto.populate_all) = false; +option (gogoproto.protosizer_all) = false; +option (gogoproto.sizer_all) = false; +option (gogoproto.stable_marshaler_all) = false; +option (gogoproto.stringer_all) = false; +option (gogoproto.testgen_all) = false; +option (gogoproto.typedecl_all) = false; +option (gogoproto.unmarshaler_all) = false; +option (gogoproto.unsafe_marshaler_all) = false; +option (gogoproto.unsafe_unmarshaler_all) = false; +option (gogoproto.verbose_equal_all) = false; + +enum ErrCode { + Undefined = 0; // default value, should never be set manually + + TODO = 666; // indicates that you plan to write a custom error handler later + ErrNotImplemented = 777; + ErrInternal = 999; // can be used to translate an "unknown" error (without Code), i.e., in gRPC + + // + // Generic helpers (try to use a more specific error when possible) + // + + ErrInvalidInput = 101; + ErrMissingInput = 102; + + // + // Berty Chat (starting at 1001) + // + + // + // Berty Protocol (starting at 2001) + // + + ErrSigChainNoEntries = 2001; + ErrSigChainInvalidEntryType = 2002; + ErrSigChainAlreadyInitialized = 2003; + ErrSigChainPermission = 2004; + ErrSigChainOperationAlreadyDone = 2005; + ErrHandshakeNoPayload = 2006; + ErrHandshakeInvalidFlow = 2007; + ErrHandshakeInvalidFlowStepNotFound = 2008; + ErrHandshakeParams = 2009; + ErrHandshakeNoAuthReturned = 2010; + ErrHandshakeInvalidKeyType = 2011; + ErrHandshakeInvalidSignature = 2012; + ErrHandshakeSessionInvalid = 2013; + ErrHandshakeKeyNotInSigChain = 2014; + ErrHandshakeDecrypt = 2015; + + // + // Chat Bridge (starting at 3001) + // + + ErrBridgeInterrupted = 3001; + ErrBridgeNotRunning = 3002; +} \ No newline at end of file diff --git a/docs/gen.sum b/docs/gen.sum index e9f5775e70..0a5fa607c8 100644 --- a/docs/gen.sum +++ b/docs/gen.sum @@ -1,4 +1,5 @@ 091c2c356ed6594b6c0e078f2826e85b0236a8d5 ../api/bertychat.proto 02c94a7ec8a717d89c8b95b20fabf942f8934724 ../api/bertyprotocol.proto 03fd21adc9eb2846c2d6cd62eac6184bbffaf455 ../api/chatmodel.proto +a433caf96caafdf90f3190cfb1763dfc73b075c4 ../api/errcode.proto dd607c85fc7a1cc1e523a59873634b362cc72824 Makefile diff --git a/githooks/pre-commit b/githooks/pre-commit index 40418ab6f6..ff961dea72 100755 --- a/githooks/pre-commit +++ b/githooks/pre-commit @@ -63,6 +63,8 @@ for DIR in $STAGED_GO_DIRS; do ) done +rm -f go.mod + if ! $PASS; then printf "\033[0;30m\033[41mCOMMIT FAILED\033[0m\n" exit 1 diff --git a/go/cmd/bertychat/main.go b/go/cmd/bertychat/main.go index 2178d6870b..9d21969c02 100644 --- a/go/cmd/bertychat/main.go +++ b/go/cmd/bertychat/main.go @@ -13,11 +13,11 @@ import ( _ "berty.tech/go/internal/buildconstraints" // fail if bad go version "berty.tech/go/pkg/bertychat" "berty.tech/go/pkg/bertyprotocol" + "berty.tech/go/pkg/errcode" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/sqlite" // required by gorm "github.com/peterbourgon/ff" "github.com/peterbourgon/ff/ffcli" - "github.com/pkg/errors" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) @@ -46,7 +46,7 @@ func main() { var err error logger, err = config.Build() if err != nil { - return errors.Wrap(err, "failed to initialize logger") + return errcode.TODO.Wrap(err) } logger.Debug("logger initialized in debug mode") } else { @@ -57,7 +57,7 @@ func main() { var err error logger, err = config.Build() if err != nil { - return errors.Wrap(err, "failed to initialize logger") + return errcode.TODO.Wrap(err) } } return nil @@ -106,7 +106,7 @@ func main() { // initialize sqlite3 gorm database db, err := gorm.Open("sqlite3", *clientProtocolURN) if err != nil { - return errors.Wrap(err, "failed to initialize gorm") + return errcode.TODO.Wrap(err) } defer db.Close() @@ -116,7 +116,7 @@ func main() { } protocol, err = bertyprotocol.New(db, opts) if err != nil { - return errors.Wrap(err, "failed to initialize protocol") + return errcode.TODO.Wrap(err) } defer protocol.Close() @@ -128,7 +128,7 @@ func main() { // initialize sqlite3 gorm database db, err := gorm.Open("sqlite3", *clientChatURN) if err != nil { - return errors.Wrap(err, "failed to initialize gorm") + return errcode.TODO.Wrap(err) } defer db.Close() @@ -138,7 +138,7 @@ func main() { } chat, err = bertychat.New(db, protocol, chatOpts) if err != nil { - return errors.Wrap(err, "failed to initialize chat") + return errcode.TODO.Wrap(err) } defer chat.Close() @@ -146,7 +146,7 @@ func main() { info, err := protocol.AccountGetInformation(ctx, nil) if err != nil { - return errors.Wrap(err, "failed to get protocol information") + return errcode.TODO.Wrap(err) } logger.Info("client initialized", zap.String("peer-id", info.PeerID), zap.Strings("listeners", info.Listeners)) diff --git a/go/framework/chatbridge/bridge.go b/go/framework/chatbridge/bridge.go index 6c7499e76e..15309430cd 100644 --- a/go/framework/chatbridge/bridge.go +++ b/go/framework/chatbridge/bridge.go @@ -10,15 +10,16 @@ import ( _ "berty.tech/go/internal/buildconstraints" // fail if bad go version _ "github.com/jinzhu/gorm/dialects/sqlite" // required by gorm + "github.com/pkg/errors" "berty.tech/go/internal/bridgeutil" "berty.tech/go/pkg/bertychat" "berty.tech/go/pkg/bertyprotocol" + "berty.tech/go/pkg/errcode" "github.com/improbable-eng/grpc-web/go/grpcweb" "github.com/jinzhu/gorm" "github.com/oklog/run" - "github.com/pkg/errors" "go.uber.org/zap" "go.uber.org/zap/zapcore" "google.golang.org/grpc" @@ -86,7 +87,7 @@ func newBridge(logger *zap.Logger, opts Opts) (*Bridge, error) { b.workers.Add(func() error { // wait for closing signal <-b.cclose - return ErrInterrupted + return errcode.ErrBridgeInterrupted }, func(error) { b.once.Do(func() { close(b.cclose) }) }) @@ -96,7 +97,7 @@ func newBridge(logger *zap.Logger, opts Opts) (*Bridge, error) { var err error b.protocolDB, err = gorm.Open("sqlite3", ":memory:") if err != nil { - return nil, errors.Wrap(err, "initialize gorm") + return nil, errcode.TODO.Wrap(err) } // initialize new protocol client @@ -106,7 +107,7 @@ func newBridge(logger *zap.Logger, opts Opts) (*Bridge, error) { b.protocolClient, err = bertyprotocol.New(b.protocolDB, protocolOpts) if err != nil { - return nil, errors.Wrap(err, "initialize protocol") + return nil, errcode.TODO.Wrap(err) } } @@ -116,7 +117,7 @@ func newBridge(logger *zap.Logger, opts Opts) (*Bridge, error) { // initialize sqlite3 gorm database b.chatDB, err = gorm.Open("sqlite3", ":memory:") if err != nil { - return nil, errors.Wrap(err, "initialize gorm") + return nil, errcode.TODO.Wrap(err) } // initialize bertychat client @@ -126,7 +127,7 @@ func newBridge(logger *zap.Logger, opts Opts) (*Bridge, error) { b.chatClient, err = bertychat.New(b.chatDB, b.protocolClient, chatOpts) if err != nil { - return nil, errors.Wrap(err, "initialize chat") + return nil, errcode.TODO.Wrap(err) } } @@ -184,7 +185,7 @@ func (b *Bridge) isClosed() bool { // Close bridge func (b *Bridge) Close() (err error) { if b.isClosed() { - return ErrNotRunning + return errcode.ErrBridgeNotRunning } b.logger.Info("bridge.Close called") @@ -209,8 +210,8 @@ func (b *Bridge) Close() (err error) { b.protocolClient.Close() b.protocolDB.Close() - if err != ErrInterrupted { - return errors.Wrap(err, "failed close bridge gracefully") + if err != errcode.ErrBridgeInterrupted { + return errcode.TODO.Wrap(err) } return nil @@ -222,7 +223,7 @@ func (b *Bridge) Close() (err error) { func (b *Bridge) addGRPCListener(addr string) (string, error) { l, err := net.Listen("tcp", addr) if err != nil { - return "", errors.Wrap(err, "listen") + return "", errcode.TODO.Wrap(err) } b.workers.Add(func() error { @@ -242,7 +243,7 @@ func (b *Bridge) addGRPCListener(addr string) (string, error) { func (b *Bridge) addGRPCWebListener(addr string) (string, error) { l, err := net.Listen("tcp", addr) if err != nil { - return "", errors.Wrap(err, "listen") + return "", errcode.TODO.Wrap(err) } // setup grpc web @@ -303,7 +304,7 @@ func (b *Bridge) addGRPCWebListener(addr string) (string, error) { // NewGRPCClient return client service on success func (b *Bridge) newGRPCClient() (client *Client, err error) { if b.isClosed() { - return nil, ErrNotRunning + return nil, errcode.ErrBridgeNotRunning } var grpcClient *grpc.ClientConn diff --git a/go/framework/chatbridge/errors.go b/go/framework/chatbridge/errors.go deleted file mode 100644 index 106d35b2c4..0000000000 --- a/go/framework/chatbridge/errors.go +++ /dev/null @@ -1,11 +0,0 @@ -package chatbridge - -// Error is a simple Error struct. See https://dave.cheney.net/2016/04/07/constant-errors -type Error string - -func (e Error) Error() string { return string(e) } - -const ( - ErrInterrupted = Error("bridge has been interrupted") - ErrNotRunning = Error("bridge is not running or has already been stopped") -) diff --git a/go/gen.sum b/go/gen.sum index 3db41112c4..8214828b9a 100644 --- a/go/gen.sum +++ b/go/gen.sum @@ -1,6 +1,7 @@ 091c2c356ed6594b6c0e078f2826e85b0236a8d5 ../api/bertychat.proto 02c94a7ec8a717d89c8b95b20fabf942f8934724 ../api/bertyprotocol.proto 03fd21adc9eb2846c2d6cd62eac6184bbffaf455 ../api/chatmodel.proto +a433caf96caafdf90f3190cfb1763dfc73b075c4 ../api/errcode.proto 74e397e248a9c7e7d094109a02e873df24e80367 ../api/go-internal/handshake.proto d8c797e0f3efd2b4f26a9ff949bedb786891a850 ../api/go-internal/protocolmodel.proto 50384eff13181fb905dee2b7df62276408e3dc62 ../api/go-internal/sigchain.proto diff --git a/go/internal/crypto/crypto.go b/go/internal/crypto/crypto.go index 2b3b3f92fb..c3e25ff35b 100644 --- a/go/internal/crypto/crypto.go +++ b/go/internal/crypto/crypto.go @@ -3,11 +3,9 @@ package crypto import ( "context" - "github.com/pkg/errors" - - "go.uber.org/zap" - + "berty.tech/go/pkg/errcode" p2pcrypto "github.com/libp2p/go-libp2p-core/crypto" + "go.uber.org/zap" ) type crypto struct { @@ -24,12 +22,12 @@ func (c *crypto) GetDevicePublicKey() p2pcrypto.PubKey { func (c *crypto) GetAccountPublicKey() (p2pcrypto.PubKey, error) { initialEntry, err := c.sigChain.GetInitialEntry() if err != nil { - return nil, errors.Wrap(err, "unable to get initial sig chain entry") + return nil, errcode.TODO.Wrap(err) } pubKey, err := initialEntry.GetSubject() if err != nil { - return nil, errors.Wrap(err, "unable to get entry subject") + return nil, errcode.TODO.Wrap(err) } return pubKey, nil @@ -45,7 +43,7 @@ func (c *crypto) Sign(data []byte) ([]byte, error) { func (c *crypto) AddDeviceToOwnSigChain(ctx context.Context, key p2pcrypto.PubKey) error { _, err := c.sigChain.AddEntry(c.privKey, key, c.opts) - return errors.Wrap(err, "unable to add device to sig chain") + return errcode.TODO.Wrap(err) } func (c *crypto) Close() error { diff --git a/go/internal/crypto/crypto_module.go b/go/internal/crypto/crypto_module.go index 711debfe84..92f50e8d86 100644 --- a/go/internal/crypto/crypto_module.go +++ b/go/internal/crypto/crypto_module.go @@ -8,10 +8,9 @@ import ( "encoding/binary" "time" - "go.uber.org/zap" - + "berty.tech/go/pkg/errcode" p2pcrypto "github.com/libp2p/go-libp2p-core/crypto" - "github.com/pkg/errors" + "go.uber.org/zap" ) type Opts struct { @@ -21,12 +20,12 @@ type Opts struct { func InitNewIdentity(ctx context.Context, opts *Opts) (Manager, p2pcrypto.PrivKey, error) { privKey, err := GeneratePrivateKey() if err != nil { - return nil, nil, errors.Wrap(err, "unable to generate a private key") + return nil, nil, errcode.TODO.Wrap(err) } sigChain, err := InitSigChain(privKey, opts) if err != nil { - return nil, nil, errors.Wrap(err, "unable to get initial sig chain entry") + return nil, nil, errcode.TODO.Wrap(err) } return NewCrypto(privKey, sigChain, opts), privKey, nil @@ -44,19 +43,19 @@ func OpenIdentity(ctx context.Context, key p2pcrypto.PrivKey, chain SigChainMana func InitSigChain(key p2pcrypto.PrivKey, opts *Opts) (SigChainManager, error) { accountKey, err := GeneratePrivateKey() if err != nil { - return nil, errors.Wrap(err, "unable to get generate a private key") + return nil, errcode.TODO.Wrap(err) } sigChain := NewSigChain(opts) _, err = sigChain.Init(accountKey) if err != nil { - return nil, errors.Wrap(err, "unable to initiate sig chain") + return nil, errcode.TODO.Wrap(err) } _, err = sigChain.AddEntry(accountKey, key.GetPublic(), opts) if err != nil { - return nil, errors.Wrap(err, "unable to add a sig chain entry") + return nil, errcode.TODO.Wrap(err) } return sigChain, nil @@ -65,7 +64,7 @@ func InitSigChain(key p2pcrypto.PrivKey, opts *Opts) (SigChainManager, error) { func GeneratePrivateKey() (p2pcrypto.PrivKey, error) { key, _, err := p2pcrypto.GenerateEd25519Key(rand.Reader) if err != nil { - return nil, errors.Wrap(err, "unable to generate a key pair") + return nil, errcode.TODO.Wrap(err) } return key, nil @@ -84,7 +83,7 @@ func GetRendezvousPointForTime(id, seed []byte, date time.Time) ([]byte, error) binary.BigEndian.PutUint64(buf, uint64(date.Unix())) if _, err := mac.Write(buf); err != nil { - return nil, errors.Wrap(err, "unable to write to buffer") + return nil, errcode.TODO.Wrap(err) } sum := mac.Sum(nil) diff --git a/go/internal/crypto/crypto_sigchain.go b/go/internal/crypto/crypto_sigchain.go index e03e8a0ebb..f9f6f80e2f 100644 --- a/go/internal/crypto/crypto_sigchain.go +++ b/go/internal/crypto/crypto_sigchain.go @@ -3,10 +3,9 @@ package crypto import ( "time" - "go.uber.org/zap" - + "berty.tech/go/pkg/errcode" p2pcrypto "github.com/libp2p/go-libp2p-core/crypto" - "github.com/pkg/errors" + "go.uber.org/zap" ) var theFuture = time.Date(2199, time.December, 31, 0, 0, 0, 0, time.UTC) @@ -19,13 +18,13 @@ type wrappedSigChain struct { func (m *wrappedSigChain) GetInitialEntry() (*SigChainEntry, error) { entries := m.ListEntries() if len(entries) == 0 { - return nil, ErrSigChainNoEntries + return nil, errcode.ErrSigChainNoEntries } e := entries[0] if e.EntryTypeCode != SigChainEntry_SigChainEntryTypeInitChain { - return nil, ErrSigChainInvalidEntryType + return nil, errcode.ErrSigChainInvalidEntryType } return e, nil @@ -79,12 +78,12 @@ func (m *wrappedSigChain) ListCurrentPubKeys() []p2pcrypto.PubKey { func (m *wrappedSigChain) Init(privKey p2pcrypto.PrivKey) (*SigChainEntry, error) { if len(m.Entries) > 0 { - return nil, ErrSigChainAlreadyInitialized + return nil, errcode.ErrSigChainAlreadyInitialized } subjectKeyBytes, err := privKey.GetPublic().Bytes() if err != nil { - return nil, errors.Wrap(err, "unable to get subject key bytes") + return nil, errcode.TODO.Wrap(err) } return m.appendEntry(privKey, &SigChainEntry{ @@ -95,16 +94,16 @@ func (m *wrappedSigChain) Init(privKey p2pcrypto.PrivKey) (*SigChainEntry, error func (m *wrappedSigChain) AddEntry(privKey p2pcrypto.PrivKey, pubKey p2pcrypto.PubKey, opts *Opts) (*SigChainEntry, error) { if !m.isKeyCurrentlyPresent(privKey.GetPublic(), opts) { - return nil, ErrSigChainPermission + return nil, errcode.ErrSigChainPermission } if m.isKeyCurrentlyPresent(pubKey, opts) { - return nil, ErrSigChainOperationAlreadyDone + return nil, errcode.ErrSigChainOperationAlreadyDone } subjectKeyBytes, err := pubKey.Bytes() if err != nil { - return nil, errors.Wrap(err, "unable to get subject key bytes") + return nil, errcode.TODO.Wrap(err) } return m.appendEntry(privKey, &SigChainEntry{ @@ -115,16 +114,16 @@ func (m *wrappedSigChain) AddEntry(privKey p2pcrypto.PrivKey, pubKey p2pcrypto.P func (m *wrappedSigChain) RemoveEntry(privKey p2pcrypto.PrivKey, pubKey p2pcrypto.PubKey, opts *Opts) (*SigChainEntry, error) { if !m.isKeyCurrentlyPresent(privKey.GetPublic(), opts) { - return nil, ErrSigChainPermission + return nil, errcode.ErrSigChainPermission } if !m.isKeyCurrentlyPresent(pubKey, opts) { - return nil, ErrSigChainOperationAlreadyDone + return nil, errcode.ErrSigChainOperationAlreadyDone } subjectKeyBytes, err := pubKey.Bytes() if err != nil { - return nil, errors.Wrap(err, "unable to get subject key bytes") + return nil, errcode.TODO.Wrap(err) } return m.appendEntry(privKey, &SigChainEntry{ @@ -151,7 +150,7 @@ func (m *wrappedSigChain) appendEntry(privKey p2pcrypto.PrivKey, entry *SigChain signerPubKeyBytes, err := privKey.GetPublic().Bytes() if err != nil { - return nil, errors.Wrap(err, "unable to get signer key bytes") + return nil, errcode.TODO.Wrap(err) } entry.CreatedAt = time.Now() @@ -160,7 +159,7 @@ func (m *wrappedSigChain) appendEntry(privKey p2pcrypto.PrivKey, entry *SigChain err = entry.Sign(privKey) if err != nil { - return nil, errors.Wrap(err, "unable to sign entry") + return nil, errcode.TODO.Wrap(err) } m.Entries = append(m.Entries, entry) diff --git a/go/internal/crypto/crypto_sigchain_entry.go b/go/internal/crypto/crypto_sigchain_entry.go index 638daf45f0..e9eac53f93 100644 --- a/go/internal/crypto/crypto_sigchain_entry.go +++ b/go/internal/crypto/crypto_sigchain_entry.go @@ -1,9 +1,9 @@ package crypto import ( + "berty.tech/go/pkg/errcode" p2pcrypto "github.com/libp2p/go-libp2p-core/crypto" mh "github.com/multiformats/go-multihash" - "github.com/pkg/errors" ) func (m *SigChainEntry) Sign(key p2pcrypto.PrivKey) error { @@ -18,19 +18,19 @@ func (m *SigChainEntry) Sign(key p2pcrypto.PrivKey) error { entryBytes, err := entryToSign.Marshal() if err != nil { - return errors.Wrap(err, "unable to marshal entry to sign") + return errcode.TODO.Wrap(err) } h, err := mh.Sum(entryBytes, mh.SHA2_256, -1) if err != nil { - return errors.Wrap(err, "unable to make hash of entry to sign") + return errcode.TODO.Wrap(err) } entryHash := []byte(h.B58String()) sig, err := key.Sign(entryHash) if err != nil { - return errors.Wrap(err, "unable to sign entry to add") + return errcode.TODO.Wrap(err) } m.EntryHash = entryHash @@ -42,7 +42,7 @@ func (m *SigChainEntry) Sign(key p2pcrypto.PrivKey) error { func (m *SigChainEntry) GetSignedBy() (p2pcrypto.PubKey, error) { pubKey, err := p2pcrypto.UnmarshalPublicKey(m.SignerPublicKeyBytes) if err != nil { - return nil, errors.Wrap(err, "unable to unmarshal entry signer") + return nil, errcode.TODO.Wrap(err) } return pubKey, nil @@ -51,7 +51,7 @@ func (m *SigChainEntry) GetSignedBy() (p2pcrypto.PubKey, error) { func (m *SigChainEntry) GetSubject() (p2pcrypto.PubKey, error) { pubKey, err := p2pcrypto.UnmarshalPublicKey(m.SubjectPublicKeyBytes) if err != nil { - return nil, errors.Wrap(err, "unable to unmarshal entry subject") + return nil, errcode.TODO.Wrap(err) } return pubKey, nil diff --git a/go/internal/crypto/errors.go b/go/internal/crypto/errors.go deleted file mode 100644 index 45c3b08ae4..0000000000 --- a/go/internal/crypto/errors.go +++ /dev/null @@ -1,14 +0,0 @@ -package crypto - -// Error is a simple error struct. See https://dave.cheney.net/2016/04/07/constant-errors -type Error string - -func (e Error) Error() string { return string(e) } - -const ( - ErrSigChainNoEntries = Error("sigchain: no entries found") - ErrSigChainInvalidEntryType = Error("sigchain: invalid entry type") - ErrSigChainAlreadyInitialized = Error("sigchain: chain already initialized") - ErrSigChainPermission = Error("sigchain: not allowed to perform operation") - ErrSigChainOperationAlreadyDone = Error("sigchain: operation already performed") -) diff --git a/go/internal/handshake/crypto_module.go b/go/internal/handshake/crypto_module.go index 9a5d91fec5..5bf1668c11 100644 --- a/go/internal/handshake/crypto_module.go +++ b/go/internal/handshake/crypto_module.go @@ -4,9 +4,8 @@ import ( "crypto/rand" "berty.tech/go/internal/crypto" - + "berty.tech/go/pkg/errcode" p2pcrypto "github.com/libp2p/go-libp2p-core/crypto" - "golang.org/x/crypto/nacl/box" ) @@ -14,7 +13,7 @@ func bytesSliceToArray(slice []byte) (*[32]byte, error) { var arr [32]byte if len(slice) != 32 { - return nil, ErrInvalidKeyType + return nil, errcode.ErrHandshakeInvalidKeyType } for i, c := range slice { diff --git a/go/internal/handshake/crypto_session.go b/go/internal/handshake/crypto_session.go index 4b6bb02598..c09c0819ad 100644 --- a/go/internal/handshake/crypto_session.go +++ b/go/internal/handshake/crypto_session.go @@ -4,9 +4,8 @@ import ( "encoding/binary" "berty.tech/go/internal/crypto" - + "berty.tech/go/pkg/errcode" p2pcrypto "github.com/libp2p/go-libp2p-core/crypto" - "golang.org/x/crypto/nacl/box" ) @@ -32,7 +31,7 @@ func (h *handshakeSession) SetOtherKeys(sign p2pcrypto.PubKey, box []byte) error } if sign.Type() != SupportedKeyType { - return ErrInvalidKeyType + return errcode.ErrHandshakeInvalidKeyType } h.otherSigningPublicKey = sign @@ -51,7 +50,7 @@ func (h *handshakeSession) GetPublicKeys() (sign p2pcrypto.PubKey, box []byte) { func computeValueToProvePubKey(keyToProve p2pcrypto.PubKey, receiverSigKey *[32]byte) ([]byte, error) { if keyToProve == nil || receiverSigKey == nil { - return nil, ErrParams + return nil, errcode.ErrHandshakeParams } keyToProveBytes, err := keyToProve.Raw() @@ -66,7 +65,7 @@ func computeValueToProvePubKey(keyToProve p2pcrypto.PubKey, receiverSigKey *[32] func computeValueToProveDevicePubKeyAndSigChain(keyToProve *[32]byte, chain crypto.SigChainManager) ([]byte, error) { if keyToProve == nil || chain == nil { - return nil, ErrParams + return nil, errcode.ErrHandshakeParams } sigChainBytes, err := chain.Marshal() @@ -82,7 +81,7 @@ func computeValueToProveDevicePubKeyAndSigChain(keyToProve *[32]byte, chain cryp func (h *handshakeSession) ProveOtherKey() ([]byte, error) { // Step 3a (out) : sig_a1(B·b1) if h.accountKeyToProve == nil { - return nil, ErrSessionInvalid + return nil, errcode.ErrHandshakeSessionInvalid } signedValue, err := computeValueToProvePubKey(h.accountKeyToProve, h.otherBoxPublicKey) @@ -122,7 +121,7 @@ func (h *handshakeSession) CheckOwnKeyProof(sig []byte) error { } if !ok { - return ErrInvalidSignature + return errcode.ErrHandshakeInvalidSignature } return nil @@ -157,7 +156,7 @@ func (h *handshakeSession) CheckOtherKeyProof(sig []byte, chain crypto.SigChainM } if !ok { - return ErrInvalidSignature + return errcode.ErrHandshakeInvalidSignature } entries := chain.ListCurrentPubKeys() @@ -167,7 +166,7 @@ func (h *handshakeSession) CheckOtherKeyProof(sig []byte, chain crypto.SigChainM } } - return ErrKeyNotInSigChain + return errcode.ErrHandshakeKeyNotInSigChain } func (h *handshakeSession) ProveOtherKnownAccount() ([]byte, error) { @@ -198,7 +197,7 @@ func (h *handshakeSession) CheckOwnKnownAccountProof(attemptedDeviceKey p2pcrypt } if !ok { - return ErrInvalidSignature + return errcode.ErrHandshakeInvalidSignature } return nil @@ -206,7 +205,7 @@ func (h *handshakeSession) CheckOwnKnownAccountProof(attemptedDeviceKey p2pcrypt func (h *handshakeSession) Encrypt(data []byte) ([]byte, error) { if h.otherBoxPublicKey == nil || h.selfBoxPrivateKey == nil { - return nil, ErrSessionInvalid + return nil, errcode.ErrHandshakeSessionInvalid } nonce := h.getNonce() @@ -220,14 +219,14 @@ func (h *handshakeSession) Encrypt(data []byte) ([]byte, error) { func (h *handshakeSession) Decrypt(data []byte) ([]byte, error) { if h.otherBoxPublicKey == nil || h.selfBoxPrivateKey == nil { - return nil, ErrSessionInvalid + return nil, errcode.ErrHandshakeSessionInvalid } nonce := h.getNonce() out, ok := box.Open(nil, data, &nonce, h.otherBoxPublicKey, h.selfBoxPrivateKey) if !ok { - return nil, ErrDecrypt + return nil, errcode.ErrHandshakeDecrypt } h.incrementNonce() diff --git a/go/internal/handshake/crypto_test.go b/go/internal/handshake/crypto_test.go index e7683b66d5..7890c6266b 100644 --- a/go/internal/handshake/crypto_test.go +++ b/go/internal/handshake/crypto_test.go @@ -6,6 +6,7 @@ import ( "testing" "berty.tech/go/internal/crypto" + "berty.tech/go/pkg/errcode" p2pcrypto "github.com/libp2p/go-libp2p-core/crypto" ) @@ -252,14 +253,14 @@ func TestHandshakeSession_Encrypt_Decrypt(t *testing.T) { // Should not decode the message twice decrypted, err = hss2.Decrypt(encrypted) - if err != ErrDecrypt || string(decrypted) != "" { - t.Fatalf("err should be ErrDecrypt and decrypted should be empty") + if err != errcode.ErrHandshakeDecrypt || string(decrypted) != "" { + t.Fatalf("err should be errcode.ErrHandshakeDecrypt and decrypted should be empty") } // Should not decode a random string decrypted, err = hss2.Decrypt([]byte("blahblah")) - if err != ErrDecrypt || string(decrypted) != "" { - t.Fatalf("err should be ErrDecrypt and decrypted should be empty") + if err != errcode.ErrHandshakeDecrypt || string(decrypted) != "" { + t.Fatalf("err should be errcode.ErrHandshakeDecrypt and decrypted should be empty") } // Should be able to encode a second message diff --git a/go/internal/handshake/errors.go b/go/internal/handshake/errors.go deleted file mode 100644 index 42dacfd82c..0000000000 --- a/go/internal/handshake/errors.go +++ /dev/null @@ -1,19 +0,0 @@ -package handshake - -// Error is a simple error struct. See https://dave.cheney.net/2016/04/07/constant-errors -type Error string - -func (e Error) Error() string { return string(e) } - -const ( - ErrNoPayload = Error("handshake: no payload specified") - ErrInvalidFlow = Error("handshake: invalid flow") - ErrInvalidFlowStepNotFound = Error("handshake: invalid flow, step not found") - ErrParams = Error("handshake: can't init with supplied parameters") - ErrNoAuthReturned = Error("handshake: no authenticated sig chain or device key returned") - ErrInvalidKeyType = Error("handshake: invalid key type") - ErrInvalidSignature = Error("handshake: signature is not valid") - ErrSessionInvalid = Error("handshake: session has not been properly initialized") - ErrKeyNotInSigChain = Error("handshake: key not found in sig chain") - ErrDecrypt = Error("handshake: unable to decrypt data") -) diff --git a/go/internal/handshake/net_flow.go b/go/internal/handshake/net_flow.go index 4ff831e71f..e451a88d62 100644 --- a/go/internal/handshake/net_flow.go +++ b/go/internal/handshake/net_flow.go @@ -5,7 +5,7 @@ import ( "net" "berty.tech/go/internal/crypto" - + "berty.tech/go/pkg/errcode" ggio "github.com/gogo/protobuf/io" p2pcrypto "github.com/libp2p/go-libp2p-core/crypto" inet "github.com/libp2p/go-libp2p-core/network" @@ -29,7 +29,7 @@ type flow struct { func newHandshakeFlow(ctx context.Context, conn net.Conn, devPubKey p2pcrypto.PubKey, ownSigChain crypto.SigChainManager, session *handshakeSession, steps map[HandshakeFrame_HandshakeStep]flowStep) (crypto.SigChainManager, p2pcrypto.PubKey, error) { if conn == nil || session == nil || steps == nil { - return nil, nil, ErrParams + return nil, nil, errcode.ErrHandshakeParams } writer := ggio.NewDelimitedWriter(conn) @@ -48,8 +48,6 @@ func newHandshakeFlow(ctx context.Context, conn net.Conn, devPubKey p2pcrypto.Pu } func (f *flow) close() error { - var retErr error - if f.writer != nil { _ = f.writer.Close() } @@ -62,7 +60,7 @@ func (f *flow) close() error { _ = f.session.Close() } - return retErr + return nil } func (f *flow) performFlow(ctx context.Context) (crypto.SigChainManager, p2pcrypto.PubKey, error) { @@ -75,7 +73,7 @@ func (f *flow) performFlow(ctx context.Context) (crypto.SigChainManager, p2pcryp for nextStep != nil { if *nextStep == HandshakeFrame_STEP_9_DONE { if f.provedSigChain == nil || f.provedDevicePubKey == nil { - return nil, nil, ErrNoAuthReturned + return nil, nil, errcode.ErrHandshakeNoAuthReturned } return f.provedSigChain, f.provedDevicePubKey, nil @@ -85,7 +83,7 @@ func (f *flow) performFlow(ctx context.Context) (crypto.SigChainManager, p2pcryp step, ok := f.steps[*nextStep] if !ok { - return nil, nil, ErrInvalidFlowStepNotFound + return nil, nil, errcode.ErrHandshakeInvalidFlowStepNotFound } var readMsg = &HandshakeFrame{} @@ -103,11 +101,11 @@ func (f *flow) performFlow(ctx context.Context) (crypto.SigChainManager, p2pcryp } if *nextStep == currentStep { - return nil, nil, ErrInvalidFlow + return nil, nil, errcode.ErrHandshakeInvalidFlow } } - return nil, nil, ErrInvalidFlow + return nil, nil, errcode.ErrHandshakeInvalidFlow } func Request(ctx context.Context, conn net.Conn, devicePrivateKey p2pcrypto.PrivKey, sigChain crypto.SigChainManager, accountToReach p2pcrypto.PubKey, opts *crypto.Opts) (crypto.SigChainManager, p2pcrypto.PubKey, error) { diff --git a/go/internal/handshake/net_flow_test.go b/go/internal/handshake/net_flow_test.go index 71982f4047..5b896dfb75 100644 --- a/go/internal/handshake/net_flow_test.go +++ b/go/internal/handshake/net_flow_test.go @@ -11,6 +11,7 @@ import ( "time" "berty.tech/go/internal/crypto" + "berty.tech/go/pkg/errcode" ggio "github.com/gogo/protobuf/io" "github.com/gogo/protobuf/proto" p2pcrypto "github.com/libp2p/go-libp2p-core/crypto" @@ -97,7 +98,7 @@ func Test_flow_performFlow(t *testing.T) { { name: "no steps", steps: map[HandshakeFrame_HandshakeStep]flowStep{}, - expected: ErrInvalidFlowStepNotFound, + expected: errcode.ErrHandshakeInvalidFlowStepNotFound, }, { name: "single valid, no authenticated returned", @@ -106,7 +107,7 @@ func Test_flow_performFlow(t *testing.T) { next: HandshakeFrame_STEP_9_DONE, }, }, - expected: ErrNoAuthReturned, + expected: errcode.ErrHandshakeNoAuthReturned, }, { name: "single valid, read, no authenticated returned", @@ -118,7 +119,7 @@ func Test_flow_performFlow(t *testing.T) { }, }, reader: &dummyReader{msg: expectedMsg}, - expected: ErrNoAuthReturned, + expected: errcode.ErrHandshakeNoAuthReturned, }, { name: "single invalid looping", @@ -127,7 +128,7 @@ func Test_flow_performFlow(t *testing.T) { next: HandshakeFrame_STEP_1_KEY_AGREEMENT, }, }, - expected: ErrInvalidFlow, + expected: errcode.ErrHandshakeInvalidFlow, }, { name: "single invalid end", @@ -136,7 +137,7 @@ func Test_flow_performFlow(t *testing.T) { next: HandshakeFrame_STEP_2_KEY_AGREEMENT, }, }, - expected: ErrInvalidFlowStepNotFound, + expected: errcode.ErrHandshakeInvalidFlowStepNotFound, }, { name: "single invalid start", @@ -145,7 +146,7 @@ func Test_flow_performFlow(t *testing.T) { next: HandshakeFrame_STEP_9_DONE, }, }, - expected: ErrInvalidFlowStepNotFound, + expected: errcode.ErrHandshakeInvalidFlowStepNotFound, }, { name: "multiple valid, no authenticated returned", @@ -157,7 +158,7 @@ func Test_flow_performFlow(t *testing.T) { next: HandshakeFrame_STEP_9_DONE, }, }, - expected: ErrNoAuthReturned, + expected: errcode.ErrHandshakeNoAuthReturned, }, { name: "multiple valid, authenticated returned", diff --git a/go/internal/handshake/net_step_1_2_key_agreement.go b/go/internal/handshake/net_step_1_2_key_agreement.go index e1b4be2154..06462d20cc 100644 --- a/go/internal/handshake/net_step_1_2_key_agreement.go +++ b/go/internal/handshake/net_step_1_2_key_agreement.go @@ -3,8 +3,7 @@ package handshake import ( "context" - "github.com/pkg/errors" - + "berty.tech/go/pkg/errcode" "github.com/libp2p/go-libp2p-core/crypto" ) @@ -17,7 +16,7 @@ func (s *step1or2SendKeys) action(ctx context.Context, f *flow, step HandshakeFr signKey, encryptKey := f.session.GetPublicKeys() signKeyProto, err := crypto.MarshalPublicKey(signKey) if err != nil { - return nil, errors.Wrap(err, "can't unmarshal public key") + return nil, errcode.TODO.Wrap(err) } if err = f.writer.WriteMsg(&HandshakeFrame{ @@ -25,7 +24,7 @@ func (s *step1or2SendKeys) action(ctx context.Context, f *flow, step HandshakeFr SignatureKey: signKeyProto, EncryptionKey: encryptKey, }); err != nil { - return nil, errors.Wrap(err, "can't write on conn") + return nil, errcode.TODO.Wrap(err) } return &s.next, nil @@ -39,11 +38,11 @@ func (s *step1or2ReceiveKey) isReadAction() bool { return true } func (s *step1or2ReceiveKey) action(ctx context.Context, f *flow, step HandshakeFrame_HandshakeStep, readMsg *HandshakeFrame) (*HandshakeFrame_HandshakeStep, error) { signKey, err := crypto.UnmarshalPublicKey(readMsg.SignatureKey) if err != nil { - return nil, errors.Wrap(err, "can't unmarshal public key") + return nil, errcode.TODO.Wrap(err) } if err := f.session.SetOtherKeys(signKey, readMsg.EncryptionKey); err != nil { - return nil, errors.Wrap(err, "can't set keys for other peer") + return nil, errcode.TODO.Wrap(err) } return &s.next, nil diff --git a/go/internal/handshake/net_step_3_auth_challenge.go b/go/internal/handshake/net_step_3_auth_challenge.go index f37c4a045e..6a2f374744 100644 --- a/go/internal/handshake/net_step_3_auth_challenge.go +++ b/go/internal/handshake/net_step_3_auth_challenge.go @@ -3,7 +3,7 @@ package handshake import ( "context" - "github.com/pkg/errors" + "berty.tech/go/pkg/errcode" ) type step3ProveOtherKey struct { @@ -14,14 +14,14 @@ func (s *step3ProveOtherKey) isReadAction() bool { return false } func (s *step3ProveOtherKey) action(ctx context.Context, f *flow, step HandshakeFrame_HandshakeStep, readMsg *HandshakeFrame) (*HandshakeFrame_HandshakeStep, error) { sig, err := f.session.ProveOtherKey() if err != nil { - return nil, errors.Wrap(err, "can't prove other key") + return nil, errcode.TODO.Wrap(err) } err = writeEncryptedPayload(f.session, f.writer, step, &HandshakePayload{ Signature: sig, }) if err != nil { - return nil, errors.Wrap(err, "can't write on stream") + return nil, errcode.TODO.Wrap(err) } return &s.next, nil @@ -35,11 +35,11 @@ func (s *step3CheckOwnKey) isReadAction() bool { return true } func (s *step3CheckOwnKey) action(ctx context.Context, f *flow, step HandshakeFrame_HandshakeStep, readMsg *HandshakeFrame) (*HandshakeFrame_HandshakeStep, error) { payload, err := decryptPayload(f.session, readMsg.EncryptedPayload) if err != nil { - return nil, errors.Wrap(err, "can't decrypt payload") + return nil, errcode.TODO.Wrap(err) } if err := f.session.CheckOwnKeyProof(payload.Signature); err != nil { - return nil, errors.Wrap(err, "can't check other peer proof") + return nil, errcode.TODO.Wrap(err) } return &s.next, nil diff --git a/go/internal/handshake/net_step_4_5_sigchain_exchange.go b/go/internal/handshake/net_step_4_5_sigchain_exchange.go index eb6366bf76..cbd4bbfeb7 100644 --- a/go/internal/handshake/net_step_4_5_sigchain_exchange.go +++ b/go/internal/handshake/net_step_4_5_sigchain_exchange.go @@ -4,9 +4,7 @@ import ( "context" "berty.tech/go/internal/crypto" - - "github.com/pkg/errors" - + "berty.tech/go/pkg/errcode" p2pcrypto "github.com/libp2p/go-libp2p-core/crypto" ) @@ -18,18 +16,18 @@ func (s *step4or5CheckSigChainProof) isReadAction() bool { return true } func (s *step4or5CheckSigChainProof) action(ctx context.Context, f *flow, step HandshakeFrame_HandshakeStep, readMsg *HandshakeFrame) (*HandshakeFrame_HandshakeStep, error) { payload, err := decryptPayload(f.session, readMsg.EncryptedPayload) if err != nil { - return nil, errors.Wrap(err, "can't decrypt payload") + return nil, errcode.TODO.Wrap(err) } signKey, err := p2pcrypto.UnmarshalPublicKey(payload.DeviceKey) if err != nil { - return nil, errors.Wrap(err, "can't unmarshal public key") + return nil, errcode.TODO.Wrap(err) } chain := crypto.WrapSigChain(payload.SigChain, f.session.opts) if err = f.session.CheckOtherKeyProof(payload.Signature, chain, signKey); err != nil { - return nil, errors.Wrap(err, "can't check other peer key proof") + return nil, errcode.TODO.Wrap(err) } f.provedDevicePubKey = signKey @@ -46,12 +44,12 @@ func (s *step4or5SendSigChainProof) isReadAction() bool { return false } func (s *step4or5SendSigChainProof) action(ctx context.Context, f *flow, step HandshakeFrame_HandshakeStep, readMsg *HandshakeFrame) (*HandshakeFrame_HandshakeStep, error) { proof, err := f.session.ProveOwnDeviceKey() if err != nil { - return nil, errors.Wrap(err, "can't prove own device key") + return nil, errcode.TODO.Wrap(err) } devicePubKey, err := p2pcrypto.MarshalPublicKey(f.ownDevicePubKey) if err != nil { - return nil, errors.Wrap(err, "can't marshal public key") + return nil, errcode.TODO.Wrap(err) } if err := writeEncryptedPayload(f.session, f.writer, step, &HandshakePayload{ @@ -59,7 +57,7 @@ func (s *step4or5SendSigChainProof) action(ctx context.Context, f *flow, step Ha SigChain: f.ownSigChain.Unwrap(), DeviceKey: devicePubKey, }); err != nil { - return nil, errors.Wrap(err, "can't write on conn") + return nil, errcode.TODO.Wrap(err) } return &s.next, nil diff --git a/go/internal/handshake/net_utils.go b/go/internal/handshake/net_utils.go index f8c87ec8c3..3527e64d0c 100644 --- a/go/internal/handshake/net_utils.go +++ b/go/internal/handshake/net_utils.go @@ -1,14 +1,14 @@ package handshake import ( + "berty.tech/go/pkg/errcode" ggio "github.com/gogo/protobuf/io" - "github.com/pkg/errors" ) func encryptPayload(session *handshakeSession, payload *HandshakePayload) ([]byte, error) { data, err := payload.Marshal() if err != nil { - return nil, errors.Wrap(err, "can't marshal payload") + return nil, errcode.TODO.Wrap(err) } return session.Encrypt(data) @@ -21,12 +21,12 @@ func writeEncryptedPayload(session *handshakeSession, writer ggio.WriteCloser, s ) if payload == nil { - return ErrNoPayload + return errcode.ErrHandshakeNoPayload } data, err = encryptPayload(session, payload) if err != nil { - return errors.Wrap(err, "can't encrypt payload") + return errcode.TODO.Wrap(err) } return writer.WriteMsg(&HandshakeFrame{ @@ -40,11 +40,11 @@ func decryptPayload(session *handshakeSession, payload []byte) (*HandshakePayloa clear, err := session.Decrypt(payload) if err != nil { - return nil, errors.Wrap(err, "can't decrypt payload") + return nil, errcode.TODO.Wrap(err) } if err = instance.Unmarshal(clear); err != nil { - return nil, errors.Wrap(err, "can't unmarshal payload") + return nil, errcode.TODO.Wrap(err) } return instance, nil diff --git a/go/internal/ipfsutil/api_inmemory.go b/go/internal/ipfsutil/api_inmemory.go index fcb26f93c9..870d86eab4 100644 --- a/go/internal/ipfsutil/api_inmemory.go +++ b/go/internal/ipfsutil/api_inmemory.go @@ -5,6 +5,7 @@ import ( "crypto/rand" "encoding/base64" + "berty.tech/go/pkg/errcode" ipfs_datastore "github.com/ipfs/go-datastore" ipfs_datastoresync "github.com/ipfs/go-datastore/sync" ipfs_cfg "github.com/ipfs/go-ipfs-config" @@ -16,19 +17,18 @@ import ( ipfs_interface "github.com/ipfs/interface-go-ipfs-core" libp2p_ci "github.com/libp2p/go-libp2p-crypto" // nolint:staticcheck libp2p_peer "github.com/libp2p/go-libp2p-peer" // nolint:staticcheck - "github.com/pkg/errors" ) // NewInMemoryCoreAPI returns an IPFS CoreAPI based on an opininated ipfs_node.BuildCfg func NewInMemoryCoreAPI(ctx context.Context) (ipfs_interface.CoreAPI, error) { cfg, err := createBuildConfig() if err != nil { - return nil, errors.Wrap(err, "failed to create ipfs build config") + return nil, errcode.TODO.Wrap(err) } node, err := ipfs_core.NewNode(ctx, cfg) if err != nil { - return nil, errors.Wrap(err, "failed to create a new ipfs node") + return nil, errcode.TODO.Wrap(err) } return ipfs_coreapi.NewCoreAPI(node) @@ -38,7 +38,7 @@ func createBuildConfig() (*ipfs_node.BuildCfg, error) { ds := ipfs_datastore.NewMapDatastore() repo, err := createRepo(ipfs_datastoresync.MutexWrap(ds)) if err != nil { - return nil, errors.Wrap(err, "failed to create ipfs repo") + return nil, errcode.TODO.Wrap(err) } routing := ipfs_libp2p.DHTOption @@ -58,17 +58,17 @@ func createRepo(dstore ipfs_repo.Datastore) (ipfs_repo.Repo, error) { c := ipfs_cfg.Config{} priv, pub, err := libp2p_ci.GenerateKeyPairWithReader(libp2p_ci.RSA, 2048, rand.Reader) // nolint:staticcheck if err != nil { - return nil, errors.Wrap(err, "failed to create ipfs build config") + return nil, errcode.TODO.Wrap(err) } pid, err := libp2p_peer.IDFromPublicKey(pub) // nolint:staticcheck if err != nil { - return nil, errors.Wrap(err, "failed to convert public key to PeerID") + return nil, errcode.TODO.Wrap(err) } privkeyb, err := priv.Bytes() if err != nil { - return nil, errors.Wrap(err, "failed to get serialized private key") + return nil, errcode.TODO.Wrap(err) } c.Bootstrap = ipfs_cfg.DefaultBootstrapAddresses diff --git a/go/pkg/bertychat/api_account.go b/go/pkg/bertychat/api_account.go index db58583a26..fd953154de 100644 --- a/go/pkg/bertychat/api_account.go +++ b/go/pkg/bertychat/api_account.go @@ -1,19 +1,23 @@ package bertychat -import context "context" +import ( + "context" + + "berty.tech/go/pkg/errcode" +) func (c *client) AccountSettingsGet(context.Context, *AccountSettingsGetRequest) (*AccountSettingsGetReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) AccountSettingsUpdate(context.Context, *AccountSettingsUpdateRequest) (*AccountSettingsUpdateReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) AccountPairingInvitationCreate(context.Context, *AccountPairingInvitationCreateRequest) (*AccountPairingInvitationCreateReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) AccountRenewIncomingContactRequestLink(context.Context, *AccountRenewIncomingContactRequestLinkRequest) (*AccountRenewIncomingContactRequestLinkReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } diff --git a/go/pkg/bertychat/api_contact.go b/go/pkg/bertychat/api_contact.go index f8dd64cfbb..85fffd23dc 100644 --- a/go/pkg/bertychat/api_contact.go +++ b/go/pkg/bertychat/api_contact.go @@ -1,19 +1,23 @@ package bertychat -import context "context" +import ( + "context" + + "berty.tech/go/pkg/errcode" +) func (c *client) ContactList(*ContactListRequest, Account_ContactListServer) error { - return ErrNotImplemented + return errcode.ErrNotImplemented } func (c *client) ContactGet(context.Context, *ContactGetRequest) (*ContactGetReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) ContactUpdate(context.Context, *ContactUpdateRequest) (*ContactUpdateReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) ContactRemove(context.Context, *ContactRemoveRequest) (*ContactRemoveReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } diff --git a/go/pkg/bertychat/api_contactrequest.go b/go/pkg/bertychat/api_contactrequest.go index 87da6c2b6e..9c9f706568 100644 --- a/go/pkg/bertychat/api_contactrequest.go +++ b/go/pkg/bertychat/api_contactrequest.go @@ -1,15 +1,19 @@ package bertychat -import context "context" +import ( + "context" + + "berty.tech/go/pkg/errcode" +) func (c *client) ContactRequestCreate(context.Context, *ContactRequestCreateRequest) (*ContactRequestCreateReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) ContactRequestAccept(context.Context, *ContactRequestAcceptRequest) (*ContactRequestAcceptReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) ContactRequestDiscard(context.Context, *ContactRequestDiscardRequest) (*ContactRequestDiscardReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } diff --git a/go/pkg/bertychat/api_conversation.go b/go/pkg/bertychat/api_conversation.go index 3daa43833f..2707b91110 100644 --- a/go/pkg/bertychat/api_conversation.go +++ b/go/pkg/bertychat/api_conversation.go @@ -4,7 +4,7 @@ import ( "context" "math/rand" - "github.com/pkg/errors" + "berty.tech/go/pkg/errcode" ) func (c *client) ConversationList(req *ConversationListRequest, stream Account_ConversationListServer) error { @@ -13,7 +13,7 @@ func (c *client) ConversationList(req *ConversationListRequest, stream Account_C conversation := fakeConversation(c.logger) err := stream.Send(&ConversationListReply{Conversation: conversation}) if err != nil { - return errors.Wrap(err, "failed to send conversation to stream") + return errcode.TODO.Wrap(err) } } return nil @@ -21,10 +21,10 @@ func (c *client) ConversationList(req *ConversationListRequest, stream Account_C func (c *client) ConversationGet(ctx context.Context, input *ConversationGetRequest) (*ConversationGetReply, error) { if input == nil || input.ID == "" { - return nil, ErrMissingInput + return nil, errcode.ErrMissingInput } if input.ID == "invalid" { // simulating an invalid ID (tmp) - return nil, ErrInvalidInput + return nil, errcode.ErrInvalidInput } return &ConversationGetReply{ Conversation: fakeConversation(c.logger), @@ -32,21 +32,21 @@ func (c *client) ConversationGet(ctx context.Context, input *ConversationGetRequ } func (c *client) ConversationCreate(context.Context, *ConversationCreateRequest) (*ConversationCreateReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) ConversationLeave(context.Context, *ConversationLeaveRequest) (*ConversationLeaveReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) ConversationErase(context.Context, *ConversationEraseRequest) (*ConversationEraseReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) ConversationSetSeenPosition(context.Context, *ConversationSetSeenPositionRequest) (*ConversationSetSeenPositionReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) ConversationUpdateSettings(context.Context, *ConversationUpdateSettingsRequest) (*ConversationUpdateSettingsReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } diff --git a/go/pkg/bertychat/api_conversation_test.go b/go/pkg/bertychat/api_conversation_test.go index c32337a779..0a24f324a8 100644 --- a/go/pkg/bertychat/api_conversation_test.go +++ b/go/pkg/bertychat/api_conversation_test.go @@ -5,6 +5,7 @@ import ( "testing" "berty.tech/go/internal/testutil" + "berty.tech/go/pkg/errcode" ) func TestClient_ConversationGet(t *testing.T) { @@ -18,11 +19,11 @@ func TestClient_ConversationGet(t *testing.T) { { "no-input", nil, - ErrMissingInput, + errcode.ErrMissingInput, }, { "invalid-id", &ConversationGetRequest{ID: "invalid"}, - ErrInvalidInput, + errcode.ErrInvalidInput, }, { "valid-id", &ConversationGetRequest{ID: "lorem-ipsum"}, diff --git a/go/pkg/bertychat/api_conversationinvitation.go b/go/pkg/bertychat/api_conversationinvitation.go index e09c6faee3..282616d26e 100644 --- a/go/pkg/bertychat/api_conversationinvitation.go +++ b/go/pkg/bertychat/api_conversationinvitation.go @@ -1,15 +1,19 @@ package bertychat -import context "context" +import ( + "context" + + "berty.tech/go/pkg/errcode" +) func (c *client) ConversationInvitationAccept(context.Context, *ConversationInvitationAcceptRequest) (*ConversationInvitationAcceptReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) ConversationInvitationCreate(context.Context, *ConversationInvitationCreateRequest) (*ConversationInvitationCreateReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) ConversationInvitationDiscard(context.Context, *ConversationInvitationDiscardRequest) (*ConversationInvitationDiscardReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } diff --git a/go/pkg/bertychat/api_conversationmessage.go b/go/pkg/bertychat/api_conversationmessage.go index ad7f457ad6..7562184329 100644 --- a/go/pkg/bertychat/api_conversationmessage.go +++ b/go/pkg/bertychat/api_conversationmessage.go @@ -1,19 +1,23 @@ package bertychat -import context "context" +import ( + "context" + + "berty.tech/go/pkg/errcode" +) func (c *client) ConversationMessageList(*ConversationMessageListRequest, Account_ConversationMessageListServer) error { - return ErrNotImplemented + return errcode.ErrNotImplemented } func (c *client) ConversationMessageSend(context.Context, *ConversationMessageSendRequest) (*ConversationMessageSendReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) ConversationMessageEdit(context.Context, *ConversationMessageEditRequest) (*ConversationMessageEditReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) ConversationMessageHide(context.Context, *ConversationMessageHideRequest) (*ConversationMessageHideReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } diff --git a/go/pkg/bertychat/api_devtool.go b/go/pkg/bertychat/api_devtool.go index 9041e1d0b2..0397073d01 100644 --- a/go/pkg/bertychat/api_devtool.go +++ b/go/pkg/bertychat/api_devtool.go @@ -1,5 +1,7 @@ package bertychat +import "berty.tech/go/pkg/errcode" + func (c *client) DevEventSubscribe(*DevEventSubscribeRequest, Account_DevEventSubscribeServer) error { - return ErrNotImplemented + return errcode.ErrNotImplemented } diff --git a/go/pkg/bertychat/api_event.go b/go/pkg/bertychat/api_event.go index ca8b27ad71..20890a9beb 100644 --- a/go/pkg/bertychat/api_event.go +++ b/go/pkg/bertychat/api_event.go @@ -1,5 +1,7 @@ package bertychat +import "berty.tech/go/pkg/errcode" + func (c *client) EventSubscribe(*EventSubscribeRequest, Account_EventSubscribeServer) error { - return ErrNotImplemented + return errcode.ErrNotImplemented } diff --git a/go/pkg/bertychat/api_search.go b/go/pkg/bertychat/api_search.go index 3cec0bcaee..93d742a34a 100644 --- a/go/pkg/bertychat/api_search.go +++ b/go/pkg/bertychat/api_search.go @@ -1,5 +1,7 @@ package bertychat +import "berty.tech/go/pkg/errcode" + func (c *client) Search(*SearchRequest, Account_SearchServer) error { - return ErrNotImplemented + return errcode.ErrNotImplemented } diff --git a/go/pkg/bertychat/client.go b/go/pkg/bertychat/client.go index 28953571af..4b49cdf497 100644 --- a/go/pkg/bertychat/client.go +++ b/go/pkg/bertychat/client.go @@ -3,8 +3,8 @@ package bertychat import ( "berty.tech/go/internal/chatdb" "berty.tech/go/pkg/bertyprotocol" + "berty.tech/go/pkg/errcode" "github.com/jinzhu/gorm" - "github.com/pkg/errors" "go.uber.org/zap" ) @@ -45,7 +45,7 @@ func New(db *gorm.DB, protocol bertyprotocol.Client, opts Opts) (Client, error) var err error client.db, err = chatdb.InitMigrate(client.db, client.logger.Named("datastore")) if err != nil { - return nil, errors.Wrap(err, "failed to initialize datastore") + return nil, errcode.TODO.Wrap(err) } return &client, nil diff --git a/go/pkg/bertychat/errors.go b/go/pkg/bertychat/errors.go deleted file mode 100644 index b60912b031..0000000000 --- a/go/pkg/bertychat/errors.go +++ /dev/null @@ -1,17 +0,0 @@ -package bertychat - -// See https://dave.cheney.net/2016/04/07/constant-errors -type Error string - -func (e Error) Error() string { return string(e) } - -const ( - // ErrNotImplemented is a placeholder for functions that will be implemented later - ErrNotImplemented = Error("not implemented") - - // ErrInvalidInput indicates that the input contains invalid arguments - ErrInvalidInput = Error("invalid input") // FIXME: use an input validation tool - - // ErrMissingInput indicates that a mandatory input is missing - ErrMissingInput = Error("missing input") -) diff --git a/go/pkg/bertyprotocol/api_account.go b/go/pkg/bertyprotocol/api_account.go index 23cbfe6323..bd1347a98a 100644 --- a/go/pkg/bertyprotocol/api_account.go +++ b/go/pkg/bertyprotocol/api_account.go @@ -3,11 +3,11 @@ package bertyprotocol import ( "context" - "github.com/pkg/errors" + "berty.tech/go/pkg/errcode" ) func (c *client) AccountGetConfiguration(context.Context, *AccountGetConfigurationRequest) (*AccountGetConfigurationReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) AccountGetInformation(ctx context.Context, req *AccountGetInformationRequest) (*AccountGetInformationReply, error) { @@ -15,13 +15,13 @@ func (c *client) AccountGetInformation(ctx context.Context, req *AccountGetInfor key, err := c.ipfsCoreAPI.Key().Self(ctx) if err != nil { - return nil, errors.Wrap(err, "failed to get key") + return nil, errcode.TODO.Wrap(err) } ret.PeerID = key.ID().Pretty() maddrs, err := c.ipfsCoreAPI.Swarm().ListenAddrs(ctx) if err != nil { - return nil, errors.Wrap(err, "failed to get swarm listeners") + return nil, errcode.TODO.Wrap(err) } ret.Listeners = make([]string, len(maddrs)) for i, addr := range maddrs { @@ -32,17 +32,17 @@ func (c *client) AccountGetInformation(ctx context.Context, req *AccountGetInfor } func (c *client) AccountLinkNewDevice(context.Context, *AccountLinkNewDeviceRequest) (*AccountLinkNewDeviceReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) AccountDisableIncomingContactRequest(context.Context, *AccountDisableIncomingContactRequestRequest) (*AccountDisableIncomingContactRequestReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) AccountEnableIncomingContactRequest(context.Context, *AccountEnableIncomingContactRequestRequest) (*AccountEnableIncomingContactRequestReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) AccountResetIncomingContactRequestLink(context.Context, *AccountResetIncomingContactRequestLinkRequest) (*AccountResetIncomingContactRequestLinkReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } diff --git a/go/pkg/bertyprotocol/api_client.go b/go/pkg/bertyprotocol/api_client.go index a4b0221c76..959f6d0c20 100644 --- a/go/pkg/bertyprotocol/api_client.go +++ b/go/pkg/bertyprotocol/api_client.go @@ -1,11 +1,15 @@ package bertyprotocol -import "context" +import ( + "context" + + "berty.tech/go/pkg/errcode" +) func (c *client) InstanceExportData(context.Context, *InstanceExportDataRequest) (*InstanceExportDataReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) InstanceGetConfiguration(context.Context, *InstanceGetConfigurationRequest) (*InstanceGetConfigurationReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } diff --git a/go/pkg/bertyprotocol/api_contact.go b/go/pkg/bertyprotocol/api_contact.go index b7640a65db..b8ab50d6a7 100644 --- a/go/pkg/bertyprotocol/api_contact.go +++ b/go/pkg/bertyprotocol/api_contact.go @@ -1,15 +1,19 @@ package bertyprotocol -import "context" +import ( + "context" + + "berty.tech/go/pkg/errcode" +) func (c *client) ContactGet(context.Context, *ContactGetRequest) (*ContactGetReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) ContactList(*ContactListRequest, Instance_ContactListServer) error { - return ErrNotImplemented + return errcode.ErrNotImplemented } func (c *client) ContactRemove(context.Context, *ContactRemoveRequest) (*ContactRemoveReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } diff --git a/go/pkg/bertyprotocol/api_contactrequest.go b/go/pkg/bertyprotocol/api_contactrequest.go index 2d3a4899f4..7e27d8195f 100644 --- a/go/pkg/bertyprotocol/api_contactrequest.go +++ b/go/pkg/bertyprotocol/api_contactrequest.go @@ -1,23 +1,27 @@ package bertyprotocol -import "context" +import ( + "context" + + "berty.tech/go/pkg/errcode" +) func (c *client) ContactRequestAccept(context.Context, *ContactRequestAcceptRequest) (*ContactRequestAcceptReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) ContactRequestDiscard(context.Context, *ContactRequestDiscardRequest) (*ContactRequestDiscardReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) ContactRequestListIncoming(*ContactRequestListIncomingRequest, Instance_ContactRequestListIncomingServer) error { - return ErrNotImplemented + return errcode.ErrNotImplemented } func (c *client) ContactRequestListOutgoing(*ContactRequestListOutgoingRequest, Instance_ContactRequestListOutgoingServer) error { - return ErrNotImplemented + return errcode.ErrNotImplemented } func (c *client) ContactRequestSend(context.Context, *ContactRequestSendRequest) (*ContactRequestSendReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } diff --git a/go/pkg/bertyprotocol/api_event.go b/go/pkg/bertyprotocol/api_event.go index ba68e96a03..e396640159 100644 --- a/go/pkg/bertyprotocol/api_event.go +++ b/go/pkg/bertyprotocol/api_event.go @@ -1,5 +1,7 @@ package bertyprotocol +import "berty.tech/go/pkg/errcode" + func (c *client) EventSubscribe(*EventSubscribeRequest, Instance_EventSubscribeServer) error { - return ErrNotImplemented + return errcode.ErrNotImplemented } diff --git a/go/pkg/bertyprotocol/api_group.go b/go/pkg/bertyprotocol/api_group.go index 9f02dd7fa9..afed557174 100644 --- a/go/pkg/bertyprotocol/api_group.go +++ b/go/pkg/bertyprotocol/api_group.go @@ -1,35 +1,39 @@ package bertyprotocol -import "context" +import ( + "context" + + "berty.tech/go/pkg/errcode" +) func (c *client) GroupCreate(context.Context, *GroupCreateRequest) (*GroupCreateReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) GroupGenerateInviteLink(context.Context, *GroupGenerateInviteLinkRequest) (*GroupGenerateInviteLinkReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) GroupLeave(context.Context, *GroupLeaveRequest) (*GroupLeaveReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) GroupList(*GroupListRequest, Instance_GroupListServer) error { - return ErrNotImplemented + return errcode.ErrNotImplemented } func (c *client) GroupMessageCreate(context.Context, *GroupMessageCreateRequest) (*GroupMessageCreateReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) GroupMessageList(*GroupMessageListRequest, Instance_GroupMessageListServer) error { - return ErrNotImplemented + return errcode.ErrNotImplemented } func (c *client) GroupPubSubTopicInit(Instance_GroupPubSubTopicInitServer) error { - return ErrNotImplemented + return errcode.ErrNotImplemented } func (c *client) GroupPubSubTopicSubscribe(*GroupPubSubTopicSubscribeRequest, Instance_GroupPubSubTopicSubscribeServer) error { - return ErrNotImplemented + return errcode.ErrNotImplemented } diff --git a/go/pkg/bertyprotocol/api_groupinvitation.go b/go/pkg/bertyprotocol/api_groupinvitation.go index 986850effa..a6658c3dda 100644 --- a/go/pkg/bertyprotocol/api_groupinvitation.go +++ b/go/pkg/bertyprotocol/api_groupinvitation.go @@ -1,19 +1,23 @@ package bertyprotocol -import "context" +import ( + "context" + + "berty.tech/go/pkg/errcode" +) func (c *client) GroupInvitationAccept(context.Context, *GroupInvitationAcceptRequest) (*GroupInvitationAcceptReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) GroupInvitationCreate(context.Context, *GroupInvitationCreateRequest) (*GroupInvitationCreateReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) GroupInvitationDiscard(context.Context, *GroupInvitationDiscardRequest) (*GroupInvitationDiscardReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) GroupInvitationList(*GroupInvitationListRequest, Instance_GroupInvitationListServer) error { - return ErrNotImplemented + return errcode.ErrNotImplemented } diff --git a/go/pkg/bertyprotocol/api_stream.go b/go/pkg/bertyprotocol/api_stream.go index f9a13a852d..6eae36d3e0 100644 --- a/go/pkg/bertyprotocol/api_stream.go +++ b/go/pkg/bertyprotocol/api_stream.go @@ -1,11 +1,15 @@ package bertyprotocol -import "context" +import ( + "context" + + "berty.tech/go/pkg/errcode" +) func (c *client) StreamManagerRequestToContact(context.Context, *StreamManagerRequestToContactRequest) (*StreamManagerRequestToContactReply, error) { - return nil, ErrNotImplemented + return nil, errcode.ErrNotImplemented } func (c *client) StreamManagerAccept(Instance_StreamManagerAcceptServer) error { - return ErrNotImplemented + return errcode.ErrNotImplemented } diff --git a/go/pkg/bertyprotocol/client.go b/go/pkg/bertyprotocol/client.go index be91f3986b..39a57c9114 100644 --- a/go/pkg/bertyprotocol/client.go +++ b/go/pkg/bertyprotocol/client.go @@ -5,9 +5,9 @@ import ( "berty.tech/go/internal/ipfsutil" "berty.tech/go/internal/protocoldb" + "berty.tech/go/pkg/errcode" ipfs_coreapi "github.com/ipfs/interface-go-ipfs-core" "github.com/jinzhu/gorm" - "github.com/pkg/errors" "go.uber.org/zap" ) @@ -50,7 +50,7 @@ func New(db *gorm.DB, opts Opts) (Client, error) { var err error client.db, err = protocoldb.InitMigrate(db, client.logger.Named("datastore")) if err != nil { - return nil, errors.Wrap(err, "failed to initialize datastore") + return nil, errcode.TODO.Wrap(err) } ctx := opts.RootContext @@ -61,7 +61,7 @@ func New(db *gorm.DB, opts Opts) (Client, error) { var err error client.ipfsCoreAPI, err = ipfsutil.NewInMemoryCoreAPI(ctx) if err != nil { - return nil, errors.Wrap(err, "failed to initialize ipfsutil") + return nil, errcode.TODO.Wrap(err) } } diff --git a/go/pkg/bertyprotocol/errors.go b/go/pkg/bertyprotocol/errors.go deleted file mode 100644 index 9afffe3e8d..0000000000 --- a/go/pkg/bertyprotocol/errors.go +++ /dev/null @@ -1,11 +0,0 @@ -package bertyprotocol - -// See https://dave.cheney.net/2016/04/07/constant-errors -type Error string - -func (e Error) Error() string { return string(e) } - -const ( - // ErrNotImplemented is a placeholder for functions that will be implemented later - ErrNotImplemented = Error("not implemented") -) diff --git a/go/pkg/errcode/doc.go b/go/pkg/errcode/doc.go new file mode 100644 index 0000000000..acf0ccf0ea --- /dev/null +++ b/go/pkg/errcode/doc.go @@ -0,0 +1 @@ +package errcode // import "berty.tech/go/pkg/errcode" diff --git a/go/pkg/errcode/errcode.pb.go b/go/pkg/errcode/errcode.pb.go new file mode 100644 index 0000000000..9e5a90ed5e --- /dev/null +++ b/go/pkg/errcode/errcode.pb.go @@ -0,0 +1,155 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: errcode.proto + +package errcode + +import ( + fmt "fmt" + math "math" + + _ "github.com/gogo/protobuf/gogoproto" + proto "github.com/golang/protobuf/proto" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package + +type ErrCode int32 + +const ( + Undefined ErrCode = 0 + TODO ErrCode = 666 + ErrNotImplemented ErrCode = 777 + ErrInternal ErrCode = 999 + ErrInvalidInput ErrCode = 101 + ErrMissingInput ErrCode = 102 + ErrSigChainNoEntries ErrCode = 2001 + ErrSigChainInvalidEntryType ErrCode = 2002 + ErrSigChainAlreadyInitialized ErrCode = 2003 + ErrSigChainPermission ErrCode = 2004 + ErrSigChainOperationAlreadyDone ErrCode = 2005 + ErrHandshakeNoPayload ErrCode = 2006 + ErrHandshakeInvalidFlow ErrCode = 2007 + ErrHandshakeInvalidFlowStepNotFound ErrCode = 2008 + ErrHandshakeParams ErrCode = 2009 + ErrHandshakeNoAuthReturned ErrCode = 2010 + ErrHandshakeInvalidKeyType ErrCode = 2011 + ErrHandshakeInvalidSignature ErrCode = 2012 + ErrHandshakeSessionInvalid ErrCode = 2013 + ErrHandshakeKeyNotInSigChain ErrCode = 2014 + ErrHandshakeDecrypt ErrCode = 2015 + ErrBridgeInterrupted ErrCode = 3001 + ErrBridgeNotRunning ErrCode = 3002 +) + +var ErrCode_name = map[int32]string{ + 0: "Undefined", + 666: "TODO", + 777: "ErrNotImplemented", + 999: "ErrInternal", + 101: "ErrInvalidInput", + 102: "ErrMissingInput", + 2001: "ErrSigChainNoEntries", + 2002: "ErrSigChainInvalidEntryType", + 2003: "ErrSigChainAlreadyInitialized", + 2004: "ErrSigChainPermission", + 2005: "ErrSigChainOperationAlreadyDone", + 2006: "ErrHandshakeNoPayload", + 2007: "ErrHandshakeInvalidFlow", + 2008: "ErrHandshakeInvalidFlowStepNotFound", + 2009: "ErrHandshakeParams", + 2010: "ErrHandshakeNoAuthReturned", + 2011: "ErrHandshakeInvalidKeyType", + 2012: "ErrHandshakeInvalidSignature", + 2013: "ErrHandshakeSessionInvalid", + 2014: "ErrHandshakeKeyNotInSigChain", + 2015: "ErrHandshakeDecrypt", + 3001: "ErrBridgeInterrupted", + 3002: "ErrBridgeNotRunning", +} + +var ErrCode_value = map[string]int32{ + "Undefined": 0, + "TODO": 666, + "ErrNotImplemented": 777, + "ErrInternal": 999, + "ErrInvalidInput": 101, + "ErrMissingInput": 102, + "ErrSigChainNoEntries": 2001, + "ErrSigChainInvalidEntryType": 2002, + "ErrSigChainAlreadyInitialized": 2003, + "ErrSigChainPermission": 2004, + "ErrSigChainOperationAlreadyDone": 2005, + "ErrHandshakeNoPayload": 2006, + "ErrHandshakeInvalidFlow": 2007, + "ErrHandshakeInvalidFlowStepNotFound": 2008, + "ErrHandshakeParams": 2009, + "ErrHandshakeNoAuthReturned": 2010, + "ErrHandshakeInvalidKeyType": 2011, + "ErrHandshakeInvalidSignature": 2012, + "ErrHandshakeSessionInvalid": 2013, + "ErrHandshakeKeyNotInSigChain": 2014, + "ErrHandshakeDecrypt": 2015, + "ErrBridgeInterrupted": 3001, + "ErrBridgeNotRunning": 3002, +} + +func (ErrCode) EnumDescriptor() ([]byte, []int) { + return fileDescriptor_4240057316120df7, []int{0} +} + +func init() { + proto.RegisterEnum("berty.errcode.ErrCode", ErrCode_name, ErrCode_value) +} + +func init() { proto.RegisterFile("errcode.proto", fileDescriptor_4240057316120df7) } + +var fileDescriptor_4240057316120df7 = []byte{ + // 599 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x93, 0xbd, 0x4f, 0x14, 0x4f, + 0x18, 0xc7, 0x1f, 0x92, 0x1f, 0x10, 0xe6, 0x17, 0xb2, 0xe3, 0xa0, 0x9e, 0x20, 0x3e, 0xf8, 0x56, + 0x18, 0x13, 0xb9, 0xc2, 0xbf, 0x80, 0x97, 0x23, 0x5e, 0x88, 0x07, 0xe1, 0xb0, 0xb1, 0xdb, 0xbb, + 0x7d, 0xd8, 0x9b, 0x70, 0x37, 0xb3, 0x99, 0x9b, 0xd5, 0x9c, 0x15, 0x25, 0x56, 0x5a, 0x1a, 0x6d, + 0x2c, 0x29, 0x29, 0xd1, 0x8a, 0x92, 0x12, 0xdf, 0xcf, 0x77, 0x76, 0xb7, 0x90, 0x92, 0x92, 0xd2, + 0xdc, 0xde, 0x92, 0xac, 0x41, 0xbb, 0x99, 0xe7, 0xfb, 0x99, 0xef, 0x4c, 0x9e, 0x79, 0xbe, 0x6c, + 0x94, 0x8c, 0xa9, 0x6b, 0x8f, 0xa6, 0x03, 0xa3, 0xad, 0x16, 0xa3, 0x35, 0x32, 0xb6, 0x33, 0x9d, + 0x15, 0x27, 0x6e, 0xf9, 0xd2, 0x36, 0xc2, 0xda, 0x74, 0x5d, 0xb7, 0x8a, 0xbe, 0xf6, 0x75, 0x31, + 0xa5, 0x6a, 0xe1, 0x5a, 0xba, 0x4b, 0x37, 0xe9, 0xaa, 0x7f, 0xfa, 0xe6, 0x93, 0x41, 0x36, 0x5c, + 0x32, 0x66, 0x4e, 0x7b, 0x24, 0x46, 0xd9, 0xc8, 0x3d, 0xe5, 0xd1, 0x9a, 0x54, 0xe4, 0x71, 0x10, + 0x23, 0xec, 0xbf, 0xd5, 0xa5, 0xf9, 0x25, 0xfe, 0x7c, 0x50, 0x9c, 0x67, 0x67, 0x4a, 0xc6, 0x54, + 0xb4, 0x2d, 0xb7, 0x82, 0x26, 0xb5, 0x48, 0x59, 0xf2, 0xf8, 0xe3, 0x21, 0xc1, 0xd9, 0xff, 0x25, + 0x63, 0xca, 0xca, 0x92, 0x51, 0x6e, 0x93, 0xff, 0x1a, 0x16, 0x63, 0xcc, 0x49, 0x2b, 0x0f, 0xdc, + 0xa6, 0xf4, 0xca, 0x2a, 0x08, 0x2d, 0xa7, 0xac, 0x78, 0x57, 0xb6, 0xdb, 0x52, 0xf9, 0xfd, 0xe2, + 0x9a, 0x18, 0x67, 0x67, 0x4b, 0xc6, 0x54, 0xa5, 0x3f, 0xd7, 0x70, 0xa5, 0xaa, 0xe8, 0x92, 0xb2, + 0x46, 0x52, 0x9b, 0xbf, 0x71, 0xc4, 0x65, 0x76, 0x31, 0x27, 0x65, 0x66, 0x3d, 0xbd, 0xb3, 0xda, + 0x09, 0x88, 0xbf, 0x75, 0xc4, 0x55, 0x76, 0x29, 0x47, 0xcc, 0x34, 0x0d, 0xb9, 0x5e, 0xa7, 0xac, + 0xa4, 0x95, 0x6e, 0x53, 0x3e, 0x22, 0x8f, 0xbf, 0x73, 0xc4, 0x04, 0x3b, 0x97, 0x63, 0x96, 0xc9, + 0xb4, 0x7a, 0x0f, 0xd0, 0x8a, 0xbf, 0x77, 0xc4, 0x75, 0x36, 0x95, 0xd3, 0x96, 0x02, 0x32, 0xae, + 0x95, 0xfa, 0xc4, 0x68, 0x5e, 0x2b, 0xe2, 0x1f, 0x4e, 0x1c, 0xee, 0xb8, 0xca, 0x6b, 0x37, 0xdc, + 0x75, 0xaa, 0xe8, 0x65, 0xb7, 0xd3, 0xd4, 0xae, 0xc7, 0x3f, 0x3a, 0x62, 0x92, 0x15, 0xf2, 0x5a, + 0xf6, 0xc8, 0x85, 0xa6, 0x7e, 0xc8, 0x3f, 0x39, 0xe2, 0x06, 0xbb, 0xf6, 0x0f, 0xb5, 0x6a, 0x29, + 0xa8, 0x68, 0xbb, 0xa0, 0x43, 0xe5, 0xf1, 0xae, 0x23, 0x0a, 0x4c, 0xe4, 0xc9, 0x65, 0xd7, 0xb8, + 0xad, 0x36, 0xff, 0xec, 0x88, 0x29, 0x36, 0xf1, 0xe7, 0xe5, 0x33, 0xa1, 0x6d, 0xac, 0x90, 0x0d, + 0x4d, 0xef, 0x7b, 0xbe, 0x9c, 0x02, 0xb2, 0x3b, 0x16, 0xa9, 0xdf, 0xa4, 0xaf, 0x8e, 0xb8, 0xc2, + 0x26, 0xff, 0x02, 0x54, 0xa5, 0xaf, 0x5c, 0x1b, 0x1a, 0xe2, 0xdf, 0x4e, 0x79, 0x54, 0x29, 0xed, + 0x50, 0x46, 0xf2, 0xef, 0xa7, 0x3c, 0x16, 0xa9, 0xd3, 0x9b, 0x02, 0x75, 0xd2, 0x39, 0xfe, 0xc3, + 0x11, 0x17, 0xd8, 0x58, 0x1e, 0x99, 0xa7, 0xba, 0xe9, 0x04, 0x96, 0xff, 0x74, 0xb2, 0x2f, 0x9e, + 0x35, 0xd2, 0xf3, 0x29, 0x1d, 0x12, 0x13, 0x06, 0xbd, 0xc9, 0x79, 0x55, 0xc8, 0x0e, 0xf5, 0xa5, + 0x8a, 0xb6, 0x2b, 0xa1, 0x52, 0x52, 0xf9, 0xfc, 0x75, 0x61, 0xf6, 0xc5, 0xc0, 0x5e, 0x84, 0xb0, + 0x1f, 0x21, 0x74, 0x23, 0x84, 0x83, 0x08, 0xe1, 0x30, 0x42, 0x38, 0x8a, 0x10, 0x8e, 0x23, 0x84, + 0x8d, 0x18, 0x61, 0x33, 0x46, 0xd8, 0x8a, 0x11, 0xb6, 0x63, 0x84, 0x9d, 0x18, 0x61, 0x37, 0x46, + 0xd8, 0x8b, 0x11, 0xf6, 0x63, 0x84, 0x6e, 0x8c, 0x70, 0x10, 0x23, 0x1c, 0xc6, 0x08, 0x47, 0x31, + 0xc2, 0x71, 0x8c, 0xb0, 0x91, 0x20, 0x6c, 0x26, 0x08, 0x4f, 0x13, 0x84, 0x67, 0x09, 0xc2, 0xcb, + 0x04, 0x61, 0x2b, 0x41, 0xd8, 0x4e, 0x10, 0x76, 0x12, 0x1c, 0xd8, 0x4d, 0x10, 0xf6, 0x12, 0x84, + 0xfd, 0x04, 0xa1, 0x9b, 0x20, 0xdc, 0x1f, 0xef, 0x47, 0xca, 0x52, 0xbd, 0x51, 0xec, 0x25, 0x68, + 0xdd, 0x2f, 0x66, 0xf1, 0xaa, 0x0d, 0xa5, 0xb1, 0xb9, 0xfd, 0x3b, 0x00, 0x00, 0xff, 0xff, 0x0a, + 0xe5, 0xc6, 0xf3, 0x85, 0x03, 0x00, 0x00, +} diff --git a/go/pkg/errcode/error.go b/go/pkg/errcode/error.go new file mode 100644 index 0000000000..283f701829 --- /dev/null +++ b/go/pkg/errcode/error.go @@ -0,0 +1,67 @@ +package errcode + +import "fmt" + +type WithCode interface { + error + Code() int32 +} + +// Code returns the code of the +func Code(err error) int32 { + typed, ok := err.(WithCode) + if ok { + return typed.Code() + } + return -1 +} + +// +// Error +// + +func (e ErrCode) Error() string { + name, ok := ErrCode_name[int32(e)] + if ok { + return fmt.Sprintf("%s(#%d)", name, e) + } + return fmt.Sprintf("UNKNOWN_ERRCODE(#%d)", e) +} + +func (e ErrCode) Code() int32 { + return int32(e) +} + +func (e ErrCode) Wrap(inner error) WithCode { + return wrappedError{ + code: int32(e), + inner: inner, + } +} + +// +// ConfigurableError +// + +type wrappedError struct { + code int32 + inner error +} + +func (e wrappedError) Error() string { + return fmt.Sprintf("%s: %v", ErrCode(e.code), e.inner) +} + +func (e wrappedError) Code() int32 { + return e.code +} + +// Cause returns the inner error (github.com/pkg/errors) +func (e wrappedError) Cause() error { + return e.inner +} + +// Unwrap returns the inner error (go1.13) +func (e wrappedError) Unwrap() error { + return e.inner +} diff --git a/go/pkg/errcode/error_test.go b/go/pkg/errcode/error_test.go new file mode 100644 index 0000000000..32b185f2f6 --- /dev/null +++ b/go/pkg/errcode/error_test.go @@ -0,0 +1,111 @@ +package errcode + +import ( + "fmt" + "testing" + + "github.com/pkg/errors" +) + +func TestError(t *testing.T) { + // test instance + var ( + _ ErrCode = ErrNotImplemented + _ error = ErrNotImplemented + _ WithCode = ErrNotImplemented + ) + + // table-driven tests + var ( + errStdHello = fmt.Errorf("hello") + errCodeUndef = ErrCode(65530) // simulate a client receiving an error generated from a more recent API + ) + var tests = []struct { + name string + input error + expectedString string + expectedCode int32 + expectedCause error + }{ + { + "ErrNotImplemented", + ErrNotImplemented, + "ErrNotImplemented(#777)", + 777, + ErrNotImplemented, + }, { + "ErrInternal", + ErrInternal, + "ErrInternal(#999)", + 999, + ErrInternal, + }, { + "ErrNotImplemented.Wrap(errStdHello)", + ErrNotImplemented.Wrap(errStdHello), + "ErrNotImplemented(#777): hello", + 777, + errStdHello, + }, { + "ErrNotImplemented.Wrap(ErrInternal)", + ErrNotImplemented.Wrap(ErrInternal), + "ErrNotImplemented(#777): ErrInternal(#999)", + 777, + ErrInternal, + }, { + "ErrNotImplemented.Wrap(ErrInternal.Wrap(errStdHello))", + ErrNotImplemented.Wrap(ErrInternal.Wrap(errStdHello)), + "ErrNotImplemented(#777): ErrInternal(#999): hello", + 777, + errStdHello, + }, { + `errors.Wrap(ErrNotImplemented, "blah")`, + errors.Wrap(ErrNotImplemented, "blah"), + "blah: ErrNotImplemented(#777)", + -1, + ErrNotImplemented, + }, { + `errors.Wrap(ErrNotImplemented.Wrap(ErrInternal), "blah")`, + errors.Wrap(ErrNotImplemented.Wrap(ErrInternal), "blah"), + "blah: ErrNotImplemented(#777): ErrInternal(#999)", + -1, + ErrInternal, + }, { + "nil", + nil, + "", + -1, + nil, + }, { + "errStdHello", + errStdHello, + "hello", + -1, + errStdHello, + }, { + "errCodeUndef", + errCodeUndef, + "UNKNOWN_ERRCODE(#65530)", + 65530, + errCodeUndef, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + actualString := fmt.Sprint(test.input) + if test.expectedString != actualString { + t.Errorf("Expected string to be %q, got %q.", test.expectedString, actualString) + } + + actualCode := Code(test.input) + if test.expectedCode != actualCode { + t.Errorf("Expected code to be %d, got %d.", test.expectedCode, actualCode) + } + + actualCause := errors.Cause(test.input) + if test.expectedCause != actualCause { + t.Errorf("Expected cause to be %v, got %v.", test.expectedCause, actualCause) + } + }) + } +}