Skip to content

Commit

Permalink
http codes
Browse files Browse the repository at this point in the history
  • Loading branch information
0xSulpiride committed Sep 12, 2023
1 parent cd1b239 commit 47076d9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
9 changes: 5 additions & 4 deletions packages/api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import logger from "./logger";
import {
BundlerRPCMethods,
CustomRPCMethods,
HttpStatus,
RedirectedRPCMethods,
} from "./constants";
import { EthAPI, DebugAPI, Web3API, RedirectAPI } from "./modules";
Expand Down Expand Up @@ -139,9 +140,9 @@ export class ApiApp {
if (this.redirectRpc && method in RedirectedRPCMethods) {
const body = await redirectApi.redirect(method, params);
if (body.error) {
return res.status(200).send({ ...body, id });
return res.status(HttpStatus.OK).send({ ...body, id });
}
return res.status(200).send({ jsonrpc, id, ...body });
return res.status(HttpStatus.OK).send({ jsonrpc, id, ...body });
}

if (result === undefined) {
Expand Down Expand Up @@ -200,7 +201,7 @@ export class ApiApp {
case CustomRPCMethods.skandha_config:
result = await skandhaApi.getConfig();
// skip hexlify for this particular rpc
return res.status(200).send({
return res.status(HttpStatus.OK).send({
jsonrpc,
id,
result,
Expand All @@ -214,7 +215,7 @@ export class ApiApp {
}

result = deepHexlify(result);
return res.status(200).send({
return res.status(HttpStatus.OK).send({
jsonrpc,
id,
result,
Expand Down
51 changes: 51 additions & 0 deletions packages/api/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,54 @@ export const RedirectedRPCMethods = {
eth_maxPriorityFeePerGas: "eth_maxPriorityFeePerGas",
eth_sendRawTransaction: "eth_sendRawTransaction",
};

export enum HttpStatus {
CONTINUE = 100,
SWITCHING_PROTOCOLS = 101,
PROCESSING = 102,
EARLYHINTS = 103,
OK = 200,
CREATED = 201,
ACCEPTED = 202,
NON_AUTHORITATIVE_INFORMATION = 203,
NO_CONTENT = 204,
RESET_CONTENT = 205,
PARTIAL_CONTENT = 206,
AMBIGUOUS = 300,
MOVED_PERMANENTLY = 301,
FOUND = 302,
SEE_OTHER = 303,
NOT_MODIFIED = 304,
TEMPORARY_REDIRECT = 307,
PERMANENT_REDIRECT = 308,
BAD_REQUEST = 400,
UNAUTHORIZED = 401,
PAYMENT_REQUIRED = 402,
FORBIDDEN = 403,
NOT_FOUND = 404,
METHOD_NOT_ALLOWED = 405,
NOT_ACCEPTABLE = 406,
PROXY_AUTHENTICATION_REQUIRED = 407,
REQUEST_TIMEOUT = 408,
CONFLICT = 409,
GONE = 410,
LENGTH_REQUIRED = 411,
PRECONDITION_FAILED = 412,
PAYLOAD_TOO_LARGE = 413,
URI_TOO_LONG = 414,
UNSUPPORTED_MEDIA_TYPE = 415,
REQUESTED_RANGE_NOT_SATISFIABLE = 416,
EXPECTATION_FAILED = 417,
I_AM_A_TEAPOT = 418,
MISDIRECTED = 421,
UNPROCESSABLE_ENTITY = 422,
FAILED_DEPENDENCY = 424,
PRECONDITION_REQUIRED = 428,
TOO_MANY_REQUESTS = 429,
INTERNAL_SERVER_ERROR = 500,
NOT_IMPLEMENTED = 501,
BAD_GATEWAY = 502,
SERVICE_UNAVAILABLE = 503,
GATEWAY_TIMEOUT = 504,
HTTP_VERSION_NOT_SUPPORTED = 505,
}
11 changes: 7 additions & 4 deletions packages/api/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import cors from "@fastify/cors";
import RpcError from "types/lib/api/errors/rpc-error";
import { ServerConfig } from "types/lib/api/interfaces";
import logger from "./logger";
import { HttpStatus } from "./constants";

export class Server {
constructor(private app: FastifyInstance, private config: ServerConfig) {
Expand Down Expand Up @@ -70,16 +71,18 @@ export class Server {
data: err.data,
code: err.code,
};
return res.status(200).send({
return res.status(HttpStatus.OK).send({
jsonrpc: body.jsonrpc,
id: body.id,
error,
});
}

return res.status(err.statusCode ?? 500).send({
error: "Unexpected behaviour",
});
return res
.status(err.statusCode ?? HttpStatus.INTERNAL_SERVER_ERROR)
.send({
error: "Unexpected behaviour",
});
});

await this.app.listen({
Expand Down

0 comments on commit 47076d9

Please sign in to comment.