Skip to content

Commit 1166c1b

Browse files
ahmedAlaaInstabugabdelhamid-f-nassera7medevkholood-ea
committed
feat: capture client error in the network interceptor (#1257)
* feat/support-capture-client-error-in-xhr-requests --------- Co-authored-by: Abdelhamid Nasser <[email protected]> Co-authored-by: Ahmed Elrefaey <[email protected]> Co-authored-by: kholood <[email protected]>
1 parent 1368b0e commit 1166c1b

File tree

6 files changed

+57
-8
lines changed

6 files changed

+57
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Added
66

77
- Add support for Expo Router navigation tracking ([#1270](https://github.com/Instabug/Instabug-React-Native/pull/1270)).
8+
- Enhance the network interceptor to capture more client error messages ([#1257](https://github.com/Instabug/Instabug-React-Native/pull/1257)).
89

910
### Fixed
1011

examples/default/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"@react-navigation/native": "^6.1.6",
1717
"@react-navigation/native-stack": "^6.9.12",
1818
"graphql": "^16.8.1",
19+
"axios": "^1.7.4",
1920
"graphql-request": "^6.1.0",
2021
"instabug-reactnative": "link:../..",
2122
"instabug-reactnative-ndk": "github:https://github.com/Instabug/Instabug-React-Native-NDK",

examples/default/src/screens/apm/NetworkScreen.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { useQuery } from 'react-query';
77
import { HStack, VStack } from 'native-base';
88
import { gql, request } from 'graphql-request';
99
import { CustomButton } from '../../components/CustomButton';
10+
import axios from 'axios';
1011

1112
export const NetworkScreen: React.FC = () => {
1213
const [endpointUrl, setEndpointUrl] = useState('');
@@ -45,6 +46,32 @@ export const NetworkScreen: React.FC = () => {
4546
}
4647
}
4748

49+
async function sendRequestToUrlUsingAxios() {
50+
let urlToSend = '';
51+
52+
if (endpointUrl.trim() !== '') {
53+
urlToSend = endpointUrl;
54+
console.log('Sending request to: ', endpointUrl);
55+
} else {
56+
// Use json placeholder URL as a default if endpointUrl is empty
57+
console.log('sending request to default json placeholder');
58+
urlToSend = defaultRequestUrl;
59+
}
60+
61+
try {
62+
// Perform the request using the urlToSend
63+
const response = await axios.get(urlToSend);
64+
// Format the JSON response for better logging
65+
const formattedData = JSON.stringify(response.data, null, 2);
66+
67+
// Log the formatted response
68+
console.log('Response:', formattedData);
69+
} catch (error) {
70+
// Handle errors appropriately
71+
console.error('Error:', error);
72+
}
73+
}
74+
4875
const fetchGraphQlData = async () => {
4976
const document = gql`
5077
query {
@@ -75,6 +102,11 @@ export const NetworkScreen: React.FC = () => {
75102
value={endpointUrl}
76103
/>
77104
<CustomButton onPress={sendRequestToUrl} title="Send Request To Url" />
105+
<CustomButton
106+
onPress={sendRequestToUrlUsingAxios}
107+
title="Send Request To Url Using Axios"
108+
/>
109+
78110
<CustomButton onPress={() => refetch} title="Reload GraphQL" />
79111
<View>
80112
{isLoading && <Text>Loading...</Text>}

examples/default/yarn.lock

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3249,10 +3249,10 @@ at-least-node@^1.0.0:
32493249
resolved "https://registry.npmjs.org/at-least-node/-/at-least-node-1.0.0.tgz#602cd4b46e844ad4effc92a8011a3c46e0238dc2"
32503250
integrity sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg==
32513251

3252-
axios@^1.7.2:
3253-
version "1.7.2"
3254-
resolved "https://registry.yarnpkg.com/axios/-/axios-1.7.2.tgz#b625db8a7051fbea61c35a3cbb3a1daa7b9c7621"
3255-
integrity sha512-2A8QhOMrbomlDuiLeK9XibIBzuHeRcqqNOHp0Cyp5EoJ1IFDh+XZH3A6BkXtv0K4gFGCI0Y4BM7B1wOEi0Rmgw==
3252+
axios@^1.7.4:
3253+
version "1.7.4"
3254+
resolved "https://registry.yarnpkg.com/axios/-/axios-1.7.4.tgz#4c8ded1b43683c8dd362973c393f3ede24052aa2"
3255+
integrity sha512-DukmaFRnY6AzAALSH4J2M3k6PkaC+MfaAGdEERRWcC9q3/TWQwLpHR8ZRLKTdQ3aBDL64EdluRDjJqKw+BPZEw==
32563256
dependencies:
32573257
follow-redirects "^1.15.6"
32583258
form-data "^4.0.0"
@@ -4690,7 +4690,8 @@ inline-style-prefixer@^6.0.1:
46904690
resolved "https://codeload.github.com/Instabug/Instabug-React-Native-NDK/tar.gz/3b0bac281253133852d32f52aa50cc805dd0b570"
46914691

46924692
"instabug-reactnative@link:../..":
4693-
version "13.0.5"
4693+
version "0.0.0"
4694+
uid ""
46944695

46954696
intl-messageformat@^10.1.0:
46964697
version "10.5.0"

src/utils/XhrNetworkInterceptor.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ export default {
7272
originalXHROpen = XMLHttpRequest.prototype.open;
7373
originalXHRSend = XMLHttpRequest.prototype.send;
7474
originalXHRSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader;
75-
75+
// An error code that signifies an issue with the RN client.
76+
const clientErrorCode = 9876;
7677
XMLHttpRequest.prototype.open = function (method, url, ...args) {
7778
_reset();
7879
network.url = url;
@@ -145,14 +146,24 @@ export default {
145146

146147
// @ts-ignore
147148
if (this._hasError) {
148-
cloneNetwork.errorCode = 0;
149+
cloneNetwork.errorCode = clientErrorCode;
149150
cloneNetwork.errorDomain = 'ClientError';
150151

151152
// @ts-ignore
152153
const _response = this._response;
153154
cloneNetwork.requestBody =
154155
typeof _response === 'string' ? _response : JSON.stringify(_response);
155156
cloneNetwork.responseBody = '';
157+
158+
// Detect a more descriptive error message.
159+
if (typeof _response === 'string' && _response.length > 0) {
160+
cloneNetwork.errorDomain = _response;
161+
}
162+
163+
// @ts-ignore
164+
} else if (this._timedOut) {
165+
cloneNetwork.errorCode = clientErrorCode;
166+
cloneNetwork.errorDomain = 'TimeOutError';
156167
}
157168

158169
if (this.response) {
@@ -162,6 +173,9 @@ export default {
162173
} else if (['text', '', 'json'].includes(this.responseType)) {
163174
cloneNetwork.responseBody = JSON.stringify(this.response);
164175
}
176+
} else {
177+
cloneNetwork.responseBody = '';
178+
cloneNetwork.contentType = 'text/plain';
165179
}
166180

167181
cloneNetwork.requestBodySize = cloneNetwork.requestBody.length;

test/utils/XhrNetworkInterceptor.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ describe('Network Interceptor', () => {
235235
expect(callback).toBeCalledWith(
236236
expect.objectContaining({
237237
errorDomain: 'ClientError',
238-
errorCode: 0,
238+
errorCode: 9876,
239239
responseBody: '',
240240
}),
241241
);

0 commit comments

Comments
 (0)