Skip to content
Projects
Groups
Snippets
Help
Sign in / Register
Toggle navigation
Minds Mobile
Project overview
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Locked Files
Issues
188
Merge Requests
18
Security & Compliance
Packages
Wiki
Snippets
Members
Collapse sidebar
Close sidebar
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Minds
Minds Mobile
Commits
672878f9
Commit
672878f9
authored
19 hours ago
by
Martin Santangelo
Browse files
Options
Download
(feat) show api error messages to the user
parent
22dd4fab
feat/retry-actions-better-message-offline
1 merge request
!471
Remote action with retry and different messages for network, offline and generic errors.
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
0 deletions
+45
-0
__tests__/common/RemoteAction.js
View file @
672878f9
import
{
Alert
}
from
'
react-native
'
;
import
remoteAction
from
'
../../src/common/RemoteAction
'
;
import
connectivityService
from
'
../../src/common/services/connectivity.service
'
;
import
{
ApiError
}
from
'
../../src/common/services/api.service
'
;
jest
.
mock
(
'
../../src/common/services/connectivity.service
'
);
...
...
@@ -10,6 +11,7 @@ describe('remote action', () => {
beforeEach
(()
=>
{
Alert
.
alert
.
mockClear
();
connectivityService
.
isConnected
=
true
;
});
it
(
'
should not auto retry on generic error
'
,
async
()
=>
{
...
...
@@ -68,6 +70,46 @@ describe('remote action', () => {
expect
(
action
).
toHaveBeenCalledTimes
(
2
);
});
it
(
'
should show offline error message
'
,
async
()
=>
{
const
action
=
jest
.
fn
();
action
.
mockImplementation
(
async
()
=>
{
throw
new
TypeError
(
'
Network request failed
'
);
});
connectivityService
.
isConnected
=
false
;
await
remoteAction
(
action
,
''
,
0
);
// should have been called
expect
(
action
).
toHaveBeenCalledTimes
(
1
);
// should call alert with the correct messages
expect
(
Alert
.
alert
.
mock
.
calls
[
0
][
0
]).
toBe
(
'
Sorry!
'
);
expect
(
Alert
.
alert
.
mock
.
calls
[
0
][
1
]).
toBe
(
'
No Internet Connection
'
);
expect
(
Alert
.
alert
.
mock
.
calls
[
0
][
2
][
0
].
text
).
toBe
(
'
Ok
'
);
expect
(
Alert
.
alert
.
mock
.
calls
[
0
][
2
][
1
].
text
).
toBe
(
'
Try again
'
);
});
it
(
'
should show api errors message
'
,
async
()
=>
{
const
action
=
jest
.
fn
();
action
.
mockImplementation
(
async
()
=>
{
throw
new
ApiError
(
'
Some Error
'
);
});
await
remoteAction
(
action
,
''
,
0
);
// should have been called
expect
(
action
).
toHaveBeenCalledTimes
(
1
);
// should call alert with the correct messages
expect
(
Alert
.
alert
.
mock
.
calls
[
0
][
0
]).
toBe
(
'
Sorry!
'
);
expect
(
Alert
.
alert
.
mock
.
calls
[
0
][
1
]).
toBe
(
'
Some Error
'
);
expect
(
Alert
.
alert
.
mock
.
calls
[
0
][
2
][
0
].
text
).
toBe
(
'
Ok
'
);
expect
(
Alert
.
alert
.
mock
.
calls
[
0
][
2
][
1
].
text
).
toBe
(
'
Try again
'
);
});
it
(
'
should show error message with retry on failure
'
,
async
()
=>
{
const
action
=
jest
.
fn
();
...
...
This diff is collapsed.
src/common/RemoteAction.js
View file @
672878f9
...
...
@@ -2,6 +2,7 @@ import connectivityService from './services/connectivity.service';
import
{
isNetworkFail
}
from
'
./helpers/abortableFetch
'
;
import
i18nService
from
'
./services/i18n.service
'
;
import
{
Alert
}
from
'
react-native
'
;
import
{
isApiError
}
from
'
./services/api.service
'
;
/**
* Remote action with auto and manual retry
...
...
@@ -24,6 +25,8 @@ async function remoteAction(action, actionName = '', retries = 1) {
message
=
connectivityService
.
isConnected
?
i18nService
.
t
(
'
cantReachServer
'
)
:
i18nService
.
t
(
'
noInternet
'
);
}
else
if
(
isApiError
(
error
))
{
message
=
error
.
message
;
}
else
{
message
=
i18nService
.
t
(
'
errorMessage
'
);
}
...
...
This diff is collapsed.
Please
register
or
sign in
to comment