diff --git a/bindings/client.go b/bindings/client.go index 4abd7b17326ddce487d25d08f75955f112aaf8f3..8e24a3b017ca18467f22f547a2b1a6cd2b4bda09 100644 --- a/bindings/client.go +++ b/bindings/client.go @@ -41,11 +41,9 @@ func init() { // BindingsClient wraps the api.Client, implementing additional functions // to support the gomobile Client interface type Client struct { - api api.Client - single *single.Manager - singleMux sync.Mutex - errorMux sync.RWMutex - errToUserErr map[string]string + api api.Client + single *single.Manager + singleMux sync.Mutex } // NewClient creates client storage, generates keys, connects, and registers diff --git a/bindings/errors.go b/bindings/errors.go index 6268c3afd1ef576e94e1566669e2ee1efc8e785a..b223e6039ff1d3b23a1891ca5d357dc6e06b7710 100644 --- a/bindings/errors.go +++ b/bindings/errors.go @@ -13,28 +13,31 @@ import ( "fmt" "github.com/pkg/errors" "strings" + "sync" ) // ErrToUserErr maps backend patterns to user friendly error messages. // Example format: // (Back-end) "Building new HostPool because no HostList stored:": (Front-end) "Missing host list", -//var ErrToUserErr = map[string]string{ -//// Registration errors -//"cannot create username when network is not health" : -// "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" : -// "Invalid fact, is the field empty?", -//// UD failures -//"failed to create user discovery manager: cannot return single manager, network is not health" : -// "Could not connect to user discovery", -//"user discovery returned error on search: no results found" : -// "No results found", -//"failed to search.: waiting for response to single-use transmisson timed out after 10s" : -// "Search timed out", -//"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." : -// "Could not get network status", -//} +var ErrToUserErr = map[string]string{ + // Registration errors + //"cannot create username when network is not health" : + // "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" : + // "Invalid fact, is the field empty?", + //// UD failures + //"failed to create user discovery manager: cannot return single manager, network is not health" : + // "Could not connect to user discovery", + //"user discovery returned error on search: no results found" : + // "No results found", + //"failed to search.: waiting for response to single-use transmisson timed out after 10s" : + // "Search timed out", + //"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." : + // "Could not get network status", +} + +var ErrorMux sync.RWMutex // Error codes const UnrecognizedCode = "UR: " @@ -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. // 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. -func (c *Client) ErrorStringToUserFriendlyMessage(errStr string) string { - c.errorMux.RLock() - defer c.errorMux.RUnlock() +func ErrorStringToUserFriendlyMessage(errStr string) string { + ErrorMux.RLock() + defer ErrorMux.RUnlock() // Go through common errors - for backendErr, userFriendly := range c.errToUserErr { + for backendErr, userFriendly := range ErrToUserErr { // Determine if error contains a common error if strings.Contains(errStr, backendErr) { return userFriendly @@ -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 // conform with the commented examples provides in ErrToUserErr above. // NOTE that you should not pass in a file path, but a preloaded JSON file -func (c *Client) UpdateCommonErrors(jsonFile string) error { - c.errorMux.Lock() - defer c.errorMux.Unlock() - err := json.Unmarshal([]byte(jsonFile), &c.errToUserErr) +func UpdateCommonErrors(jsonFile string) error { + ErrorMux.Lock() + defer ErrorMux.Unlock() + err := json.Unmarshal([]byte(jsonFile), &ErrToUserErr) if err != nil { return errors.WithMessage(err, "Failed to unmarshal json file, "+ "did you pass in the contents or the path?") diff --git a/bindings/errors_test.go b/bindings/errors_test.go index c232a14a361267891ae4f481981d20ffd737a6cc..4ff60bfdab03e2c5be60fa8e4d874195e0b14dc4 100644 --- a/bindings/errors_test.go +++ b/bindings/errors_test.go @@ -21,14 +21,12 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) { userErrs := []string{"Could not retrieve conversation", "Failed to initiate group chat", "Failed to pull up friend requests"} - c := &Client{errToUserErr: make(map[string]string)} - 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 - received := c.ErrorStringToUserFriendlyMessage(backendErrs[0]) + received := ErrorStringToUserFriendlyMessage(backendErrs[0]) if strings.Compare(received, userErrs[0]) != 0 { t.Errorf("Unexpected user friendly message returned from common error mapping."+ "\n\tExpected: %s"+ @@ -40,7 +38,7 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) { expected := "Could not poll network: " rpcPrefix := "rpc error: desc = " rpcErr := expected + rpcPrefix + context.DeadlineExceeded.Error() - received = c.ErrorStringToUserFriendlyMessage(rpcErr) + received = ErrorStringToUserFriendlyMessage(rpcErr) if strings.Compare(expected, received) != 0 { t.Errorf("Rpc error parsed unxecpectedly with error "+ "\n\"%s\" "+ @@ -51,7 +49,7 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) { // Test RPC error where server side error information is provided serverSideError := "Could not parse message! Please try again with a properly crafted message" rpcErr = rpcPrefix + serverSideError - received = c.ErrorStringToUserFriendlyMessage(rpcErr) + received = ErrorStringToUserFriendlyMessage(rpcErr) if strings.Compare(serverSideError, received) != 0 { t.Errorf("RPC error parsed unexpectedly with error "+ "\n\"%s\" "+ @@ -62,7 +60,7 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) { // Test uncommon error, should return highest level message expected = "failed to register with permissioning" uncommonErr := expected + ": sendRegistrationMessage: Unable to contact Identity Server" - received = c.ErrorStringToUserFriendlyMessage(uncommonErr) + received = ErrorStringToUserFriendlyMessage(uncommonErr) if strings.Compare(received, UnrecognizedCode+expected) != 0 { t.Errorf("Uncommon error parsed unexpectedly with error "+ "\n\"%s\" "+ @@ -73,7 +71,7 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) { // Test fully unrecognizable and un-parsable message, // should hardcoded error message uncommonErr = "failed to register with permissioning" - received = c.ErrorStringToUserFriendlyMessage(uncommonErr) + received = ErrorStringToUserFriendlyMessage(uncommonErr) if strings.Compare(UnrecognizedCode+": "+uncommonErr, received) != 0 { t.Errorf("Uncommon error parsed unexpectedly with error "+ "\n\"%s\" "+ @@ -85,18 +83,17 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) { // Unit test 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" 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 { t.Fatalf("UpdateCommonErrors error: %v", err) } - val, ok := c.errToUserErr[key] + val, ok := ErrToUserErr[key] if !ok { t.Fatalf("Expected entry was not populated") }