Skip to content
Snippets Groups Projects
Commit b2c50b46 authored by Josh Brooks's avatar Josh Brooks
Browse files

Update error handling bindings

parent 150bec2b
No related branches found
No related tags found
1 merge request!23Release
...@@ -41,11 +41,9 @@ func init() { ...@@ -41,11 +41,9 @@ func init() {
// BindingsClient wraps the api.Client, implementing additional functions // BindingsClient wraps the api.Client, implementing additional functions
// to support the gomobile Client interface // to support the gomobile Client interface
type Client struct { type Client struct {
api api.Client api api.Client
single *single.Manager single *single.Manager
singleMux sync.Mutex singleMux sync.Mutex
errorMux sync.RWMutex
errToUserErr map[string]string
} }
// NewClient creates client storage, generates keys, connects, and registers // NewClient creates client storage, generates keys, connects, and registers
......
...@@ -13,28 +13,31 @@ import ( ...@@ -13,28 +13,31 @@ import (
"fmt" "fmt"
"github.com/pkg/errors" "github.com/pkg/errors"
"strings" "strings"
"sync"
) )
// ErrToUserErr maps backend patterns to user friendly error messages. // ErrToUserErr maps backend patterns to user friendly error messages.
// Example format: // Example format:
// (Back-end) "Building new HostPool because no HostList stored:": (Front-end) "Missing host list", // (Back-end) "Building new HostPool because no HostList stored:": (Front-end) "Missing host list",
//var ErrToUserErr = map[string]string{ var ErrToUserErr = map[string]string{
//// Registration errors // Registration errors
//"cannot create username when network is not health" : //"cannot create username when network is not health" :
// "Cannot create username, unable to connect to network", // "Cannot create username, unable to connect to network",
//"failed to add due to malformed fact stringified facts must at least have a type at the start" : //"failed to add due to malformed fact stringified facts must at least have a type at the start" :
// "Invalid fact, is the field empty?", // "Invalid fact, is the field empty?",
//// UD failures //// UD failures
//"failed to create user discovery manager: cannot return single manager, network is not health" : //"failed to create user discovery manager: cannot return single manager, network is not health" :
// "Could not connect to user discovery", // "Could not connect to user discovery",
//"user discovery returned error on search: no results found" : //"user discovery returned error on search: no results found" :
// "No results found", // "No results found",
//"failed to search.: waiting for response to single-use transmisson timed out after 10s" : //"failed to search.: waiting for response to single-use transmisson timed out after 10s" :
// "Search timed out", // "Search timed out",
//"the phone number supplied was empty" : "Invalid phone number", //"the phone number supplied was empty" : "Invalid phone number",
//"failed to create user discovery manager: cannot start ud manager when network follower is not running." : //"failed to create user discovery manager: cannot start ud manager when network follower is not running." :
// "Could not get network status", // "Could not get network status",
//} }
var ErrorMux sync.RWMutex
// Error codes // Error codes
const UnrecognizedCode = "UR: " const UnrecognizedCode = "UR: "
...@@ -47,11 +50,11 @@ const UnrecognizedMessage = UnrecognizedCode + "Unrecognized error from XX backe ...@@ -47,11 +50,11 @@ const UnrecognizedMessage = UnrecognizedCode + "Unrecognized error from XX backe
// ErrToUserErr to provide a more user-friendly error message for the front end. // ErrToUserErr to provide a more user-friendly error message for the front end.
// If the error is not common, some simple parsing is done on the error message // If the error is not common, some simple parsing is done on the error message
// to make it more user-accessible, removing backend specific jargon. // to make it more user-accessible, removing backend specific jargon.
func (c *Client) ErrorStringToUserFriendlyMessage(errStr string) string { func ErrorStringToUserFriendlyMessage(errStr string) string {
c.errorMux.RLock() ErrorMux.RLock()
defer c.errorMux.RUnlock() defer ErrorMux.RUnlock()
// Go through common errors // Go through common errors
for backendErr, userFriendly := range c.errToUserErr { for backendErr, userFriendly := range ErrToUserErr {
// Determine if error contains a common error // Determine if error contains a common error
if strings.Contains(errStr, backendErr) { if strings.Contains(errStr, backendErr) {
return userFriendly return userFriendly
...@@ -89,10 +92,10 @@ func (c *Client) ErrorStringToUserFriendlyMessage(errStr string) string { ...@@ -89,10 +92,10 @@ func (c *Client) ErrorStringToUserFriendlyMessage(errStr string) string {
// ErrToUserErr map with the contents of the json file. The JSON's expected format // ErrToUserErr map with the contents of the json file. The JSON's expected format
// conform with the commented examples provides in ErrToUserErr above. // conform with the commented examples provides in ErrToUserErr above.
// NOTE that you should not pass in a file path, but a preloaded JSON file // NOTE that you should not pass in a file path, but a preloaded JSON file
func (c *Client) UpdateCommonErrors(jsonFile string) error { func UpdateCommonErrors(jsonFile string) error {
c.errorMux.Lock() ErrorMux.Lock()
defer c.errorMux.Unlock() defer ErrorMux.Unlock()
err := json.Unmarshal([]byte(jsonFile), &c.errToUserErr) err := json.Unmarshal([]byte(jsonFile), &ErrToUserErr)
if err != nil { if err != nil {
return errors.WithMessage(err, "Failed to unmarshal json file, "+ return errors.WithMessage(err, "Failed to unmarshal json file, "+
"did you pass in the contents or the path?") "did you pass in the contents or the path?")
......
...@@ -21,14 +21,12 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) { ...@@ -21,14 +21,12 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) {
userErrs := []string{"Could not retrieve conversation", "Failed to initiate group chat", userErrs := []string{"Could not retrieve conversation", "Failed to initiate group chat",
"Failed to pull up friend requests"} "Failed to pull up friend requests"}
c := &Client{errToUserErr: make(map[string]string)}
for i, exampleErr := range backendErrs { for i, exampleErr := range backendErrs {
c.errToUserErr[exampleErr] = userErrs[i] ErrToUserErr[exampleErr] = userErrs[i]
} }
// Check if a mapped common error returns the expected user friendly error // Check if a mapped common error returns the expected user friendly error
received := c.ErrorStringToUserFriendlyMessage(backendErrs[0]) received := ErrorStringToUserFriendlyMessage(backendErrs[0])
if strings.Compare(received, userErrs[0]) != 0 { if strings.Compare(received, userErrs[0]) != 0 {
t.Errorf("Unexpected user friendly message returned from common error mapping."+ t.Errorf("Unexpected user friendly message returned from common error mapping."+
"\n\tExpected: %s"+ "\n\tExpected: %s"+
...@@ -40,7 +38,7 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) { ...@@ -40,7 +38,7 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) {
expected := "Could not poll network: " expected := "Could not poll network: "
rpcPrefix := "rpc error: desc = " rpcPrefix := "rpc error: desc = "
rpcErr := expected + rpcPrefix + context.DeadlineExceeded.Error() rpcErr := expected + rpcPrefix + context.DeadlineExceeded.Error()
received = c.ErrorStringToUserFriendlyMessage(rpcErr) received = ErrorStringToUserFriendlyMessage(rpcErr)
if strings.Compare(expected, received) != 0 { if strings.Compare(expected, received) != 0 {
t.Errorf("Rpc error parsed unxecpectedly with error "+ t.Errorf("Rpc error parsed unxecpectedly with error "+
"\n\"%s\" "+ "\n\"%s\" "+
...@@ -51,7 +49,7 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) { ...@@ -51,7 +49,7 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) {
// Test RPC error where server side error information is provided // Test RPC error where server side error information is provided
serverSideError := "Could not parse message! Please try again with a properly crafted message" serverSideError := "Could not parse message! Please try again with a properly crafted message"
rpcErr = rpcPrefix + serverSideError rpcErr = rpcPrefix + serverSideError
received = c.ErrorStringToUserFriendlyMessage(rpcErr) received = ErrorStringToUserFriendlyMessage(rpcErr)
if strings.Compare(serverSideError, received) != 0 { if strings.Compare(serverSideError, received) != 0 {
t.Errorf("RPC error parsed unexpectedly with error "+ t.Errorf("RPC error parsed unexpectedly with error "+
"\n\"%s\" "+ "\n\"%s\" "+
...@@ -62,7 +60,7 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) { ...@@ -62,7 +60,7 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) {
// Test uncommon error, should return highest level message // Test uncommon error, should return highest level message
expected = "failed to register with permissioning" expected = "failed to register with permissioning"
uncommonErr := expected + ": sendRegistrationMessage: Unable to contact Identity Server" uncommonErr := expected + ": sendRegistrationMessage: Unable to contact Identity Server"
received = c.ErrorStringToUserFriendlyMessage(uncommonErr) received = ErrorStringToUserFriendlyMessage(uncommonErr)
if strings.Compare(received, UnrecognizedCode+expected) != 0 { if strings.Compare(received, UnrecognizedCode+expected) != 0 {
t.Errorf("Uncommon error parsed unexpectedly with error "+ t.Errorf("Uncommon error parsed unexpectedly with error "+
"\n\"%s\" "+ "\n\"%s\" "+
...@@ -73,7 +71,7 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) { ...@@ -73,7 +71,7 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) {
// Test fully unrecognizable and un-parsable message, // Test fully unrecognizable and un-parsable message,
// should hardcoded error message // should hardcoded error message
uncommonErr = "failed to register with permissioning" uncommonErr = "failed to register with permissioning"
received = c.ErrorStringToUserFriendlyMessage(uncommonErr) received = ErrorStringToUserFriendlyMessage(uncommonErr)
if strings.Compare(UnrecognizedCode+": "+uncommonErr, received) != 0 { if strings.Compare(UnrecognizedCode+": "+uncommonErr, received) != 0 {
t.Errorf("Uncommon error parsed unexpectedly with error "+ t.Errorf("Uncommon error parsed unexpectedly with error "+
"\n\"%s\" "+ "\n\"%s\" "+
...@@ -85,18 +83,17 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) { ...@@ -85,18 +83,17 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) {
// Unit test // Unit test
func TestClient_UpdateCommonErrors(t *testing.T) { func TestClient_UpdateCommonErrors(t *testing.T) {
c := &Client{errToUserErr: make(map[string]string)}
key, expectedVal := "failed to create group key preimage", "Failed to initiate group chat" key, expectedVal := "failed to create group key preimage", "Failed to initiate group chat"
jsonData := "{\"Failed to Unmarshal Conversation\":\"Could not retrieve conversation\",\"Failed to unmarshal SentRequestMap\":\"Failed to pull up friend requests\",\"failed to create group key preimage\":\"Failed to initiate group chat\"}\n" jsonData := "{\"Failed to Unmarshal Conversation\":\"Could not retrieve conversation\",\"Failed to unmarshal SentRequestMap\":\"Failed to pull up friend requests\",\"failed to create group key preimage\":\"Failed to initiate group chat\"}\n"
err := c.UpdateCommonErrors(jsonData) err := UpdateCommonErrors(jsonData)
if err != nil { if err != nil {
t.Fatalf("UpdateCommonErrors error: %v", err) t.Fatalf("UpdateCommonErrors error: %v", err)
} }
val, ok := c.errToUserErr[key] val, ok := ErrToUserErr[key]
if !ok { if !ok {
t.Fatalf("Expected entry was not populated") t.Fatalf("Expected entry was not populated")
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment