PLT-5643 Handle unexpected response from the server (#317)
* PLT-5643 Handle unexpected response from the server * removing the need to check if the response is json
This commit is contained in:
parent
2fe69cacef
commit
d001e1d24f
2 changed files with 10 additions and 15 deletions
|
|
@ -1510,6 +1510,7 @@
|
|||
"mobile.create_channel.private": "New Private Group",
|
||||
"mobile.loading_channels": "Loading Channels...",
|
||||
"mobile.loading_members": "Loading Members...",
|
||||
"mobile.request.invalid_response": "Received invalid response from the server.",
|
||||
"mobile.routes.channels": "Channels",
|
||||
"mobile.routes.enterServerUrl": "Enter Server URL",
|
||||
"mobile.routes.login": "Login",
|
||||
|
|
|
|||
|
|
@ -8,13 +8,10 @@ import {Constants} from 'service/constants';
|
|||
|
||||
const HEADER_AUTH = 'Authorization';
|
||||
const HEADER_BEARER = 'BEARER';
|
||||
const HEADER_CONTENT_TYPE = 'Content-Type';
|
||||
const HEADER_REQUESTED_WITH = 'X-Requested-With';
|
||||
const HEADER_TOKEN = 'Token';
|
||||
const HEADER_X_VERSION_ID = 'X-Version-Id';
|
||||
|
||||
const CONTENT_TYPE_JSON = 'application/json';
|
||||
|
||||
export default class Client {
|
||||
constructor() {
|
||||
this.logToConsole = false;
|
||||
|
|
@ -707,16 +704,18 @@ export default class Client {
|
|||
|
||||
doFetchWithResponse = async (url, options) => {
|
||||
const response = await fetch(url, this.getOptions(options));
|
||||
|
||||
const headers = parseAndMergeNestedHeaders(response.headers);
|
||||
const contentType = headers.get(HEADER_CONTENT_TYPE);
|
||||
const isJson = contentType && contentType.indexOf(CONTENT_TYPE_JSON) !== -1;
|
||||
|
||||
let data;
|
||||
if (isJson) {
|
||||
try {
|
||||
data = await response.json();
|
||||
} else {
|
||||
data = await response.text();
|
||||
} catch (err) {
|
||||
throw {
|
||||
intl: {
|
||||
id: 'mobile.request.invalid_response',
|
||||
defaultMessage: 'Received invalid response from the server.'
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
if (headers.has(HEADER_X_VERSION_ID)) {
|
||||
|
|
@ -735,12 +734,7 @@ export default class Client {
|
|||
};
|
||||
}
|
||||
|
||||
let msg;
|
||||
if (isJson) {
|
||||
msg = data.message || '';
|
||||
} else {
|
||||
msg = data;
|
||||
}
|
||||
const msg = data.message || '';
|
||||
|
||||
if (this.logToConsole) {
|
||||
console.error(msg); // eslint-disable-line no-console
|
||||
|
|
|
|||
Loading…
Reference in a new issue