From 7cfd014126e5a752abadf677360b775f771ad29c Mon Sep 17 00:00:00 2001 From: Adrian Moreno Date: Thu, 18 Feb 2021 13:18:26 +0100 Subject: [PATCH] improve error in native.go Signed-off-by: Adrian Moreno --- native.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/native.go b/native.go index 76acabc7..8aa74be3 100644 --- a/native.go +++ b/native.go @@ -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. @@ -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)) @@ -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)) @@ -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 }