Skip to content

Commit

Permalink
improve error in native.go
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Moreno <[email protected]>
  • Loading branch information
amorenoz committed Mar 5, 2021
1 parent 2b6f18b commit 7cfd014
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions native.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ import (
"fmt"
)

// ErrNoTable describes a error in the provided table information
type ErrNoTable struct {
table string
}

func (e *ErrNoTable) Error() string {
return fmt.Sprintf("Table not found: %s", e.table)
}

// NewErrNoTable creates a new ErrNoTable
func NewErrNoTable(table string) error {
return &ErrNoTable{
table: table,
}
}

// NativeAPI is an API that offers functions to interact with libovsdb without
// having to handle it's internal objects. It uses a DatabaseSchema to infer the
// type of each value and make translations.
Expand Down Expand Up @@ -42,7 +58,7 @@ func (na NativeAPI) GetRowData(tableName string, row *Row) (map[string]interface
func (na NativeAPI) GetData(tableName string, ovsData map[string]interface{}) (map[string]interface{}, error) {
table, ok := na.schema.Tables[tableName]
if !ok {
return nil, fmt.Errorf("TableName not found in schema %s", tableName)
return nil, NewErrNoTable(tableName)
}
nativeRow := make(map[string]interface{}, len(table.Columns))

Expand All @@ -66,12 +82,11 @@ func (na NativeAPI) GetData(tableName string, ovsData map[string]interface{}) (m
func (na NativeAPI) NewRow(tableName string, data interface{}) (map[string]interface{}, error) {
table, ok := na.schema.Tables[tableName]
if !ok {
return nil, fmt.Errorf("TableName not found in schema %s", tableName)
return nil, NewErrNoTable(tableName)
}

nativeRow, ok := data.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("NativeAPI.NewRow requires data to be *map[string]interface{}")
return nil, NewErrWrongType("NativeAPI.NewRow", "map[string]interface{}", data)
}

ovsRow := make(map[string]interface{}, len(table.Columns))
Expand All @@ -83,7 +98,7 @@ func (na NativeAPI) NewRow(tableName string, data interface{}) (map[string]inter
}
ovsElem, err := NativeToOvs(column, nativeElem)
if err != nil {
return nil, fmt.Errorf("Table %s, Column %s: Failed to generate OvS element: %s", tableName, name, err.Error())
return nil, fmt.Errorf("Table %s, Column %s: Failed to generate OvS element. %s", tableName, name, err.Error())
}
ovsRow[name] = ovsElem
}
Expand Down

0 comments on commit 7cfd014

Please sign in to comment.