Skip to content

Commit

Permalink
feat(lightpush)!: return new error messages (#2115)
Browse files Browse the repository at this point in the history
* feat: return new error messages

* fix test, remove unused

* up

* up

* rollback

* up test
  • Loading branch information
weboko committed Aug 29, 2024
1 parent eadb85a commit a022433
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 8 deletions.
8 changes: 4 additions & 4 deletions packages/core/src/lib/filter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
return {
success: null,
failure: {
error: ProtocolError.REMOTE_PEER_FAULT,
error: ProtocolError.NO_STREAM_AVAILABLE,
peerId: peer.id
}
};
Expand Down Expand Up @@ -170,7 +170,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
if (!res || !res.length) {
return {
failure: {
error: ProtocolError.REMOTE_PEER_FAULT,
error: ProtocolError.NO_RESPONSE,
peerId: peer.id
},
success: null
Expand Down Expand Up @@ -211,7 +211,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
return {
success: null,
failure: {
error: ProtocolError.REMOTE_PEER_FAULT,
error: ProtocolError.NO_STREAM_AVAILABLE,
peerId: peer.id
}
};
Expand Down Expand Up @@ -243,7 +243,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
return {
success: null,
failure: {
error: ProtocolError.REMOTE_PEER_FAULT,
error: ProtocolError.NO_RESPONSE,
peerId: peer.id
}
};
Expand Down
15 changes: 14 additions & 1 deletion packages/core/src/lib/light_push/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Uint8ArrayList } from "uint8arraylist";
import { BaseProtocol } from "../base_protocol.js";

import { PushRpc } from "./push_rpc.js";
import { isRLNResponseError, matchRLNErrorMessage } from "./utils.js";

const log = new Logger("light-push");

Expand Down Expand Up @@ -153,7 +154,19 @@ export class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
return {
success: null,
failure: {
error: ProtocolError.REMOTE_PEER_FAULT,
error: ProtocolError.NO_RESPONSE,
peerId: peer.id
}
};
}

if (isRLNResponseError(response.info)) {
const rlnErrorCase = matchRLNErrorMessage(response.info!);
log.error("Remote peer rejected the message: ", rlnErrorCase);
return {
success: null,
failure: {
error: rlnErrorCase,
peerId: peer.id
}
};
Expand Down
31 changes: 31 additions & 0 deletions packages/core/src/lib/light_push/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ProtocolError } from "@waku/interfaces";

// should match nwaku
// https://github.com/waku-org/nwaku/blob/c3cb06ac6c03f0f382d3941ea53b330f6a8dd127/waku/waku_rln_relay/rln_relay.nim#L309
// https://github.com/waku-org/nwaku/blob/c3cb06ac6c03f0f382d3941ea53b330f6a8dd127/tests/waku_rln_relay/rln/waku_rln_relay_utils.nim#L20
const RLN_GENERATION_PREFIX_ERROR = "could not generate rln-v2 proof";

export const isRLNResponseError = (info?: string): boolean => {
if (!info) {
return false;
}

return info.includes(RLN_GENERATION_PREFIX_ERROR);
};

export const matchRLNErrorMessage = (info: string): ProtocolError => {
const rlnErrorMap: { [key: string]: ProtocolError } = {
[ProtocolError.RLN_IDENTITY_MISSING]: ProtocolError.RLN_IDENTITY_MISSING,
[ProtocolError.RLN_MEMBERSHIP_INDEX]: ProtocolError.RLN_MEMBERSHIP_INDEX,
[ProtocolError.RLN_LIMIT_MISSING]: ProtocolError.RLN_LIMIT_MISSING
};

const infoLowerCase = info.toLowerCase();
for (const errorKey in rlnErrorMap) {
if (infoLowerCase.includes(errorKey.toLowerCase())) {
return rlnErrorMap[errorKey];
}
}

return ProtocolError.RLN_PROOF_GENERATION;
};
24 changes: 22 additions & 2 deletions packages/interfaces/src/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export enum ProtocolError {
* The remote peer did not behave as expected. Mitigation for `NO_PEER_AVAILABLE`
* or `DECODE_FAILED` can be used.
*/
REMOTE_PEER_FAULT = "Remote peer fault",
NO_RESPONSE = "No response received",
/**
* The remote peer rejected the message. Information provided by the remote peer
* is logged. Review message validity, or mitigation for `NO_PEER_AVAILABLE`
Expand All @@ -190,7 +190,27 @@ export enum ProtocolError {
* The protocol request timed out without a response. This may be due to a connection issue.
* Mitigation can be: retrying after a given time period
*/
REQUEST_TIMEOUT = "Request timeout"
REQUEST_TIMEOUT = "Request timeout",
/**
* Missing credentials info message.
* nwaku: https://github.com/waku-org/nwaku/blob/c3cb06ac6c03f0f382d3941ea53b330f6a8dd127/waku/waku_rln_relay/group_manager/group_manager_base.nim#L186
*/
RLN_IDENTITY_MISSING = "Identity credentials are not set",
/**
* Membership index missing info message.
* nwaku: https://github.com/waku-org/nwaku/blob/c3cb06ac6c03f0f382d3941ea53b330f6a8dd127/waku/waku_rln_relay/group_manager/group_manager_base.nim#L188
*/
RLN_MEMBERSHIP_INDEX = "Membership index is not set",
/**
* Message limit is missing.
* nwaku: https://github.com/waku-org/nwaku/blob/c3cb06ac6c03f0f382d3941ea53b330f6a8dd127/waku/waku_rln_relay/group_manager/group_manager_base.nim#L190
*/
RLN_LIMIT_MISSING = "User message limit is not set",
/**
* General proof generation error message.
* nwaku: https://github.com/waku-org/nwaku/blob/c3cb06ac6c03f0f382d3941ea53b330f6a8dd127/waku/waku_rln_relay/group_manager/group_manager_base.nim#L201C19-L201C42
*/
RLN_PROOF_GENERATION = "Proof generation failed"
}

export interface Failure {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/protocols/light_push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class LightPushSDK extends BaseProtocolSDK implements ILightPushSDK {
failures.push(failure);
}
} else {
log.error("Failed to send message to peer", result.reason);
log.error("Failed unexpectedly while sending:", result.reason);
failures.push({ error: ProtocolError.GENERIC_FAIL });
}
}
Expand Down

0 comments on commit a022433

Please sign in to comment.