src/network/fetch.js
function getResponseContent(response, dataType) {
    const { type, status, ok } = response;

    // Handling errors
    if (type === 'opaque') {
        console.error('You tried to make a Cross Domain Request with no-cors options');
        return Promise.reject({ status: status, globalErrors: ['error.noCorsOptsOnCors'] });
    }

    if (type === 'error') {
        console.error('An unknown network issue has happened');
        return Promise.reject({ status: status, globalErrors: ['error.unknownNetworkIssue'] });
    }

    if (!ok && dataType === 'json') {
        return response.json().catch(err => Promise.reject({ globalErrors: [err] })).then(data => Promise.reject({ status, ...data }));
    }

    if (!ok) {
        return response.text().then(text => Promise.reject({ status, globalErrors: [text] }));
    }

    // Handling success
    if (ok && status === '204') {
        return Promise.resolve(null);
    }
    return ['arrayBuffer', 'blob', 'formData', 'json'].includes(dataType) ? response[dataType]().catch(err => Promise.reject({ globalErrors: [err] })) : response.text();
}
function getResponseContent(response, dataType) {
    const { type, status, ok } = response;

    // Handling errors
    if (type === 'opaque') {
        console.error('You tried to make a Cross Domain Request with no-cors options');
        return Promise.reject({ status: status, globalErrors: ['error.noCorsOptsOnCors'] });
    }

    if (type === 'error') {
        console.error('An unknown network issue has happened');
        return Promise.reject({ status: status, globalErrors: ['error.unknownNetworkIssue'] });
    }

    if (!ok && dataType === 'json') {
        return response.json().catch(err => Promise.reject({ globalErrors: [err] })).then(data => Promise.reject({ status, ...data }));
    }

    if (!ok) {
        return response.text().then(text => Promise.reject({ status, globalErrors: [text] }));
    }

    // Handling success
    if (ok && status === '204') {
        return Promise.resolve(null);
    }
    return ['arrayBuffer', 'blob', 'formData', 'json'].includes(dataType) ? response[dataType]().catch(err => Promise.reject({ globalErrors: [err] })) : response.text();
}
function getResponseContent ( response , dataType ) { const { type , status , ok } = response ; // Handling errors if ( type === 'opaque' ) { console . error ( 'You tried to make a Cross Domain Request with no-cors options' ) ; return Promise . reject ( { status : status , globalErrors : [ 'error.noCorsOptsOnCors' ] } ) ; } if ( type === 'error' ) { console . error ( 'An unknown network issue has happened' ) ; return Promise . reject ( { status : status , globalErrors : [ 'error.unknownNetworkIssue' ] } ) ; } if ( ! ok && dataType === 'json' ) { return response . json ( ) . catch ( err => Promise . reject ( { globalErrors : [ err ] } ) ) . then ( data => Promise . reject ( { status , ... data } ) ) ; } if ( ! ok ) { return response . text ( ) . then ( text => Promise . reject ( { status , globalErrors : [ text ] } ) ) ; } // Handling success if ( ok && status === '204' ) { return Promise . resolve ( null ) ; } return [ 'arrayBuffer' , 'blob' , 'formData' , 'json' ] . includes ( dataType ) ? response [ dataType ] ( ) . catch ( err => Promise . reject ( { globalErrors : [ err ] } ) ) : response . text ( ) ; }
Extract the data from the response, and handle network or server errors or wrong data format.

@param {Response} response the response to extract from
@param {string} dataType the datatype (can be 'arrayBuffer', 'blob', 'formData', 'json' or 'text')
@returns {Promise} a Promise containing response data, or error data
Extract the data from the response and handle network or server errors or wrong data format .