Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
client
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elixxir
client
Commits
0a947789
Commit
0a947789
authored
3 years ago
by
Josh Brooks
Browse files
Options
Downloads
Patches
Plain Diff
Add backend to user friendly error message parsing
parent
8ed9dea0
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!23
Release
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
bindings/errors.go
+68
-0
68 additions, 0 deletions
bindings/errors.go
bindings/errors_test.go
+82
-0
82 additions, 0 deletions
bindings/errors_test.go
with
150 additions
and
0 deletions
bindings/errors.go
0 → 100644
+
68
−
0
View file @
0a947789
///////////////////////////////////////////////////////////////////////////////
// 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"
)
// 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
{
// todo populate with common errors
}
// Error codes
const
UnrecognizedCode
=
"UR: "
const
UnrecognizedMessage
=
UnrecognizedCode
+
"Unrecognized error from XX backend, please report"
// ErrorStringToUserFriendlyMessage takes a passed in errStr which will be
// a backend generated error. These may be error specifically written by
// the backend team or lower level errors gotten from low level dependencies.
// 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.
// 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
ErrorStringToUserFriendlyMessage
(
errStr
string
)
string
{
// Go through common errors
for
backendErr
,
userFriendly
:=
range
ErrToUserErr
{
// Determine if error contains a common error
// Fixme: later versions may be improved by using regex
if
strings
.
HasPrefix
(
errStr
,
backendErr
)
{
return
userFriendly
}
}
descStr
:=
"desc = "
// If this contains an rpc error, determine how to handle it
if
strings
.
Contains
(
errStr
,
context
.
DeadlineExceeded
.
Error
())
{
// If there is a context deadline exceeded message, return the higher level
// as context deadline exceeded is not informative
rpcErr
:=
"rpc "
rpcIdx
:=
strings
.
Index
(
errStr
,
rpcErr
)
return
UnrecognizedCode
+
errStr
[
:
rpcIdx
]
}
else
if
strings
.
Contains
(
errStr
,
descStr
)
{
// If containing an rpc error where context deadline exceeded
// is NOT involved, the error returned server-side is often
//more informative
descIdx
:=
strings
.
Index
(
errStr
,
descStr
)
// return everything after "desc = "
return
UnrecognizedCode
+
errStr
[
descIdx
+
len
(
descStr
)
:
]
}
// If a compound error message, return the highest level message
errParts
:=
strings
.
Split
(
errStr
,
":"
)
if
len
(
errParts
)
>
1
{
// Return everything before the first :
return
UnrecognizedCode
+
errParts
[
0
]
}
return
UnrecognizedMessage
}
This diff is collapsed.
Click to expand it.
bindings/errors_test.go
0 → 100644
+
82
−
0
View file @
0a947789
///////////////////////////////////////////////////////////////////////////////
// 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\t
Expected: %s"
+
"
\n\t
Received: %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
(
UnrecognizedCode
+
expected
,
received
)
!=
0
{
t
.
Errorf
(
"Rpc error parsed unxecpectedly with error "
+
"
\n\"
%s
\"
"
+
"
\n\t
Expected: %s"
+
"
\n\t
Received: %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
(
UnrecognizedCode
+
serverSideError
,
received
)
!=
0
{
t
.
Errorf
(
"RPC error parsed unexpectedly with error "
+
"
\n\"
%s
\"
"
+
"
\n\t
Expected: %s"
+
"
\n\t
Received: %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\t
Expected: %s"
+
"
\n\t
Received: %s"
,
uncommonErr
,
UnrecognizedCode
+
expected
,
received
)
}
// Test fully unrecognizable and un-parsable message,
// should hardcoded error message
uncommonErr
=
"failed to register with permissioning"
received
=
ErrorStringToUserFriendlyMessage
(
uncommonErr
)
if
strings
.
Compare
(
UnrecognizedMessage
,
received
)
!=
0
{
t
.
Errorf
(
"Uncommon error parsed unexpectedly with error "
+
"
\n\"
%s
\"
"
+
"
\n\t
Expected: %s"
+
"
\n\t
Received: %s"
,
uncommonErr
,
UnrecognizedMessage
,
received
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment