Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle DispatchError failures gracefully #2182

Merged
merged 2 commits into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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