mirror of
https://github.com/alexgo-io/stacks-blockchain-api.git
synced 2026-04-28 21:05:36 +08:00
refactor: added skip in logger, changed the name of error class
This commit is contained in:
@@ -24,7 +24,7 @@ import { createRosettaMempoolRouter } from './routes/rosetta/mempool';
|
||||
import { createRosettaBlockRouter } from './routes/rosetta/block';
|
||||
import { createRosettaAccountRouter } from './routes/rosetta/account';
|
||||
import { createRosettaConstructionRouter } from './routes/rosetta/construction';
|
||||
import { isProdEnv, logError, logger, LogLevel, RespError, waiter } from '../helpers';
|
||||
import { isProdEnv, logError, logger, LogLevel, InvalidRequestError, waiter } from '../helpers';
|
||||
import { createWsRpcRouter } from './routes/ws/ws-rpc';
|
||||
import { createSocketIORouter } from './routes/ws/socket-io';
|
||||
import { createBurnchainRouter } from './routes/burnchain';
|
||||
@@ -241,15 +241,16 @@ export async function startApiServer(opts: {
|
||||
setResponseNonCacheable(res);
|
||||
}
|
||||
if (error && !res.headersSent) {
|
||||
if (error instanceof RespError) {
|
||||
return res.status(error.status).send({ error: error.message });
|
||||
if (error instanceof InvalidRequestError) {
|
||||
res.status(error.status).json({ error: error.message }).end();
|
||||
} else {
|
||||
res.status(500);
|
||||
const errorTag = uuid();
|
||||
Object.assign(error, { errorTag: errorTag });
|
||||
res
|
||||
.json({ error: error.toString(), stack: (error as Error).stack, errorTag: errorTag })
|
||||
.end();
|
||||
}
|
||||
res.status(500);
|
||||
const errorTag = uuid();
|
||||
Object.assign(error, { errorTag: errorTag });
|
||||
res
|
||||
.json({ error: error.toString(), stack: (error as Error).stack, errorTag: errorTag })
|
||||
.end();
|
||||
}
|
||||
next(error);
|
||||
}) as express.ErrorRequestHandler);
|
||||
@@ -259,6 +260,10 @@ export async function startApiServer(opts: {
|
||||
winstonInstance: logger as winston.Logger,
|
||||
metaField: (null as unknown) as string,
|
||||
blacklistedMetaFields: ['trace', 'os', 'process'],
|
||||
skip: (_req, _res, error) => {
|
||||
// Do not log errors for client 4xx responses
|
||||
return error instanceof InvalidRequestError;
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ export const pipelineAsync = util.promisify(stream.pipeline);
|
||||
enum RespErrorType {
|
||||
invalid_tx_id = 'Invalid tx id',
|
||||
}
|
||||
export class RespError extends Error {
|
||||
export class InvalidRequestError extends Error {
|
||||
type: RespErrorType;
|
||||
status: number;
|
||||
constructor(msg: string, type: RespErrorType, status: number = 400) {
|
||||
@@ -530,13 +530,13 @@ export function hexToBuffer(hex: string): Buffer {
|
||||
return Buffer.alloc(0);
|
||||
}
|
||||
if (!hex.startsWith('0x')) {
|
||||
throw new RespError(
|
||||
throw new InvalidRequestError(
|
||||
`Hex string is missing the "0x" prefix: "${hex}"`,
|
||||
RespErrorType.invalid_tx_id
|
||||
);
|
||||
}
|
||||
if (hex.length % 2 !== 0) {
|
||||
throw new RespError(
|
||||
throw new InvalidRequestError(
|
||||
`Hex string is an odd number of digits: ${hex}`,
|
||||
RespErrorType.invalid_tx_id
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user