diff --git a/core/remote/errors.js b/core/remote/errors.js index b7bf9c6e4..53c33c12f 100644 --- a/core/remote/errors.js +++ b/core/remote/errors.js @@ -188,7 +188,7 @@ const wrapError = ( err /*: FetchError | Error */, doc /*: ?SavedMetadata */ ) /*: RemoteError */ => { - if (err.name === 'FetchError') { + if (isNetworkError(err)) { // $FlowFixMe FetchErrors missing status will fallback to the default case const { status } = err @@ -379,6 +379,13 @@ function detail(err /*: FetchError */) /*: ?string */ { return detail } +function isNetworkError(err /*: Error */) { + return ( + err.name === 'FetchError' || + (typeof err.message === 'string' && err.message.includes('net::')) + ) +} + module.exports = { CozyDocumentMissingError, DirectoryNotFound, @@ -405,5 +412,6 @@ module.exports = { UNKNOWN_REMOTE_ERROR_CODE, UNREACHABLE_COZY_CODE, USER_ACTION_REQUIRED_CODE, + isNetworkError, wrapError } diff --git a/core/sync/errors.js b/core/sync/errors.js index 4723fd41c..f4e9dbbf2 100644 --- a/core/sync/errors.js +++ b/core/sync/errors.js @@ -295,10 +295,10 @@ const wrapError = ( return new SyncError({ sideName, err, code: INCOMPATIBLE_DOC_CODE, doc }) } else if (err instanceof remoteErrors.ExcludedDirError) { return new SyncError({ sideName, err, code: EXCLUDED_DIR_CODE, doc }) - } else if (sideName === 'remote' || err.name === 'FetchError') { + } else if (remoteErrors.isNetworkError(err)) { // FetchErrors can be raised from the LocalWriter when failing to download a - // file for example. In this case the sideName will be "local" but the error - // name will still be "FetchError". + // file for example. In this case the error name won't be "FetchError" but + // its message will still contain `net::`. // If err is a RemoteError, its code will be reused. return new SyncError({ sideName, diff --git a/test/unit/remote/errors.js b/test/unit/remote/errors.js new file mode 100644 index 000000000..119de81f5 --- /dev/null +++ b/test/unit/remote/errors.js @@ -0,0 +1,18 @@ +/* eslint-env mocha */ +/* @flow */ + +const should = require('should') + +const remoteErrors = require('../../../core/remote/errors') + +describe('Remote.wrapError', () => { + it('returns an UnreachableCozy error for net::ERR_NETWORKD_CHANGED errors', () => { + const netErr = new Error( + 'Failed request, reason: net::ERR_NETWORKD_CHANGED' + ) + should(remoteErrors.wrapError(netErr)).have.property( + 'code', + remoteErrors.UNREACHABLE_COZY_CODE + ) + }) +}) diff --git a/test/unit/sync/errors.js b/test/unit/sync/errors.js new file mode 100644 index 000000000..25048a462 --- /dev/null +++ b/test/unit/sync/errors.js @@ -0,0 +1,19 @@ +/* eslint-env mocha */ +/* @flow */ + +const should = require('should') + +const remoteErrors = require('../../../core/remote/errors') +const syncErrors = require('../../../core/sync/errors') + +describe('Sync.wrapError', () => { + it('returns an UnreachableCozy error for net::ERR_NETWORKD_CHANGED errors', () => { + const netErr = new Error( + 'Failed request, reason: net::ERR_NETWORKD_CHANGED' + ) + should(syncErrors.wrapError(netErr, 'local')).have.property( + 'code', + remoteErrors.UNREACHABLE_COZY_CODE + ) + }) +})