Skip to content

Commit

Permalink
feat: handle tx metadata generation errors (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelm41 authored Jun 6, 2024
1 parent fc24b15 commit e57c59c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
18 changes: 16 additions & 2 deletions src/handlers/transactionMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { Response, Request } from 'express'
import { Chain, getChains } from '../utils/chains'
import { renderChainNotFoundError, renderGetMetadataFirstError, renderInternalError, renderMissingTxBlobError } from '../utils/errors'
import {
renderChainNotFoundError,
renderGetMetadataFirstError,
renderInternalError,
renderMissingTxBlobError,
renderShortenerMetadataError
} from '../utils/errors'
import { cacheMetadata } from '../utils/metadata'
import { getShortMetadataFromTxBlob } from '../../rust'
import { TxToSign } from '../utils/types'

const validHex = new RegExp(/^[a-fA-F0-9]+$/)

export const transactionMetadata = async (req: Request, res: Response) => {
const chains = getChains()

Expand Down Expand Up @@ -45,7 +53,13 @@ export const transactionMetadata = async (req: Request, res: Response) => {
txBlob = txBlob.substring(2)
}

const txMetadata = Buffer.from(getShortMetadataFromTxBlob({ txBlob, metadata: metadataHex, props }), 'hex')
const result = getShortMetadataFromTxBlob({ txBlob, metadata: metadataHex, props })

if(!validHex.test(result)){
renderShortenerMetadataError(res, result)
return
}
const txMetadata = Buffer.from(result, 'hex')
res.status(200).send({ txMetadata: '0x' + txMetadata.toString('hex') })
} catch (e) {
renderInternalError(res, e)
Expand Down
4 changes: 4 additions & 0 deletions src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export const renderError = (res: Response, httpCode: number, e: unknown) => {
res.status(httpCode).json({ errorMessage })
}

export const renderShortenerMetadataError = (res: Response, e: string) => {
renderError(res, 500, new Error("failed to generate shortened metadata: [" + e + "]" ))
}

export const renderInternalError = (res: Response, e: unknown) => {
const errorMessage = e instanceof ChainError ? e.message : defaultInternalErrorMsg
renderError(res, 500, new Error(errorMessage))
Expand Down
20 changes: 19 additions & 1 deletion tests/common.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createAndServe } from '../src/server'
import axios from 'axios'
import axios, {Axios, AxiosError, isAxiosError} from 'axios'
import { describe } from 'node:test'
import http from 'http'

Expand Down Expand Up @@ -56,6 +56,24 @@ describe('basic api', () => {
)
})

test('fail to get tx metadata', async () => {
try {
const resp = await axios.post('http://127.0.0.1:3001/transaction/metadata', {
txBlob:
'11001c0c591bd5a5f69ae815f6aae85433bf5ef7e703cbb9c8b64bc69731252d4c0121710f001a0000006408de7737c59c238890533af25896a2c20608d8b380bb01029acb392781063eacb0ce6e825b73b46536726994c63f4684fd8a72976e07f551de5954f0df5cb501b6648e3f302d557ff1ee5e6d2462f2c668b1c4ac92db6a05c6ab857372c10a13',
chain: {id: 'roc'},
})
}catch(e:any){
expect(isAxiosError(e)).toBe(true)
expect(!!e.response).toBe(true)
expect(!!e.response.data).toBe(true)
if(e.response && e.response.data){
expect(e.response.status).toBe(500)
expect(e.response.data.errorMessage).toBe("failed to generate shortened metadata: [Failed to decode call: Could not find variant with index 17]")
}
}
})

test('flush chain metadata', async () => {
const resp = await axios.post('http://127.0.0.1:3001/node/metadata/flush', { id: 'roc' })
expect(resp.status).toBe(200)
Expand Down

0 comments on commit e57c59c

Please sign in to comment.