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

Merge branch 'feature/UpdateCommonErrors' into 'release'

Add UpdateCommonErrors bindings call

See merge request !664
parents accf072c b9971bf5
No related branches found
No related tags found
1 merge request!23Release
...@@ -9,31 +9,36 @@ package bindings ...@@ -9,31 +9,36 @@ package bindings
import ( import (
"context" "context"
"encoding/json"
"fmt"
"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{
// todo populate with common errors
// 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: "
const UnrecognizedMessage = UnrecognizedCode + "Unrecognized error from XX backend, please report" const UnrecognizedMessage = UnrecognizedCode + "Unrecognized error from XX backend, please report"
...@@ -42,12 +47,14 @@ const UnrecognizedMessage = UnrecognizedCode + "Unrecognized error from XX backe ...@@ -42,12 +47,14 @@ const UnrecognizedMessage = UnrecognizedCode + "Unrecognized error from XX backe
// a backend generated error. These may be error specifically written by // a backend generated error. These may be error specifically written by
// the backend team or lower level errors gotten from low level dependencies. // the backend team or lower level errors gotten from low level dependencies.
// This function will parse the error string for common errors provided from // This function will parse the error string for common errors provided from
// 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 ErrorStringToUserFriendlyMessage(errStr string) string { func ErrorStringToUserFriendlyMessage(errStr string) string {
errorMux.RLock()
defer errorMux.RUnlock()
// Go through common errors // Go through common errors
for backendErr, userFriendly := range 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
...@@ -78,5 +85,21 @@ func ErrorStringToUserFriendlyMessage(errStr string) string { ...@@ -78,5 +85,21 @@ func ErrorStringToUserFriendlyMessage(errStr string) string {
return UnrecognizedCode + errParts[0] return UnrecognizedCode + errParts[0]
} }
return UnrecognizedMessage return fmt.Sprintf("%s: %v", UnrecognizedCode, errStr)
}
// UpdateCommonErrors takes the passed in contents of a JSON file and updates the
// 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 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?")
}
return nil
} }
...@@ -22,7 +22,7 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) { ...@@ -22,7 +22,7 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) {
"Failed to pull up friend requests"} "Failed to pull up friend requests"}
for i, exampleErr := range backendErrs { for i, exampleErr := range backendErrs {
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
...@@ -72,7 +72,7 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) { ...@@ -72,7 +72,7 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) {
// should hardcoded error message // should hardcoded error message
uncommonErr = "failed to register with permissioning" uncommonErr = "failed to register with permissioning"
received = ErrorStringToUserFriendlyMessage(uncommonErr) received = ErrorStringToUserFriendlyMessage(uncommonErr)
if strings.Compare(UnrecognizedMessage, 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\" "+
"\n\tExpected: %s"+ "\n\tExpected: %s"+
...@@ -80,3 +80,28 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) { ...@@ -80,3 +80,28 @@ func TestErrorStringToUserFriendlyMessage(t *testing.T) {
} }
} }
// Unit test
func TestClient_UpdateCommonErrors(t *testing.T) {
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 := UpdateCommonErrors(jsonData)
if err != nil {
t.Fatalf("UpdateCommonErrors error: %v", err)
}
val, ok := errToUserErr[key]
if !ok {
t.Fatalf("Expected entry was not populated")
}
if strings.Compare(expectedVal, val) != 0 {
t.Fatalf("Entry in updated error map was not expected."+
"\n\tExpected: %s"+
"\n\tReceived: %s", expectedVal, val)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment