Skip to content

Commit

Permalink
Add error logs on send message (#1374)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitayutanov committed Sep 6, 2023
1 parent a231a4c commit 1ad9d76
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 26 deletions.
6 changes: 3 additions & 3 deletions utils/gear-hooks/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gear-js/react-hooks",
"version": "0.6.1",
"version": "0.6.2",
"description": "React hooks used across Gear applications",
"author": "Gear Technologies",
"license": "GPL-3.0",
Expand All @@ -21,14 +21,14 @@
"prepare": "npm run build"
},
"peerDependencies": {
"@gear-js/api": "0.33.0",
"@gear-js/api": "0.33.1",
"@polkadot/api": "10.9.1",
"@polkadot/extension-dapp": "0.46.5",
"react": "^18.2.0",
"react-transition-group": "4.4.5"
},
"devDependencies": {
"@gear-js/api": "0.33.0",
"@gear-js/api": "0.33.1",
"@polkadot/api": "10.9.1",
"@polkadot/extension-dapp": "0.46.5",
"@polkadot/extension-inject": "0.46.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { HexString } from '@polkadot/util/types';
import { useContext } from 'react';
import { DEFAULT_SUCCESS_OPTIONS, DEFAULT_ERROR_OPTIONS } from 'consts';
import { AlertContext, ApiContext } from 'context';
import { getExtrinsicFailedMessage } from 'utils';
import { Callbacks, Method, HandleSignStatusParams, TransactionStatus, ProgramError } from '../types';
import { getExtrinsicFailedMessage } from '../utils';

function useHandleSignStatus() {
const { api } = useContext(ApiContext); // сircular dependency fix
Expand Down
10 changes: 1 addition & 9 deletions utils/gear-hooks/src/hooks/api/useProgram/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import { GearApi } from '@gear-js/api';
import { UnsubscribePromise } from '@polkadot/api/types';
import { Event } from '@polkadot/types/interfaces';
import { ProgramStatus } from './types';

const getExtrinsicFailedMessage = (api: GearApi, event: Event) => {
const { docs, method: errorMethod } = api.getExtrinsicFailedError(event);
const formattedDocs = docs.filter(Boolean).join('. ');

return `${errorMethod}: ${formattedDocs}`;
};

const waitForProgramInit = (api: GearApi, programId: string) => {
let unsubPromise: UnsubscribePromise;

Expand All @@ -28,4 +20,4 @@ const waitForProgramInit = (api: GearApi, programId: string) => {
}).finally(unsubscribe);
};

export { getExtrinsicFailedMessage, waitForProgramInit };
export { waitForProgramInit };
19 changes: 14 additions & 5 deletions utils/gear-hooks/src/hooks/api/useSendMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { web3FromSource } from '@polkadot/extension-dapp';
import { EventRecord } from '@polkadot/types/interfaces';
import { AnyJson, ISubmittableResult } from '@polkadot/types/types';
import { HexString } from '@polkadot/util/types';
import { useContext, useRef } from 'react';
import { useContext } from 'react';
import { AccountContext, AlertContext, ApiContext } from 'context';
import { DEFAULT_ERROR_OPTIONS, DEFAULT_SUCCESS_OPTIONS } from 'consts';
import { getAutoGasLimit } from 'utils';
import { getAutoGasLimit, getExtrinsicFailedMessage } from 'utils';

type UseSendMessageOptions = {
isMaxGasLimit?: boolean;
Expand Down Expand Up @@ -34,13 +34,18 @@ function useSendMessage(
const title = 'gear.sendMessage';

const handleEventsStatus = (events: EventRecord[], onSuccess?: () => void, onError?: () => void) => {
events.forEach(({ event: { method, section } }) => {
events.forEach(({ event }) => {
const { method, section } = event;

if (method === 'MessageQueued') {
if (!disableAlerts) alert.success(`${section}.MessageQueued`);

onSuccess && onSuccess();
} else if (method === 'ExtrinsicFailed') {
alert.error('Extrinsic Failed', { title });
const message = getExtrinsicFailedMessage(api, event);

console.error(message);
alert.error(message, { title });

onError && onError();
}
Expand Down Expand Up @@ -89,7 +94,11 @@ function useSendMessage(
.then(({ signer }) =>
api.message.signAndSend(address, { signer }, (result) => handleStatus(result, alertId, onSuccess, onError)),
)
.catch(({ message }: Error) => {
.catch((error: Error) => {
const { message } = error;

console.error(error);

if (alertId) {
alert.update(alertId, message, DEFAULT_ERROR_OPTIONS);
} else {
Expand Down
12 changes: 10 additions & 2 deletions utils/gear-hooks/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GasInfo } from '@gear-js/api';
import { GasInfo, GearApi } from '@gear-js/api';
import type { InjectedAccountWithMeta } from '@polkadot/extension-inject/types';
import { Event } from '@polkadot/types/interfaces';
import { bnToBn } from '@polkadot/util';

const isLoggedIn = ({ address }: InjectedAccountWithMeta) => localStorage.account === address;
Expand All @@ -9,4 +10,11 @@ const getAutoGasLimit = ({ waited, min_limit }: GasInfo) =>

const withoutCommas = (value: string) => value.replace(/,/g, '');

export { isLoggedIn, getAutoGasLimit, withoutCommas };
const getExtrinsicFailedMessage = (api: GearApi, event: Event) => {
const { docs, method: errorMethod } = api.getExtrinsicFailedError(event);
const formattedDocs = docs.filter(Boolean).join('. ');

return `${errorMethod}: ${formattedDocs}`;
};

export { isLoggedIn, getAutoGasLimit, withoutCommas, getExtrinsicFailedMessage };
12 changes: 6 additions & 6 deletions utils/gear-hooks/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,22 @@ __metadata:
languageName: node
linkType: hard

"@gear-js/api@npm:0.33.0":
version: 0.33.0
resolution: "@gear-js/api@npm:0.33.0"
"@gear-js/api@npm:0.33.1":
version: 0.33.1
resolution: "@gear-js/api@npm:0.33.1"
peerDependencies:
"@polkadot/api": 10.9.1
"@polkadot/wasm-crypto": 7.2.1
rxjs: 7.8.1
checksum: 49cdd845dfc357baf2c206d628cfa879b67a091d0f2c14b212b7e9942f70bbd1b64ffe9b30a4749d492f156a61cca57936aa5a382e71465a2f7abf168ce382ca
checksum: 6de7f89e52c7a17ae0978dc9c30b9eac15a19df45193647ed3bbc2690b2f15daaf25ffcc9b3df9e0f1b57d000519461f44b2b60173b94251f5edcd2bfda104c7
languageName: node
linkType: hard

"@gear-js/react-hooks@workspace:.":
version: 0.0.0-use.local
resolution: "@gear-js/react-hooks@workspace:."
dependencies:
"@gear-js/api": 0.33.0
"@gear-js/api": 0.33.1
"@polkadot/api": 10.9.1
"@polkadot/extension-dapp": 0.46.5
"@polkadot/extension-inject": 0.46.5
Expand All @@ -84,7 +84,7 @@ __metadata:
rollup-plugin-typescript2: 0.31.2
typescript: 4.9.5
peerDependencies:
"@gear-js/api": 0.33.0
"@gear-js/api": 0.33.1
"@polkadot/api": 10.9.1
"@polkadot/extension-dapp": 0.46.5
react: ^18.2.0
Expand Down

0 comments on commit 1ad9d76

Please sign in to comment.