Skip to content

Commit

Permalink
Merge pull request #73 from ckb-cell/develop
Browse files Browse the repository at this point in the history
fix ElectrsAPINotFoundError
  • Loading branch information
Flouse authored Apr 9, 2024
2 parents e63ba8a + 34b3a1b commit 952b1bc
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "btc-assets-api",
"version": "1.2.0",
"version": "1.2.1",
"title": "Bitcoin/RGB++ Assets API",
"description": "",
"main": "index.js",
Expand Down
10 changes: 7 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import cors from './plugins/cors';
import { NetworkType } from './constants';
import rgbppRoutes from './routes/rgbpp';
import cronRoutes from './routes/cron';
import { ElectrsAPIError } from './services/electrs';
import { ElectrsAPIError, ElectrsAPINotFoundError } from './services/electrs';
import { BitcoinRPCError } from './services/bitcoind';
import { AppErrorCode } from './error';
import { provider } from 'std-env';
Expand Down Expand Up @@ -63,13 +63,17 @@ async function routes(fastify: FastifyInstance) {
}

fastify.addHook('onRequest', async (request) => {
Sentry.setTag('routePath', request.routerPath);
Sentry.setTag('url', request.url);
Sentry.setContext('params', request.params ?? {});
Sentry.setContext('query', request.query ?? {});
});

fastify.setErrorHandler((error, _, reply) => {
if (error instanceof ElectrsAPIError || error instanceof BitcoinRPCError) {
if (
error instanceof ElectrsAPIError ||
error instanceof ElectrsAPINotFoundError ||
error instanceof BitcoinRPCError
) {
reply
.status(error.statusCode ?? HttpStatusCode.InternalServerError)
.send({ code: error.errorCode, message: error.message });
Expand Down
7 changes: 5 additions & 2 deletions src/services/electrs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios, { AxiosError, AxiosInstance, AxiosResponse } from 'axios';
import axios, { AxiosError, AxiosInstance, AxiosResponse, HttpStatusCode } from 'axios';
import { Block, Transaction, UTXO } from '../routes/bitcoin/types';
import * as Sentry from '@sentry/node';
import { Cradle } from '../container';
Expand Down Expand Up @@ -46,6 +46,9 @@ export class ElectrsAPIError extends Error {
}

export class ElectrsAPINotFoundError extends Error {
public errorCode = HttpStatusCode.NotFound;
public statusCode = HttpStatusCode.NotFound;

constructor(message: string) {
super(message);
this.name = this.constructor.name;
Expand All @@ -69,7 +72,7 @@ export default class ElectrsAPI {
return response;
} catch (err) {
if (err instanceof AxiosError) {
if (err.status === 404) {
if (err.status === 404 || err.response?.status === 404) {
throw new ElectrsAPINotFoundError(err.message);
}

Expand Down
1 change: 1 addition & 0 deletions src/services/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ export default class TransactionManager implements ITransactionManager {
await this.moveJobToDelayed(job, token);
return;
}
Sentry.setTag('txid', txid);
Sentry.setContext('job', {
txid,
ckbVirtualResult: {
Expand Down

0 comments on commit 952b1bc

Please sign in to comment.