Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/indexer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
NotFoundError,
requestMiddleware,
shutdown,
TIMEOUT_ERROR,
} from "@intmax2-function/shared";
import { Hono } from "hono";
import { prettyJSON } from "hono/pretty-json";
Expand All @@ -26,7 +27,7 @@ const app = new Hono();
app.use(corsMiddleware);
app.use(secureHeaders());
app.use(limiter);
app.use(timeout(APP_TIMEOUT));
app.use(timeout(APP_TIMEOUT, TIMEOUT_ERROR));
app.use(requestMiddleware);

app.use(prettyJSON());
Expand Down
2 changes: 1 addition & 1 deletion packages/indexer/src/services/indexer.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FIRESTORE_DOCUMENTS, getIndexer } from "@intmax2-function/shared";
import { BUILDER_SELECTION_MODE } from "../constants";
import { getBuildersByMode } from "./../lib/builder";
import { getBuildersByMode } from "../lib/builder";

export const listBlockBuilderNodes = async () => {
const indexerInstance = getIndexer(FIRESTORE_DOCUMENTS.BUILDERS);
Expand Down
3 changes: 2 additions & 1 deletion packages/predicate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
NotFoundError,
requestMiddleware,
shutdown,
TIMEOUT_ERROR,
} from "@intmax2-function/shared";
import { Hono } from "hono";
import { prettyJSON } from "hono/pretty-json";
Expand All @@ -25,7 +26,7 @@ const app = new Hono();
app.use(corsMiddleware);
app.use(secureHeaders());
app.use(limiter);
app.use(timeout(APP_TIMEOUT));
app.use(timeout(APP_TIMEOUT, TIMEOUT_ERROR));
app.use(requestMiddleware);

app.use(prettyJSON());
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/blockchain/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createWalletClient, fallback } from "viem";
import { mnemonicToAccount, privateKeyToAccount } from "viem/accounts";
import { config } from "../config";
import type { NetworkLayer } from "./../types";
import type { NetworkLayer } from "../types";
import { getClientConfig } from "./client";

type MockWalletType = "mockMessenger";
Expand Down
51 changes: 47 additions & 4 deletions packages/shared/src/lib/error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Context } from "hono";
import { HTTPException } from "hono/http-exception";
import { ContentfulStatusCode } from "hono/utils/http-status";
import { ZodError } from "zod";
import { isProduction } from "../config";
Expand All @@ -11,6 +12,45 @@ export enum ErrorCode {
VALIDATION_ERROR = "VALIDATION_ERROR",
}

interface HasStatus {
status: number;
}

const hasStatus = (err: unknown): err is HasStatus => {
return typeof err === "object" && err !== null && "status" in err;
};

const mapError = (status: ContentfulStatusCode, _: unknown) => {
if (status === 504) {
return {
code: "GATEWAY_TIMEOUT",
message: "Gateway Timeout",
};
}
if (status >= 400 && status < 500) {
return {
code: "BAD_REQUEST",
message: "Bad Request",
};
}

return {
code: "INTERNAL_SERVER_ERROR",
message: "Internal Server Error",
};
};

const getStatus = (err: unknown): number => {
if (hasStatus(err)) {
return err.status;
}
return 500;
};

export const TIMEOUT_ERROR = new HTTPException(504, {
message: "Request timed out",
});

export class AppError extends Error {
constructor(
public statusCode: number,
Expand Down Expand Up @@ -45,7 +85,11 @@ export class InternalServerError extends AppError {
}
}

const IGNORE_ERROR_MESSAGES = ["URI malformed"];
const IGNORE_ERROR_MESSAGES = [
"URI malformed",
"Request timed out",
"Response.clone: Body has already been consumed.",
];

export const handleError = (err: unknown, c: Context): Response => {
if (err instanceof ZodError) {
Expand Down Expand Up @@ -73,9 +117,8 @@ export const handleError = (err: unknown, c: Context): Response => {
return c.json({ code: err.code, message: err.message }, err.statusCode as ContentfulStatusCode);
}

const statusCode = err instanceof Error ? 500 : 400;
const code = err instanceof Error ? "INTERNAL_SERVER_ERROR" : "BAD_REQUEST";
const message = err instanceof Error ? "Internal Server Error" : "Bad Request";
const statusCode = getStatus(err) as ContentfulStatusCode;
const { code, message } = mapError(statusCode, err);

return c.json(
{
Expand Down
12 changes: 1 addition & 11 deletions packages/shared/src/middlewares/cache.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import type { Context, Next } from "hono";
import { cache } from "../lib";

export type CachedResponse = {
body: string;
headers: Record<string, string>;
status: number;
};

type CacheOptions = {
expire: number;
key?: string;
};
import type { CachedResponse, CacheOptions } from "../types";

const validateKeys = ["perPage", "cursor", "tokenIndexes", "contractAddresses"];

Expand Down
11 changes: 11 additions & 0 deletions packages/shared/src/types/txMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,14 @@ export type TxMapData = {
data: string;
expiresAt: Date;
};

export interface CachedResponse {
body: string;
headers: Record<string, string>;
status: number;
}

export interface CacheOptions {
expire: number;
key?: string;
}
3 changes: 2 additions & 1 deletion packages/token/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
NotFoundError,
requestMiddleware,
shutdown,
TIMEOUT_ERROR,
} from "@intmax2-function/shared";
import { Hono } from "hono";
import { compress } from "hono/compress";
Expand All @@ -31,7 +32,7 @@ bootstrap();
app.use(corsMiddleware);
app.use(secureHeaders());
app.use(limiter);
app.use(timeout(APP_TIMEOUT));
app.use(timeout(APP_TIMEOUT, TIMEOUT_ERROR));
app.use(requestMiddleware);

app.use(compress());
Expand Down
3 changes: 2 additions & 1 deletion packages/tx-map/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
NotFoundError,
requestMiddleware,
shutdown,
TIMEOUT_ERROR,
} from "@intmax2-function/shared";
import { Hono } from "hono";
import { prettyJSON } from "hono/pretty-json";
Expand All @@ -26,7 +27,7 @@ const app = new Hono();
app.use(corsMiddleware);
app.use(secureHeaders());
app.use(limiter);
app.use(timeout(APP_TIMEOUT));
app.use(timeout(APP_TIMEOUT, TIMEOUT_ERROR));
app.use(requestMiddleware);

app.use(prettyJSON());
Expand Down