SEBA-705 Wrap all gRPC methods;
translate permission errors;
print prefix only when emitting final error message

Change-Id: I3cbb0cf8f321c965d11354cc01491f2b3c0dc1b3
diff --git a/commands/common.go b/commands/common.go
index 12b869f..c79c3ca 100644
--- a/commands/common.go
+++ b/commands/common.go
@@ -27,7 +27,6 @@
 	"golang.org/x/net/context"
 	"google.golang.org/grpc"
 	reflectpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
-	"google.golang.org/grpc/status"
 	"log"
 	"os"
 	"strings"
@@ -57,11 +56,11 @@
 	h := &RpcEventHandler{}
 	err := grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.utility.GetVersion", headers, h, h.GetParams)
 	if err != nil {
-		return nil, err
+		return nil, corderrors.RpcErrorToCordError(err)
 	}
 
 	if h.Status != nil && h.Status.Err() != nil {
-		return nil, h.Status.Err()
+		return nil, corderrors.RpcErrorToCordError(h.Status.Err())
 	}
 
 	d, err := dynamic.AsDynamicMessage(h.Response)
@@ -167,16 +166,3 @@
 		}
 	}
 }
-
-func HumanReadableError(err error) string {
-	st, ok := status.FromError(err)
-	if ok {
-		grpc_message := st.Message()
-		if strings.HasPrefix(grpc_message, "Exception calling application: ") {
-			return st.Message()[31:]
-		} else {
-			return st.Message()
-		}
-	}
-	return err.Error()
-}
diff --git a/commands/models.go b/commands/models.go
index e539bd9..e413178 100644
--- a/commands/models.go
+++ b/commands/models.go
@@ -162,9 +162,9 @@
 func UpdateModelStatusOutput(output *ModelStatusOutput, i int, id interface{}, status string, err error, final bool) {
 	if err != nil {
 		if output.Unbuffered {
-			fmt.Printf("%v: %s\n", id, HumanReadableError(err))
+			fmt.Printf("%v: %s\n", id, err)
 		}
-		output.Rows[i] = ModelStatusOutputRow{Id: id, Message: HumanReadableError(err)}
+		output.Rows[i] = ModelStatusOutputRow{Id: id, Message: err.Error()}
 	} else {
 		if output.Unbuffered && final {
 			fmt.Println(id)
diff --git a/commands/models_test.go b/commands/models_test.go
index 3a6031b..1f9abed 100644
--- a/commands/models_test.go
+++ b/commands/models_test.go
@@ -350,7 +350,7 @@
 }
 
 func TestModelDeleteNoExist(t *testing.T) {
-	expected := `[{"id":77, "message":"Slice matching query does not exist."}]`
+	expected := `[{"id":77, "message":"Not Found [on model Slice <id=77>]"}]`
 
 	got := new(bytes.Buffer)
 	OutputStream = got
diff --git a/commands/orm.go b/commands/orm.go
index f57f21d..1d700d7 100644
--- a/commands/orm.go
+++ b/commands/orm.go
@@ -271,9 +271,9 @@
 	}
 	err := grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.xos.Create"+modelName, headers, h, h.GetParams)
 	if err != nil {
-		return err
+		return corderrors.RpcErrorWithModelNameToCordError(err, modelName)
 	} else if h.Status != nil && h.Status.Err() != nil {
-		return h.Status.Err()
+		return corderrors.RpcErrorWithModelNameToCordError(h.Status.Err(), modelName)
 	}
 
 	resp, err := dynamic.AsDynamicMessage(h.Response)
@@ -302,9 +302,9 @@
 	}
 	err := grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.xos.Update"+modelName, headers, h, h.GetParams)
 	if err != nil {
-		return err
+		return corderrors.RpcErrorWithModelNameToCordError(err, modelName)
 	} else if h.Status != nil && h.Status.Err() != nil {
-		return h.Status.Err()
+		return corderrors.RpcErrorWithModelNameToCordError(h.Status.Err(), modelName)
 	}
 
 	resp, err := dynamic.AsDynamicMessage(h.Response)
@@ -440,11 +440,11 @@
 	h := &RpcEventHandler{}
 	err := grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.xos.List"+modelName, headers, h, h.GetParams)
 	if err != nil {
-		return nil, err
+		return nil, corderrors.RpcErrorWithModelNameToCordError(err, modelName)
 	}
 
 	if h.Status != nil && h.Status.Err() != nil {
-		return nil, h.Status.Err()
+		return nil, corderrors.RpcErrorWithModelNameToCordError(h.Status.Err(), modelName)
 	}
 
 	d, err := dynamic.AsDynamicMessage(h.Response)
@@ -489,11 +489,11 @@
 	}
 	err = grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.xos.Filter"+modelName, headers, h, h.GetParams)
 	if err != nil {
-		return nil, err
+		return nil, corderrors.RpcErrorWithQueriesToCordError(err, modelName, queries)
 	}
 
 	if h.Status != nil && h.Status.Err() != nil {
-		return nil, h.Status.Err()
+		return nil, corderrors.RpcErrorWithQueriesToCordError(h.Status.Err(), modelName, queries)
 	}
 
 	d, err := dynamic.AsDynamicMessage(h.Response)
