Skip to content

Commit

Permalink
fix(error): show contract errors
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasGassmann committed Feb 5, 2021
1 parent 25c0e1a commit 95a0a76
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
5 changes: 1 addition & 4 deletions src/app/pages/error/error.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@

<ion-content class="ion-padding">
<p class="ion-margin-bottom">{{ message }}</p>
<pre *ngIf="data">
{{ data | json }}
</pre
>
<pre *ngIf="data">{{ data | json }}</pre>

<ion-fab vertical="bottom" horizontal="end" slot="fixed">
<ion-button fill="outline" color="primary" (click)="dismiss()">
Expand Down
14 changes: 14 additions & 0 deletions src/extension/extension-client/ExtensionClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,20 @@ export class ExtensionClient extends BeaconClient {
await this.processMessage(beaconMessage, request)
}

const errorData = (beaconMessage as any).errorData as unknown
if (
errorData &&
(
!Array.isArray(errorData) ||
!errorData.every((item) => Boolean(item.kind) && Boolean(item.id))
)
) {
logger.warn(
'ErrorData provided is not in correct format. It needs to be an array of RPC errors. It will not be included in the message sent to the dApp'
)
delete (beaconMessage as any).errorData
}

// TODO: Remove v1 compatibility in later version
; (beaconMessage as any).beaconId = beaconMessage.senderId

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
OperationResponseInput
} from '@airgap/beacon-sdk'
import { TezosWrappedOperation } from '@airgap/coinlib-core/protocols/tezos/types/TezosWrappedOperation'
import { AxiosResponse } from 'axios'
import { LedgerSigner, LocalSigner } from 'src/extension/AirGapSigner'

import { WalletInfo, WalletType } from '../Actions'
Expand All @@ -34,17 +35,16 @@ export const operationRequestHandler: (client: ExtensionClient, logger: Logger)

let responseInput: OperationResponseInput

const sendError: (error: Error, errorType: BeaconErrorType, errorData?: any) => Promise<void> = async (
const sendError: (error: Error, errorType: BeaconErrorType) => Promise<void> = async (
error: Error,
errorType: BeaconErrorType,
errorData?: any
): Promise<void> => {
logger.log('error', error)
logger.log('operationRequestHandler', 'error ', error)
responseInput = {
id: operationRequest.id,
type: BeaconMessageType.OperationResponse,
errorType,
errorData
errorData: (error as any as AxiosResponse).data
} as any

const response: OperationResponse = {
Expand Down Expand Up @@ -98,7 +98,7 @@ export const operationRequestHandler: (client: ExtensionClient, logger: Logger)

const hash: To<string> = await to(client.operationProvider.broadcast(operationRequest.network, signedTx.res))
if (hash.err) {
await sendError(hash.err, BeaconErrorType.TRANSACTION_INVALID_ERROR, forgedTx.err)
await sendError(hash.err, BeaconErrorType.TRANSACTION_INVALID_ERROR)
throw forgedTx.err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
SignPayloadRequestOutput
} from '@airgap/beacon-sdk'
import { TezosWrappedOperation } from '@airgap/coinlib-core/protocols/tezos/types/TezosWrappedOperation'
import { AxiosResponse } from 'axios'

import { WalletInfo } from '../Actions'
import { ExtensionClient } from '../ExtensionClient'
Expand Down Expand Up @@ -75,12 +76,15 @@ export class ToExtensionMessageHandler extends MessageHandler {
error: Error,
errorType: BeaconErrorType
): Promise<void> => {
logger.log('error', error)
logger.log('handle', 'error', error)

const errorData = (error as any as AxiosResponse).data.data || (error as any as AxiosResponse).data

const responseInput = {
id: deserialized.id,
type: BeaconMessageType.OperationResponse,
errorType,
errorData: error
errorData
} as any

const response: OperationResponse = {
Expand All @@ -90,7 +94,7 @@ export class ToExtensionMessageHandler extends MessageHandler {
}
await this.client.sendToPage(response)

const errorObject = { title: (error as any).name, message: (error as any).message, data: (error as any).data }
const errorObject = { title: (error as any).name, message: (error as any).message, data: errorData }

return this.client.sendToPopup({ ...data, payload: { error: errorObject } })
}
Expand All @@ -110,7 +114,7 @@ export class ToExtensionMessageHandler extends MessageHandler {

if (deserialized.type === BeaconMessageType.OperationRequest) {
// Intercept Operation request and enrich it with information
;(async (): Promise<void> => {
; (async (): Promise<void> => {
const operationRequest: OperationRequestOutput = enriched.res as OperationRequestOutput

const wallet: WalletInfo | undefined = await this.client.getWalletByAddress(operationRequest.sourceAddress)
Expand All @@ -134,13 +138,8 @@ export class ToExtensionMessageHandler extends MessageHandler {

return this.client.sendToPopup({ ...data, payload: serialized })
})().catch(async (operationPrepareError: Error) => {
if ((operationPrepareError as any).data) {
await sendError((operationPrepareError as any).data, BeaconErrorType.TRANSACTION_INVALID_ERROR)
logger.error('operationPrepareError', (operationPrepareError as any).data)
} else {
await sendError(operationPrepareError, BeaconErrorType.TRANSACTION_INVALID_ERROR)
logger.error('operationPrepareError', operationPrepareError)
}
await sendError(operationPrepareError, BeaconErrorType.TRANSACTION_INVALID_ERROR)
logger.error('operationPrepareError', operationPrepareError)
})
} else {
const serialized: string = await new Serializer().serialize(enriched.res)
Expand Down

0 comments on commit 95a0a76

Please sign in to comment.