DispatchError.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2017-2020 @polkadot/react-components authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the Apache-2.0 license. See the LICENSE file for details.
  4. import { DispatchError } from '@polkadot/types/interfaces';
  5. import { Props } from '../types';
  6. import React, { useEffect, useState } from 'react';
  7. import { registry } from '@polkadot/react-api';
  8. import { Input } from '@polkadot/react-components';
  9. import { useTranslation } from '../translate';
  10. import Static from './Static';
  11. import Unknown from './Unknown';
  12. interface Details {
  13. details?: string | null;
  14. type?: string;
  15. }
  16. function ErrorDisplay (props: Props): React.ReactElement<Props> {
  17. const { t } = useTranslation();
  18. const [{ details, type }, setDetails] = useState<Details>({});
  19. useEffect((): void => {
  20. if (details !== null && props.defaultValue?.value?.isModule) {
  21. try {
  22. const { documentation, name, section } = registry.findMetaError((props.defaultValue.value as DispatchError).asModule);
  23. setDetails({
  24. details: documentation.join(', '),
  25. type: `${section}.${name}`
  26. });
  27. } catch (error) {
  28. // Errors may not actually be exposed, in this case, just return the default representation
  29. console.error(error);
  30. setDetails({ details: null });
  31. }
  32. }
  33. }, [props.defaultValue]);
  34. if (!props.isDisabled || !details) {
  35. return <Unknown {...props} />;
  36. }
  37. return (
  38. <Static {...props}>
  39. <Input
  40. className='full'
  41. isDisabled
  42. label={t('type')}
  43. value={type}
  44. />
  45. {details && (
  46. <Input
  47. className='full'
  48. isDisabled
  49. label={t('details')}
  50. value={details}
  51. />
  52. )}
  53. </Static>
  54. );
  55. }
  56. export default React.memo(ErrorDisplay);