Skip to content

Commit b7bdecc

Browse files
committed
feat: add light push v3 status codes and helpers
1 parent ed389cc commit b7bdecc

2 files changed

Lines changed: 80 additions & 1 deletion

File tree

packages/interfaces/src/light_push.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ProtocolError } from "./protocols.js";
12
import type { ISender, ISendOptions } from "./sender.js";
23

34
export type LightPushProtocolOptions = ISendOptions & {
@@ -20,3 +21,81 @@ export type ILightPush = ISender & {
2021
start: () => void;
2122
stop: () => void;
2223
};
24+
25+
export enum LightPushStatusCode {
26+
SUCCESS = 200,
27+
BAD_REQUEST = 400,
28+
PAYLOAD_TOO_LARGE = 413,
29+
INVALID_MESSAGE = 420,
30+
UNSUPPORTED_TOPIC = 421,
31+
TOO_MANY_REQUESTS = 429,
32+
INTERNAL_ERROR = 500,
33+
UNAVAILABLE = 503,
34+
NO_RLN_PROOF = 504,
35+
NO_PEERS = 505
36+
}
37+
38+
export const StatusDescriptions: Record<LightPushStatusCode, string> = {
39+
[LightPushStatusCode.SUCCESS]: "Message sent successfully",
40+
[LightPushStatusCode.BAD_REQUEST]: "Bad request format",
41+
[LightPushStatusCode.PAYLOAD_TOO_LARGE]:
42+
"Message payload exceeds maximum size",
43+
[LightPushStatusCode.INVALID_MESSAGE]: "Message validation failed",
44+
[LightPushStatusCode.UNSUPPORTED_TOPIC]: "Unsupported pubsub topic",
45+
[LightPushStatusCode.TOO_MANY_REQUESTS]: "Rate limit exceeded",
46+
[LightPushStatusCode.INTERNAL_ERROR]: "Internal server error",
47+
[LightPushStatusCode.UNAVAILABLE]: "Service temporarily unavailable",
48+
[LightPushStatusCode.NO_RLN_PROOF]: "RLN proof generation failed",
49+
[LightPushStatusCode.NO_PEERS]: "No relay peers available"
50+
};
51+
52+
export function isSuccess(statusCode: number | undefined): boolean {
53+
return statusCode === LightPushStatusCode.SUCCESS;
54+
}
55+
56+
export function toProtocolError(
57+
statusCode: LightPushStatusCode | number | undefined
58+
): ProtocolError {
59+
if (!statusCode) {
60+
return ProtocolError.GENERIC_FAIL;
61+
}
62+
63+
switch (statusCode) {
64+
case LightPushStatusCode.SUCCESS:
65+
return ProtocolError.GENERIC_FAIL;
66+
case LightPushStatusCode.BAD_REQUEST:
67+
case LightPushStatusCode.INVALID_MESSAGE:
68+
case LightPushStatusCode.TOO_MANY_REQUESTS:
69+
return ProtocolError.REMOTE_PEER_REJECTED;
70+
case LightPushStatusCode.PAYLOAD_TOO_LARGE:
71+
return ProtocolError.SIZE_TOO_BIG;
72+
case LightPushStatusCode.UNSUPPORTED_TOPIC:
73+
return ProtocolError.TOPIC_NOT_CONFIGURED;
74+
case LightPushStatusCode.UNAVAILABLE:
75+
case LightPushStatusCode.NO_PEERS:
76+
return ProtocolError.NO_PEER_AVAILABLE;
77+
case LightPushStatusCode.NO_RLN_PROOF:
78+
return ProtocolError.RLN_PROOF_GENERATION;
79+
case LightPushStatusCode.INTERNAL_ERROR:
80+
default:
81+
return ProtocolError.GENERIC_FAIL;
82+
}
83+
}
84+
85+
export function getStatusDescription(
86+
statusCode: number | undefined,
87+
customDesc?: string
88+
): string {
89+
if (customDesc) {
90+
return customDesc;
91+
}
92+
93+
if (!statusCode) {
94+
return "Unknown error";
95+
}
96+
97+
return (
98+
StatusDescriptions[statusCode as LightPushStatusCode] ||
99+
`Unknown status code: ${statusCode}`
100+
);
101+
}

packages/proto/src/lib/light_push.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ message LightPushResponseV3 {
3939
uint32 status_code = 10;
4040
optional string status_desc = 11;
4141
optional uint32 relay_peer_count = 12;
42-
}
42+
}

0 commit comments

Comments
 (0)