From b9329a145650670244ba683e91158e4a13ce9704 Mon Sep 17 00:00:00 2001 From: Jamie Hewitt Date: Mon, 30 Jan 2023 15:35:50 +0300 Subject: [PATCH] Throwing an error if the provider is not available (#1865) * Throwing an error if the provider is not available * Adding jest moch for checking if the provider is valid * Removing console log --- src/@utils/provider.ts | 11 +++++++++++ .../FormInput/InputElement/FilesInput/index.test.tsx | 3 ++- .../FormInput/InputElement/FilesInput/index.tsx | 9 ++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/@utils/provider.ts b/src/@utils/provider.ts index de87533f4..92e30d62b 100644 --- a/src/@utils/provider.ts +++ b/src/@utils/provider.ts @@ -147,3 +147,14 @@ export async function downloadFile( ) await downloadFileBrowser(downloadUrl) } + +export async function checkValidProvider( + providerUrl: string +): Promise { + try { + const response = await ProviderInstance.isValidProvider(providerUrl) + return response + } catch (error) { + LoggerInstance.error(error.message) + } +} diff --git a/src/components/@shared/FormInput/InputElement/FilesInput/index.test.tsx b/src/components/@shared/FormInput/InputElement/FilesInput/index.test.tsx index 2084b5c5c..d9281e4a7 100644 --- a/src/components/@shared/FormInput/InputElement/FilesInput/index.test.tsx +++ b/src/components/@shared/FormInput/InputElement/FilesInput/index.test.tsx @@ -2,7 +2,7 @@ import { fireEvent, render, screen } from '@testing-library/react' import React from 'react' import FilesInput from './index' import { useField } from 'formik' -import { getFileInfo } from '@utils/provider' +import { getFileInfo, checkValidProvider } from '@utils/provider' jest.mock('formik') jest.mock('@utils/provider') @@ -48,6 +48,7 @@ const mockForm = { describe('@shared/FormInput/InputElement/FilesInput', () => { it('renders without crashing', async () => { ;(useField as jest.Mock).mockReturnValue([mockField, mockMeta, mockHelpers]) + ;(checkValidProvider as jest.Mock).mockReturnValue(true) ;(getFileInfo as jest.Mock).mockReturnValue([ { valid: true, diff --git a/src/components/@shared/FormInput/InputElement/FilesInput/index.tsx b/src/components/@shared/FormInput/InputElement/FilesInput/index.tsx index 5376c37d3..7d4094617 100644 --- a/src/components/@shared/FormInput/InputElement/FilesInput/index.tsx +++ b/src/components/@shared/FormInput/InputElement/FilesInput/index.tsx @@ -3,7 +3,7 @@ import { useField } from 'formik' import FileInfo from './Info' import UrlInput from '../URLInput' import { InputProps } from '@shared/FormInput' -import { getFileInfo } from '@utils/provider' +import { getFileInfo, checkValidProvider } from '@utils/provider' import { LoggerInstance } from '@oceanprotocol/lib' import { useAsset } from '@context/Asset' import { isGoogleUrl } from '@utils/url/index' @@ -33,6 +33,13 @@ export default function FilesInput(props: InputProps): ReactElement { ) } + // Check if provider is a valid provider + const isValid = await checkValidProvider(providerUrl) + if (!isValid) + throw Error( + '✗ Provider cannot be reached, please check status.oceanprotocol.com and try again later.' + ) + const checkedFile = await getFileInfo(url, providerUrl, storageType) // error if something's not right from response