Skip to content

Commit

Permalink
core: Handle net::* errors as Fetch errors
Browse files Browse the repository at this point in the history
  When making some network requests, we can encounter network errors
  that are not of the `FetchError` type but are `net` system errors
  (with error codes starting with `net::`).
  For example, when switching from one network to another during a file
  download from the Cozy, the downloading stream will throw an error
  with the `net::ERR_NETWORKD_CHANGED` code.

  We want to handle these errors as `FetchError` errors and can do do so
  by simply looking for the `net::` string in the error message.
  • Loading branch information
taratatach committed Apr 22, 2022
1 parent 6a567f3 commit cdb7b3e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
10 changes: 9 additions & 1 deletion core/remote/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -405,5 +412,6 @@ module.exports = {
UNKNOWN_REMOTE_ERROR_CODE,
UNREACHABLE_COZY_CODE,
USER_ACTION_REQUIRED_CODE,
isNetworkError,
wrapError
}
6 changes: 3 additions & 3 deletions core/sync/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions test/unit/remote/errors.js
Original file line number Diff line number Diff line change
@@ -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
)
})
})
19 changes: 19 additions & 0 deletions test/unit/sync/errors.js
Original file line number Diff line number Diff line change
@@ -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
)
})
})

0 comments on commit cdb7b3e

Please sign in to comment.