My react native application is running on android virtual device (API 23)
Android Gradle Plugin Version - 7.2.0 Gradle Version - 7.4.2
build.gradle
buildscript {
ext {
kotlinVersion = "1.5.0"
RNNKotlinVersion = kotlinVersion
buildToolsVersion = "31.0.0"
minSdkVersion = 23
compileSdkVersion = 31
targetSdkVersion = 28
...
}
An http request (http://10.0.2.2:8888/myapp/auth) is made to confirm login to my localhost server. I get an http response code 500 (email/password is incorrect). AccessService calls FetchTool to fetch http. FetchTool throws a new Error('incorrect email/password'). But AccessService receives a ghost different error as described below:
LOG ERROR..:{"line":25722,"column":33,"sourceURL":"http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.myapp&modulesOnly=false&runModule=true"}
Why don't I get the error 500 thrown in FetchTool in AccessService's catch?
export default class AccessService {
static confirmLogon(email, password) {
const login = {
email: email,
password: password
};
const headers = {
'Content-Type': 'application/json',
}
const uri = properties.appurl '/auth';
return FetchTool.post(uri, headers, login)
.then(credentials => {
return credentials;
})
.catch(error => {
// the ghost error is captured here
console.log('ERROR..:' JSON.stringify(error))
throw error;
});
}
}
export default class FetchTool {
static post(uri, headers, playload) {
return this.executeRequest(uri, headers, playload, 'POST');
}
static executeRequest(uri, headers, playload, method) {
if (headers['Content-Type'] && headers['Content-Type'] == 'application/json') {
playload = JSON.stringify(playload);
}
const requestInfo = {
method: method,
body: playload,
headers: headers
}
//uri = http://10.0.2.2:8888/myapp/auth
return fetch(uri, requestInfo)
.then(response => {
if (response.ok) {
//some code return json ok
} else {
return response.text()
.then(responseText => JSON.parse(responseText))
.then(responseObject => {
if(responseObject.statusCode === 500) {
//the error scenario takes place here
throw new Error(JSON.stringify(responseObject));
} else {
// some code
}
})
}
})
}
}
CodePudding user response:
Just remove JSON.stringify like so:
console.log(‘ERROR: ‘, error)
And you ‘ll see the error message as expected.