diff --git a/lib/utils/services-utils.js b/lib/utils/services-utils.js new file mode 100644 --- /dev/null +++ b/lib/utils/services-utils.js @@ -0,0 +1,10 @@ +// @flow + +function handleHTTPResponseError(response: Response): void { + if (!response.ok) { + const { status, statusText } = response; + throw new Error(`Server responded with HTTP ${status}: ${statusText}`); + } +} + +export { handleHTTPResponseError }; diff --git a/native/backup/api.js b/native/backup/api.js --- a/native/backup/api.js +++ b/native/backup/api.js @@ -6,6 +6,7 @@ import type { BackupAuth, BackupEncrypted } from 'lib/types/backup-types.js'; import { makeBackupServiceEndpointURL } from 'lib/utils/backup-service.js'; import { toBase64URL } from 'lib/utils/base64.js'; +import { handleHTTPResponseError } from 'lib/utils/services-utils.js'; import { getBackupBytesFromBlob } from './conversion-utils.js'; import { commUtilsModule } from '../native-modules.js'; @@ -49,10 +50,7 @@ }, ); - if (!sendBackupResponse.ok) { - const { status, statusText } = sendBackupResponse; - throw new Error(`Server responded with HTTP ${status}: ${statusText}`); - } + handleHTTPResponseError(sendBackupResponse); } async function getBackupID(username: string): Promise { @@ -64,10 +62,7 @@ }, ); - if (!getBackupIDResponse.ok) { - const { status, statusText } = getBackupIDResponse; - throw new Error(`Server responded with HTTP ${status}: ${statusText}`); - } + handleHTTPResponseError(getBackupIDResponse); const { backupID } = await getBackupIDResponse.json(); return backupID; @@ -90,10 +85,7 @@ }, ); - if (!getUserKeysResponse.ok) { - const { status, statusText } = getUserKeysResponse; - throw new Error(`Server responded with HTTP ${status}: ${statusText}`); - } + handleHTTPResponseError(getUserKeysResponse); const blob = await getUserKeysResponse.blob(); return getBackupBytesFromBlob(blob); @@ -116,10 +108,7 @@ }, ); - if (!getUserDataResponse.ok) { - const { status, statusText } = getUserDataResponse; - throw new Error(`Server responded with HTTP ${status}: ${statusText}`); - } + handleHTTPResponseError(getUserDataResponse); const blob = await getUserDataResponse.blob(); return getBackupBytesFromBlob(blob); diff --git a/native/input/input-state-container.react.js b/native/input/input-state-container.react.js --- a/native/input/input-state-container.react.js +++ b/native/input/input-state-container.react.js @@ -102,6 +102,7 @@ generateReportID, useIsReportEnabled, } from 'lib/utils/report-utils.js'; +import { handleHTTPResponseError } from 'lib/utils/services-utils.js'; import { type EditInputBarMessageParameters, @@ -1186,10 +1187,7 @@ }, ); - if (!assignHolderResponse.ok) { - const { status, statusText } = assignHolderResponse; - throw new Error(`Server responded with HTTP ${status}: ${statusText}`); - } + handleHTTPResponseError(assignHolderResponse); const { data_exists: dataExistsResponse } = await assignHolderResponse.json(); blobAlreadyExists = dataExistsResponse; diff --git a/web/input/input-state-container.react.js b/web/input/input-state-container.react.js --- a/web/input/input-state-container.react.js +++ b/web/input/input-state-container.react.js @@ -96,6 +96,7 @@ import { getConfig } from 'lib/utils/config.js'; import { getMessageForException, cloneError } from 'lib/utils/errors.js'; import { generateReportID } from 'lib/utils/report-utils.js'; +import { handleHTTPResponseError } from 'lib/utils/services-utils.js'; import { type PendingMultimediaUpload, @@ -1099,10 +1100,8 @@ }, ); - if (!assignHolderResponse.ok) { - const { status, statusText } = assignHolderResponse; - throw new Error(`Server responded with HTTP ${status}: ${statusText}`); - } + handleHTTPResponseError(assignHolderResponse); + const { data_exists: dataExistsResponse } = await assignHolderResponse.json(); blobAlreadyExists = dataExistsResponse;