Skip to content

Commit

Permalink
[R4R] DTL response null result with 404 http status
Browse files Browse the repository at this point in the history
[R4R] DTL response null result with 404 http status
  • Loading branch information
shidaxi committed Dec 22, 2023
1 parent 14807b5 commit c6ef99c
Show file tree
Hide file tree
Showing 3 changed files with 381 additions and 17 deletions.
41 changes: 24 additions & 17 deletions packages/data-transport-layer/src/services/server/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { JsonRpcProvider } from '@ethersproject/providers'
import { LevelUp } from 'levelup'
import * as Sentry from '@sentry/node'
import * as Tracing from '@sentry/tracing'
import {HttpCodes} from "../../utils";

/* Imports: Internal */

Expand Down Expand Up @@ -237,7 +238,13 @@ export class L1TransportServer extends BaseService<L1TransportServerOptions> {
this.state.app[method](route, async (req, res) => {
const start = Date.now()
try {
const json = await handler(req, res)
let httpCode = HttpCodes.OK, json;
const result = await handler(req, res)
if(Array.isArray(result)) {
[json, httpCode] = result;
} else {
json = result;
}
const elapsed = Date.now() - start
this.logger.info('Served HTTP Request', {
method: req.method,
Expand All @@ -249,7 +256,7 @@ export class L1TransportServer extends BaseService<L1TransportServerOptions> {
url: req.url,
body: json,
})
return res.json(json)
return res.status(httpCode).json(json)
} catch (e) {
const elapsed = Date.now() - start
this.logger.error('Failed HTTP Request', {
Expand Down Expand Up @@ -498,7 +505,7 @@ export class L1TransportServer extends BaseService<L1TransportServerOptions> {
this._registerRoute(
'get',
'/transaction/index/:index',
async (req): Promise<TransactionResponse> => {
async (req): Promise<[TransactionResponse, HttpCodes]> => {
const backend = req.query.backend || this.options.defaultBackend
let transaction = null

Expand All @@ -518,20 +525,20 @@ export class L1TransportServer extends BaseService<L1TransportServerOptions> {
}

if (transaction === null) {
return {
return [{
transaction: null,
batch: null,
}
}, HttpCodes.NOT_FOUND]
}

const batch = await this.state.db.getTransactionBatchByIndex(
transaction.batchIndex
)

return {
return [{
transaction,
batch,
}
}, HttpCodes.OK]
}
)

Expand Down Expand Up @@ -565,16 +572,16 @@ export class L1TransportServer extends BaseService<L1TransportServerOptions> {
this._registerRoute(
'get',
'/batch/transaction/index/:index',
async (req): Promise<TransactionBatchResponse> => {
async (req): Promise<[TransactionBatchResponse, HttpCodes]> => {
const batch = await this.state.db.getTransactionBatchByIndex(
BigNumber.from(req.params.index).toNumber()
)

if (batch === null) {
return {
return [{
batch: null,
transactions: [],
}
}, HttpCodes.NOT_FOUND]
}

const transactions =
Expand All @@ -584,10 +591,10 @@ export class L1TransportServer extends BaseService<L1TransportServerOptions> {
BigNumber.from(batch.size).toNumber()
)

return {
return [{
batch,
transactions,
}
}, HttpCodes.OK]
}
)

Expand Down Expand Up @@ -936,21 +943,21 @@ export class L1TransportServer extends BaseService<L1TransportServerOptions> {
this._registerRoute(
'get',
'/da/transaction/index/:index',
async (req): Promise<TransactionResponse> => {
async (req): Promise<[TransactionResponse, HttpCodes]> => {
const transactionEntry = await this.state.db.getDaTransactionByIndex(
BigNumber.from(req.params.index).toNumber()
)

if (transactionEntry === null) {
return {
return [{
transaction: null,
batch: null,
}
}, HttpCodes.NOT_FOUND]
}
return {
return [{
transaction: transactionEntry,
batch: null,
}
}, HttpCodes.OK]
}
)

Expand Down
Loading

0 comments on commit c6ef99c

Please sign in to comment.