mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-09-07 00:55:55 +00:00
using json to move CodedErrors across boundaries and associated tests
This commit is contained in:
parent
3692635391
commit
aa746bd254
6 changed files with 97 additions and 46 deletions
|
|
@ -95,8 +95,7 @@ func (c *Client) DropDatabase(ctx context.Context, qdbid dax.QualifiedDatabaseID
|
|||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
b, _ := io.ReadAll(resp.Body)
|
||||
return errors.Errorf("status code: %d: %s", resp.StatusCode, b)
|
||||
return errors.Wrapf(errors.UnmarshalJSON(resp.Body), "status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
@ -121,8 +120,8 @@ func (c *Client) DatabaseByID(ctx context.Context, qdbid dax.QualifiedDatabaseID
|
|||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
b, _ := io.ReadAll(resp.Body)
|
||||
return nil, errors.Errorf("status code: %d: %s", resp.StatusCode, b)
|
||||
// b, _ := io.ReadAll(resp.Body)
|
||||
return nil, errors.Wrapf(errors.UnmarshalJSON(resp.Body), "status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var qdb *dax.QualifiedDatabase
|
||||
|
|
@ -192,8 +191,7 @@ func (c *Client) Databases(ctx context.Context, orgID dax.OrganizationID, ids ..
|
|||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
b, _ := io.ReadAll(resp.Body)
|
||||
return nil, errors.Errorf("status code: %d: %s", resp.StatusCode, b)
|
||||
return nil, errors.Wrapf(errors.UnmarshalJSON(resp.Body), "status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var qdbs []*dax.QualifiedDatabase
|
||||
|
|
@ -235,8 +233,7 @@ func (c *Client) SetDatabaseOption(ctx context.Context, qdbid dax.QualifiedDatab
|
|||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
b, _ := io.ReadAll(resp.Body)
|
||||
return errors.Errorf("status code: %d: %s", resp.StatusCode, b)
|
||||
return errors.Wrapf(errors.UnmarshalJSON(resp.Body), "status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
@ -275,8 +272,7 @@ func (c *Client) Table(ctx context.Context, qtid dax.QualifiedTableID) (*dax.Qua
|
|||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
b, _ := io.ReadAll(resp.Body)
|
||||
return nil, errors.Errorf("status code: %d: %s", resp.StatusCode, b)
|
||||
return nil, errors.Wrapf(errors.UnmarshalJSON(resp.Body), "status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var qtable *dax.QualifiedTable
|
||||
|
|
@ -312,8 +308,7 @@ func (c *Client) TableID(ctx context.Context, qdbid dax.QualifiedDatabaseID, nam
|
|||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
b, _ := io.ReadAll(resp.Body)
|
||||
return dflt, errors.Errorf("status code: %d: %s", resp.StatusCode, b)
|
||||
return dflt, errors.Wrapf(errors.UnmarshalJSON(resp.Body), "status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var qtid dax.QualifiedTableID
|
||||
|
|
@ -348,8 +343,7 @@ func (c *Client) Tables(ctx context.Context, qdbid dax.QualifiedDatabaseID, ids
|
|||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
b, _ := io.ReadAll(resp.Body)
|
||||
return nil, errors.Errorf("status code: %d: %s", resp.StatusCode, b)
|
||||
return nil, errors.Wrapf(errors.UnmarshalJSON(resp.Body), "Status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var qtables []*dax.QualifiedTable
|
||||
|
|
@ -378,8 +372,7 @@ func (c *Client) CreateTable(ctx context.Context, qtbl *dax.QualifiedTable) erro
|
|||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
b, _ := io.ReadAll(resp.Body)
|
||||
return errors.Errorf("status code: %d: %s", resp.StatusCode, b)
|
||||
return errors.Wrapf(errors.UnmarshalJSON(resp.Body), "status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
@ -403,8 +396,7 @@ func (c *Client) DropTable(ctx context.Context, qtid dax.QualifiedTableID) error
|
|||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
b, _ := io.ReadAll(resp.Body)
|
||||
return errors.Errorf("status code: %d: %s", resp.StatusCode, b)
|
||||
return errors.Wrapf(errors.UnmarshalJSON(resp.Body), "status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
@ -432,8 +424,7 @@ func (c *Client) CreateField(ctx context.Context, qtid dax.QualifiedTableID, fld
|
|||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
b, _ := io.ReadAll(resp.Body)
|
||||
return errors.Errorf("status code: %d: %s", resp.StatusCode, b)
|
||||
return errors.Wrapf(errors.UnmarshalJSON(resp.Body), "status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
@ -462,8 +453,7 @@ func (c *Client) DropField(ctx context.Context, qtid dax.QualifiedTableID, fldNa
|
|||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
b, _ := io.ReadAll(resp.Body)
|
||||
return errors.Errorf("status code: %d: %s", resp.StatusCode, b)
|
||||
return errors.Wrapf(errors.UnmarshalJSON(resp.Body), "status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -741,6 +741,10 @@ func (c *Controller) SetDatabaseOption(ctx context.Context, qdbid dax.QualifiedD
|
|||
}
|
||||
|
||||
func (c *Controller) Databases(ctx context.Context, orgID dax.OrganizationID, ids ...dax.DatabaseID) ([]*dax.QualifiedDatabase, error) {
|
||||
if orgID == "" {
|
||||
return nil, dax.NewErrOrganizationIDDoesNotExist(orgID)
|
||||
}
|
||||
|
||||
tx, err := c.BoltDB.BeginTx(ctx, false)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "beginning tx")
|
||||
|
|
|
|||
|
|
@ -106,7 +106,7 @@ func (s *server) postDropDatabase(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
err := s.controller.DropDatabase(ctx, req)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
http.Error(w, errors.MarshalJSON(err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
@ -125,7 +125,7 @@ func (s *server) postDatabaseByID(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
resp, err := s.controller.DatabaseByID(ctx, qdbid)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
http.Error(w, errors.MarshalJSON(err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -181,7 +181,7 @@ func (s *server) postDatabases(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
resp, err := s.controller.Databases(ctx, req.OrganizationID, ids...)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
http.Error(w, errors.MarshalJSON(err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -209,7 +209,7 @@ func (s *server) patchDatabaseOptions(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
if err := s.controller.SetDatabaseOption(r.Context(), req.QualifiedDatabaseID, req.Option, req.Value); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
http.Error(w, errors.MarshalJSON(err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
@ -238,7 +238,7 @@ func (s *server) postCreateTable(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
err := s.controller.CreateTable(ctx, req)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
http.Error(w, errors.MarshalJSON(err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -262,7 +262,7 @@ func (s *server) postTable(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
resp, err := s.controller.TableByID(ctx, qtid)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
http.Error(w, errors.MarshalJSON(err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -287,7 +287,7 @@ func (s *server) postTableID(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
qtbl, err := s.controller.TableByName(ctx, req.QualifiedDatabaseID, req.Name)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
http.Error(w, errors.MarshalJSON(err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
qtid := qtbl.QualifiedID()
|
||||
|
|
@ -313,7 +313,7 @@ func (s *server) postDropTable(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
err := s.controller.DropTable(ctx, req)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
http.Error(w, errors.MarshalJSON(err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
@ -335,7 +335,7 @@ func (s *server) postCreateField(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
err := s.controller.CreateField(ctx, qtid, req.Field)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
http.Error(w, errors.MarshalJSON(err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
@ -362,7 +362,7 @@ func (s *server) postDropField(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
err := s.controller.DropField(ctx, qtid, req.Field)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
http.Error(w, errors.MarshalJSON(err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
@ -390,7 +390,7 @@ func (s *server) postTables(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
resp, err := s.controller.Tables(ctx, qdbid, ids...)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
http.Error(w, errors.MarshalJSON(err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
ErrOrganizationIDDoesNotExist errors.Code = "OrganizationIDDoesNotExist"
|
||||
|
||||
ErrDatabaseIDExists errors.Code = "DatabaseIDExists"
|
||||
ErrDatabaseIDDoesNotExist errors.Code = "DatabaseIDDoesNotExist"
|
||||
ErrDatabaseNameDoesNotExist errors.Code = "DatabaseNameDoesNotExist"
|
||||
|
|
@ -29,6 +31,13 @@ const (
|
|||
// The following are helper functions for constructing coded errors containing
|
||||
// relevant information about the specific error.
|
||||
|
||||
func NewErrOrganizationIDDoesNotExist(orgID OrganizationID) error {
|
||||
return errors.New(
|
||||
ErrOrganizationIDDoesNotExist,
|
||||
fmt.Sprintf("Organization ID '%s' does not exist", orgID),
|
||||
)
|
||||
}
|
||||
|
||||
func NewErrDatabaseIDExists(qdbid QualifiedDatabaseID) error {
|
||||
return errors.New(
|
||||
ErrDatabaseIDExists,
|
||||
|
|
|
|||
|
|
@ -739,60 +739,106 @@ func TestDAXIntegration(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("Schemar", func(t *testing.T) {
|
||||
|
||||
qtid := dax.QualifiedTableID{
|
||||
ID: "BAD",
|
||||
}
|
||||
qtbl := dax.QualifiedTable{}
|
||||
qdbid := dax.QualifiedDatabaseID{}
|
||||
tbfld := &dax.Field{}
|
||||
|
||||
t.Run("CreateDatabase", func(t *testing.T) {
|
||||
err := client.CreateDatabase(ctx, nil)
|
||||
log.Printf("ERR: %v", err)
|
||||
if assert.Error(t, err) {
|
||||
assert.True(t, errors.Is(err, schemar.ErrCodeDatabaseNameInvalid))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("DropDatabase", func(t *testing.T) {
|
||||
//DropDatabase(context.Context, QualifiedDatabaseID) error
|
||||
err := client.DropDatabase(ctx, qdbid)
|
||||
if assert.Error(t, err) {
|
||||
assert.True(t, errors.Is(err, dax.ErrDatabaseIDDoesNotExist))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("DatabaseByName", func(t *testing.T) {
|
||||
// DatabaseByName(ctx context.Context, orgID OrganizationID, dbname DatabaseName) (*QualifiedDatabase, error)
|
||||
_, err := client.DatabaseByName(ctx, "", "")
|
||||
if assert.Error(t, err) {
|
||||
assert.True(t, errors.Is(err, dax.ErrDatabaseNameDoesNotExist))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("DatabaseByID", func(t *testing.T) {
|
||||
// DatabaseByID(ctx context.Context, qdbid QualifiedDatabaseID) (*QualifiedDatabase, error)
|
||||
_, err := client.DatabaseByID(ctx, qdbid)
|
||||
if assert.Error(t, err) {
|
||||
assert.True(t, errors.Is(err, dax.ErrDatabaseIDDoesNotExist))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("SetDatabaseOption", func(t *testing.T) {
|
||||
// SetDatabaseOption(ctx context.Context, qdbid QualifiedDatabaseID, option string, value string) error
|
||||
err := client.SetDatabaseOption(ctx, qdbid, "", "")
|
||||
if assert.Error(t, err) {
|
||||
assert.True(t, errors.Is(err, dax.ErrDatabaseIDDoesNotExist))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Databases", func(t *testing.T) {
|
||||
// Databases(context.Context, OrganizationID, ...DatabaseID) ([]*QualifiedDatabase, error)
|
||||
_, err := client.Databases(ctx, "", dbID)
|
||||
if assert.Error(t, err) {
|
||||
assert.True(t, errors.Is(err, dax.ErrOrganizationIDDoesNotExist))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("CreateTable", func(t *testing.T) {
|
||||
// CreateTable(ctx context.Context, qtbl *QualifiedTable) error
|
||||
err := client.CreateTable(ctx, &qtbl)
|
||||
if assert.Error(t, err) {
|
||||
assert.True(t, errors.Is(err, schemar.ErrCodeTableNameInvalid))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("DropTable", func(t *testing.T) {
|
||||
// DropTable(ctx context.Context, qtid QualifiedTableID) error
|
||||
err := client.DropTable(ctx, qtid)
|
||||
if assert.Error(t, err) {
|
||||
assert.True(t, errors.Is(err, dax.ErrTableIDDoesNotExist))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("TableByName", func(t *testing.T) {
|
||||
// TableByName(ctx context.Context, qdbid QualifiedDatabaseID, tname TableName) (*QualifiedTable, error)
|
||||
req := dax.QualifiedTableID{}
|
||||
_, err := client.TableByName(ctx, qdbid, req.Name)
|
||||
if assert.Error(t, err) {
|
||||
assert.True(t, errors.Is(err, dax.ErrTableNameDoesNotExist))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("TableByID", func(t *testing.T) {
|
||||
// TableByID(ctx context.Context, qtid QualifiedTableID) (*QualifiedTable, error)
|
||||
_, err := client.TableByID(ctx, qtid)
|
||||
if assert.Error(t, err) {
|
||||
assert.True(t, errors.Is(err, dax.ErrTableIDDoesNotExist))
|
||||
}
|
||||
})
|
||||
|
||||
//Todo: make it so "Tables" doesn't return all tables if error is present
|
||||
t.Run("Tables", func(t *testing.T) {
|
||||
// Tables(ctx context.Context, qdbid QualifiedDatabaseID, tids ...TableID) ([]*QualifiedTable, error)
|
||||
// _, err := client.Tables(ctx, qdbid)
|
||||
// //the error is also nil
|
||||
// if err != nil {
|
||||
// assert.True(t, errors.Is(err, ""))
|
||||
// }
|
||||
})
|
||||
|
||||
t.Run("CreateField", func(t *testing.T) {
|
||||
// CreateField(ctx context.Context, qtid QualifiedTableID, fld *Field) error
|
||||
err := client.CreateField(ctx, qtid, tbfld)
|
||||
if assert.Error(t, err) {
|
||||
assert.True(t, errors.Is(err, schemar.ErrCodeFieldNameInvalid))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("DropField", func(t *testing.T) {
|
||||
// DropField(ctx context.Context, qtid QualifiedTableID, fname FieldName) error
|
||||
err := client.DropField(ctx, qtid, tbfld.Name)
|
||||
if assert.Error(t, err) {
|
||||
assert.True(t, errors.Is(err, dax.ErrTableIDDoesNotExist))
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
|||
|
|
@ -28,6 +28,8 @@ func newWrappedControllerClient(cli *controllerclient.Client) *wrappedController
|
|||
}
|
||||
}
|
||||
|
||||
// Registrar
|
||||
|
||||
func (w *wrappedControllerClient) RegisterNode(ctx context.Context, node *dax.Node) error {
|
||||
return w.cli.RegisterNode(ctx, node)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue