Skip to content

Commit

Permalink
Handle DispatchError failures gracefully (#2182)
Browse files Browse the repository at this point in the history
* Handle DispatchError failures gracefully

* Also allow queue display failures
  • Loading branch information
jacogr committed Jan 23, 2020
1 parent a54eef3 commit 09e974b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
12 changes: 8 additions & 4 deletions packages/react-components/src/Status/Queue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()]));

message = `${error.section}.${error.name}`;
try {
const mod = dispatchError.asModule;
const error = registry.findMetaError(new Uint8Array([mod.index.toNumber(), mod.error.toNumber()]));

message = `${error.section}.${error.name}`;
} catch (error) {
// swallow
}
}

return {
Expand Down
34 changes: 27 additions & 7 deletions packages/react-params/src/Param/DispatchError.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,46 @@
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';

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>({});

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()]));

if (!props.isDisabled || !props.defaultValue?.value?.isModule) {
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
Expand Down

0 comments on commit 09e974b

Please sign in to comment.