Skip to content

Commit

Permalink
Merge pull request #129 from ckb-cell/fix/catch-bitcoin-client-error-…
Browse files Browse the repository at this point in the history
…message

fix: fix bitcoin client error message handle
  • Loading branch information
ahonn authored May 7, 2024
2 parents c6c53e0 + 7356146 commit 9e29e01
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 823 deletions.
4 changes: 2 additions & 2 deletions src/plugins/sentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fastifySentry from '@immobiliarelabs/fastify-sentry';
import { ProfilingIntegration } from '@sentry/profiling-node';
import pkg from '../../package.json';
import { env } from '../env';
import { HttpStatusCode, AxiosError } from 'axios';
import { HttpStatusCode, isAxiosError } from 'axios';
import { BitcoinClientAPIError } from '../services/bitcoin';

export default fp(async (fastify) => {
Expand All @@ -23,7 +23,7 @@ export default fp(async (fastify) => {
return;
}

if (error instanceof AxiosError) {
if (isAxiosError(error)) {
const { response } = error;
reply.status(response?.status ?? HttpStatusCode.InternalServerError).send({
message: response?.data ?? error.message,
Expand Down
10 changes: 5 additions & 5 deletions src/services/bitcoin/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AxiosError, HttpStatusCode } from 'axios';
import { HttpStatusCode, isAxiosError } from 'axios';
import * as Sentry from '@sentry/node';
import { Cradle } from '../../container';
import { IBitcoinBroadcastBackuper, IBitcoinDataProvider } from './interface';
Expand Down Expand Up @@ -115,10 +115,10 @@ export default class BitcoinClient implements IBitcoinClient {
return result;
} catch (err) {
this.cradle.logger.error(err);
if ((err as AxiosError).isAxiosError) {
const error = new BitcoinClientAPIError((err as AxiosError).message);
if ((err as AxiosError).response) {
error.statusCode = (err as AxiosError).response?.status || 500;
if (isAxiosError(err)) {
const error = new BitcoinClientAPIError(err.response?.data ?? err.message);
if (err.response?.status) {
error.statusCode = err.response.status;
}
throw error;
}
Expand Down
6 changes: 6 additions & 0 deletions test/routes/bitcoin/__snapshots__/transaction.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Get not exists transaction 1`] = `
{
"message": "Transaction not found",
}
`;

exports[`Get transaction by txid 1`] = `
{
"fee": 141,
Expand Down
4 changes: 1 addition & 3 deletions test/routes/bitcoin/transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ describe('/bitcoin/v1/transaction', () => {
const data = await response.json();

expect(response.statusCode).toBe(404);
expect(data).toEqual({
message: 'Request failed with status code 404',
});
expect(data).toMatchSnapshot();

await fastify.close();
});
Expand Down
194 changes: 0 additions & 194 deletions test/routes/bitcoind/__snapshots__/address.test.ts.snap

This file was deleted.

31 changes: 0 additions & 31 deletions test/routes/bitcoind/__snapshots__/block.test.ts.snap

This file was deleted.

109 changes: 0 additions & 109 deletions test/routes/bitcoind/__snapshots__/transaction.test.ts.snap

This file was deleted.

Loading

0 comments on commit 9e29e01

Please sign in to comment.