1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

Fix custom provider error handling (#1965)

* fixes error handling for is valid and is compatible custom provider input field

* revert some input form changes
This commit is contained in:
Bogdan Fazakas 2023-09-25 07:53:54 +03:00 committed by GitHub
parent 24d8998fc6
commit a976e5b5c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 17 deletions

View File

@ -36,10 +36,17 @@ export default function CustomProvider(props: InputProps): ReactElement {
// No way to detect a failed request with ProviderInstance.isValidProvider, // No way to detect a failed request with ProviderInstance.isValidProvider,
// making this error show up for multiple cases it shouldn't, like network // making this error show up for multiple cases it shouldn't, like network
// down. // down.
if (!isValid) if (!isValid) {
throw Error( setFieldError(
`${field.name}.url`,
'✗ No valid provider detected. Check your network, your URL and try again.' '✗ No valid provider detected. Check your network, your URL and try again.'
) )
LoggerInstance.error(
'[Custom Provider]:',
'✗ No valid provider detected. Check your network, your URL and try again.'
)
return
}
// Check if valid provider is for same chain user is on // Check if valid provider is for same chain user is on
const providerResponse = await axios.get(field.value.url, { const providerResponse = await axios.get(field.value.url, {
@ -53,10 +60,18 @@ export default function CustomProvider(props: InputProps): ReactElement {
providerChain === userChainId providerChain === userChainId
? true ? true
: !!(providerChain.length > 0 && providerChain.includes(userChainId)) : !!(providerChain.length > 0 && providerChain.includes(userChainId))
if (!isCompatible)
throw Error( if (!isCompatible) {
setFieldError(
`${field.name}.url`,
'✗ This provider is incompatible with the network your wallet is connected to.' '✗ This provider is incompatible with the network your wallet is connected to.'
) )
LoggerInstance.error(
'[Custom Provider]:',
'✗ This provider is incompatible with the network your wallet is connected to.'
)
return
}
// if all good, add provider to formik state // if all good, add provider to formik state
helpers.setValue({ url: field.value.url, valid: isValid, custom: true }) helpers.setValue({ url: field.value.url, valid: isValid, custom: true })

View File

@ -72,17 +72,21 @@ export interface InputProps {
disclaimerValues?: string[] disclaimerValues?: string[]
} }
function checkError(form: any, field: FieldInputProps<any>) { function checkError(
const touched = getObjectPropertyByPath(form?.touched, field?.name) form: any,
const errors = getObjectPropertyByPath(form?.errors, field?.name) parsedFieldName: string[],
field: FieldInputProps<any>
return ( ) {
touched && if (
errors && (form?.touched?.[parsedFieldName[0]]?.[parsedFieldName[1]] &&
!field.name.endsWith('.files') && form?.errors?.[parsedFieldName[0]]?.[parsedFieldName[1]]) ||
!field.name.endsWith('.links') && (form?.touched[field?.name] &&
!field.name.endsWith('consumerParameters') form?.errors[field?.name] &&
) field.name !== 'files' &&
field.name !== 'links')
) {
return true
}
} }
export default function Input(props: Partial<InputProps>): ReactElement { export default function Input(props: Partial<InputProps>): ReactElement {
@ -99,10 +103,13 @@ export default function Input(props: Partial<InputProps>): ReactElement {
} = props } = props
const isFormikField = typeof field !== 'undefined' const isFormikField = typeof field !== 'undefined'
const isNestedField = field?.name?.includes('.')
// TODO: this feels hacky as it assumes nested `values` store. But we can't use the // TODO: this feels hacky as it assumes nested `values` store. But we can't use the
// `useField()` hook in here to get `meta.error` so we have to match against form?.errors? // `useField()` hook in here to get `meta.error` so we have to match against form?.errors?
// handling flat and nested data at same time. // handling flat and nested data at same time.
const hasFormikError = checkError(form, field) const parsedFieldName =
isFormikField && (isNestedField ? field?.name.split('.') : [field?.name])
const hasFormikError = checkError(form, parsedFieldName, field)
const styleClasses = cx({ const styleClasses = cx({
field: true, field: true,
@ -117,7 +124,7 @@ export default function Input(props: Partial<InputProps>): ReactElement {
if (disclaimer && disclaimerValues) { if (disclaimer && disclaimerValues) {
setDisclaimerVisible( setDisclaimerVisible(
disclaimerValues.includes( disclaimerValues.includes(
getObjectPropertyByPath(props.form?.values, field?.name) props.form?.values[parsedFieldName[0]]?.[parsedFieldName[1]]
) )
) )
} }