Select Git revision
errors_test.go
errors_test.go 3.98 KiB
///////////////////////////////////////////////////////////////////////////////
// Copyright © 2020 xx network SEZC //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file //
///////////////////////////////////////////////////////////////////////////////
package bindings
import (
"context"
"strings"
"testing"
)
// Unit test
func TestErrorStringToUserFriendlyMessage(t *testing.T) {
// Setup: Populate map
backendErrs := []string{"Failed to Unmarshal Conversation", "failed to create group key preimage",
"Failed to unmarshal SentRequestMap"}
userErrs := []string{"Could not retrieve conversation", "Failed to initiate group chat",
"Failed to pull up friend requests"}
for i, exampleErr := range backendErrs {
errToUserErr[exampleErr] = userErrs[i]
}
// Check if a mapped common error returns the expected user friendly error
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"+
"\n\tReceived: %v", userErrs[0], received)
}
// Test RPC error in which high level information should
// be passed along (ie context deadline exceeded error)
expected := "Could not poll network: "
rpcPrefix := "rpc error: desc = "
rpcErr := expected + rpcPrefix + context.DeadlineExceeded.Error()
received = ErrorStringToUserFriendlyMessage(rpcErr)
if strings.Compare(expected, received) != 0 {
t.Errorf("Rpc error parsed unxecpectedly with error "+
"\n\"%s\" "+
"\n\tExpected: %s"+
"\n\tReceived: %v", rpcErr, UnrecognizedCode+expected, received)
}
// 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 = ErrorStringToUserFriendlyMessage(rpcErr)
if strings.Compare(serverSideError, received) != 0 {
t.Errorf("RPC error parsed unexpectedly with error "+
"\n\"%s\" "+
"\n\tExpected: %s"+
"\n\tReceived: %v", rpcErr, UnrecognizedCode+serverSideError, received)
}
// Test uncommon error, should return highest level message
expected = "failed to register with permissioning"
uncommonErr := expected + ": sendRegistrationMessage: Unable to contact Identity Server"
received = ErrorStringToUserFriendlyMessage(uncommonErr)
if strings.Compare(received, UnrecognizedCode+expected) != 0 {
t.Errorf("Uncommon error parsed unexpectedly with error "+
"\n\"%s\" "+
"\n\tExpected: %s"+
"\n\tReceived: %s", uncommonErr, UnrecognizedCode+expected, received)
}