Handle DispatchError failures gracefully (#2182)

* Handle DispatchError failures gracefully

* Also allow queue display failures
This commit is contained in:
Jaco Greeff 2020-01-23 13:52:18 +01:00 committed by GitHub
parent a54eef368f
commit 09e974b777
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 10 deletions

View File

@ -135,10 +135,14 @@ export default function Queue ({ children }: Props): React.ReactElement<Props> {
let message = dispatchError.type;
if (dispatchError.isModule) {
const mod = dispatchError.asModule;
const error = registry.findMetaError(new Uint8Array([mod.index.toNumber(), mod.error.toNumber()]));
try {
const mod = dispatchError.asModule;
const error = registry.findMetaError(new Uint8Array([mod.index.toNumber(), mod.error.toNumber()]));
message = `${error.section}.${error.name}`;
message = `${error.section}.${error.name}`;
} catch (error) {
// swallow
}
}
return {

View File

@ -5,7 +5,7 @@
import { DispatchError } from '@polkadot/types/interfaces';
import { Props } from '../types';
import React from 'react';
import React, { useEffect, useState } from 'react';
import { registry } from '@polkadot/react-api';
import { Input } from '@polkadot/react-components';
@ -13,18 +13,38 @@ import { useTranslation } from '../translate';
import Static from './Static';
import Unknown from './Unknown';
interface Details {
details?: string | null;
type?: string;
}
export default function ErrorDisplay (props: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const [{ details, type }, setDetails] = useState<Details>({});
if (!props.isDisabled || !props.defaultValue?.value?.isModule) {
useEffect((): void => {
if (details !== null && props.defaultValue?.value?.isModule) {
try {
const mod = (props.defaultValue.value as DispatchError).asModule;
const error = registry.findMetaError(new Uint8Array([mod.index.toNumber(), mod.error.toNumber()]));
setDetails({
details: error.documentation.join(', '),
type: `${error.section}.${error.name}`
});
} catch (error) {
// Errors may not actually be exposed, in this case, just return the default representation
console.error(error);
setDetails({ details: null });
}
}
}, [props.defaultValue]);
if (!props.isDisabled || !details) {
return <Unknown {...props} />;
}
const mod = (props.defaultValue.value as DispatchError).asModule;
const error = registry.findMetaError(new Uint8Array([mod.index.toNumber(), mod.error.toNumber()]));
const type = `${error.section}.${error.name}`;
const details = error.documentation.join(', ');
return (
<Static {...props}>
<Input