Skip to content

Commit

Permalink
Update error response parsing from ARC broadcaster
Browse files Browse the repository at this point in the history
  • Loading branch information
tonesnotes committed Jun 28, 2024
1 parent 6dc80a7 commit 6dce0be
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/transaction/Broadcaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ export interface BroadcastResponse {
* @interface
* @property {string} status - The status of the response, indicating an error.
* @property {string} code - A machine-readable error code representing the type of error encountered.
* @property {string} txid - The transaction ID of the broadcasted transaction.
* @property {string} description - A detailed description of the error.
*/
export interface BroadcastFailure {
status: 'error'
code: string
txid?: string
description: string
}

Expand Down
21 changes: 14 additions & 7 deletions src/transaction/broadcasters/ARC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,25 @@ export default class ARC implements Broadcaster {
message: `${txStatus} ${extraInfo}`
}
} else {
const st = typeof response.status
const r: BroadcastFailure = {
status: 'error',
code: response.status.toString() ?? 'ERR_UNKNOWN',
code: st === 'number' || st === 'string' ? response.status.toString() : 'ERR_UNKNOWN',
description: 'Unknown error'
}
if (typeof response.data === 'string') {
let d = response.data
if (typeof d === 'string') {
try {
const data = JSON.parse(response.data)
if (typeof data.detail === 'string') {
r.description = data.detail
}
} catch {}
d = JSON.parse(response.data)
} catch { }
}
if (typeof d === 'object') {
if (typeof d.txid === 'string') {
r.txid = d.txid
}
if (typeof d.detail === 'string') {
r.description = d.detail
}
}
return r
}
Expand Down
26 changes: 24 additions & 2 deletions src/transaction/broadcasters/__tests/ARC.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,29 @@ describe('ARC Broadcaster', () => {
})
})

it('handles error 460', async () => {
// Model the actual response format received from...
//const URL = 'https://arc.taal.com'
//const apiKey = 'mainnet_9596de07e92300c6287e4393594ae39c'
//const arc = new ARC(URL, apiKey)

const mockFetch = mockedFetch({
status: 460,
data: {
status: 460,
detail: 'Transaction is not in extended format, missing input scripts',
txid: 'd21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43'
}
})

const broadcaster = new ARC(URL, {httpClient: new FetchHttpClient(mockFetch)})
const response = await broadcaster.broadcast(transaction)
expect(response.status).toBe('error')
expect(response.code).toBe('460')
expect(response.description).toBe('Transaction is not in extended format, missing input scripts')
expect(response.txid).toBe('d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43')
})

function mockedFetch(response) {
return jest.fn().mockResolvedValue({
ok: response.status === 200,
Expand Down Expand Up @@ -222,5 +245,4 @@ describe('ARC Broadcaster', () => {
jest.mock('https', () => https)
return https
}
})

})

0 comments on commit 6dce0be

Please sign in to comment.