Skip to content
Open
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
22 changes: 15 additions & 7 deletions apps/ensrainbow/src/lib/api.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import packageJson from "@/../package.json";
import type { EnsRainbow } from "@ensnode/ensrainbow-sdk";
import { StatusCode } from "@ensnode/ensrainbow-sdk";
import { Hono } from "hono";
import type { Context as HonoContext } from "hono";
import { cors } from "hono/cors";

import packageJson from "@/../package.json";
import { ENSRainbowDB, SCHEMA_VERSION } from "@/lib/database";
import { ENSRainbowServer } from "@/lib/server";
import { logger } from "@/utils/logger";
import type { ContentfulStatusCode } from "hono/utils/http-status";
import { logger } from "../utils/logger";
import { ENSRainbowDB, SCHEMA_VERSION } from "./database";
import { ENSRainbowServer } from "./server";

/**
* Creates and configures an ENS Rainbow api
Expand All @@ -32,7 +32,11 @@ export async function createApi(db: ENSRainbowDB): Promise<Hono> {
logger.debug(`Healing request for labelhash: ${labelhash}`);
const result = await server.heal(labelhash);
logger.debug(`Heal result:`, result);
return c.json(result, result.errorCode);

// Map error codes > 1000 to 500, otherwise use the original code
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it ok?

const statusCode = result.errorCode && result.errorCode >= 1000 ? 500 : result.errorCode;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can result.errorCode >= 100 be ever true? We haven't changed anything int the server code, so it won't know about new error codes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are new error codes defined:

  // Network error codes
  TIMEOUT: 1000,
  NETWORK_OFFLINE: 1001,
  GENERAL_NETWORK_ERROR: 1099,


return c.json(result, statusCode as ContentfulStatusCode);
});

api.get("/health", (c: HonoContext) => {
Expand All @@ -45,7 +49,11 @@ export async function createApi(db: ENSRainbowDB): Promise<Hono> {
logger.debug("Label count request");
const result = await server.labelCount();
logger.debug(`Count result:`, result);
return c.json(result, result.errorCode);

// Map error codes > 1000 to 500, otherwise use the original code
const statusCode = result.errorCode && result.errorCode >= 1000 ? 500 : result.errorCode;

return c.json(result, statusCode as ContentfulStatusCode);
});

api.get("/v1/version", (c: HonoContext) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/ensrainbow-sdk/src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe("EnsRainbowApiClient", () => {
expect(client.getOptions()).toEqual({
endpointUrl: new URL(DEFAULT_ENSRAINBOW_URL),
cacheCapacity: EnsRainbowApiClient.DEFAULT_CACHE_CAPACITY,
requestTimeout: EnsRainbowApiClient.DEFAULT_REQUEST_TIMEOUT,
} satisfies EnsRainbowApiClientOptions);
});

Expand All @@ -32,6 +33,7 @@ describe("EnsRainbowApiClient", () => {
expect(client.getOptions()).toEqual({
endpointUrl: customEndpointUrl,
cacheCapacity: 0,
requestTimeout: EnsRainbowApiClient.DEFAULT_REQUEST_TIMEOUT,
} satisfies EnsRainbowApiClientOptions);
});

Expand Down
Loading