@@ -526,7 +526,9 @@
 	}
 
 	if len(models) == 0 {
-		return nil, corderrors.WithStackTrace(&corderrors.ModelNotFoundError{ModelName: modelName, Queries: queries})
+		cordError := &corderrors.ModelNotFoundError{}
+		cordError.Obj = corderrors.ObjectReference{ModelName: modelName, Queries: queries}
+		return nil, corderrors.WithStackTrace(cordError)
 	}
 
 	return models[0], nil
@@ -621,11 +623,11 @@
 	}
 	err := grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.xos.Delete"+modelName, headers, h, h.GetParams)
 	if err != nil {
-		return err
+		return corderrors.RpcErrorWithIdToCordError(err, modelName, id)
 	}
 
 	if h.Status != nil && h.Status.Err() != nil {
-		return h.Status.Err()
+		return corderrors.RpcErrorWithIdToCordError(h.Status.Err(), modelName, id)
 	}
 
 	_, err = dynamic.AsDynamicMessage(h.Response)
diff --git a/commands/services.go b/commands/services.go
index cc99dc5..19d4fd0 100644
--- a/commands/services.go
+++ b/commands/services.go
@@ -21,6 +21,7 @@
 	"github.com/fullstorydev/grpcurl"
 	flags "github.com/jessevdk/go-flags"
 	"github.com/jhump/protoreflect/dynamic"
+	corderrors "github.com/opencord/cordctl/error"
 )
 
 const (
@@ -62,11 +63,11 @@
 	h := &RpcEventHandler{}
 	err = grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.dynamicload.GetLoadStatus", headers, h, h.GetParams)
 	if err != nil {
-		return err
+		return corderrors.RpcErrorToCordError(err)
 	}
 
 	if h.Status != nil && h.Status.Err() != nil {
-		return h.Status.Err()
+		return corderrors.RpcErrorToCordError(h.Status.Err())
 	}
 
 	d, err := dynamic.AsDynamicMessage(h.Response)
diff --git a/commands/status.go b/commands/status.go
index 0924db4..6527371 100644
--- a/commands/status.go
+++ b/commands/status.go
@@ -21,6 +21,7 @@
 	"github.com/fullstorydev/grpcurl"
 	flags "github.com/jessevdk/go-flags"
 	"github.com/jhump/protoreflect/dynamic"
+	corderrors "github.com/opencord/cordctl/error"
 	"strings"
 )
 
@@ -62,11 +63,11 @@
 		h := &RpcEventHandler{}
 		err = grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.utility.GetDatabaseInfo", headers, h, h.GetParams)
 		if err != nil {
-			return err
+			return corderrors.RpcErrorToCordError(err)
 		}
 
 		if h.Status != nil && h.Status.Err() != nil {
-			return h.Status.Err()
+			return corderrors.RpcErrorToCordError(h.Status.Err())
 		}
 
 		d, err := dynamic.AsDynamicMessage(h.Response)