*Properly handle RPC errors

This commit is contained in:
Derrick Hammer 2022-08-30 22:25:44 -04:00
parent 599a1a1e0b
commit 33f5d36aca
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 17 additions and 8 deletions

View File

@ -13,7 +13,9 @@ import {
ensureUniqueRecords, ensureUniqueRecords,
DNSRecord, DNSRecord,
getTld, getTld,
resolverError,
} from "@lumeweb/libresolver"; } from "@lumeweb/libresolver";
import { RPCResponse } from "@lumeweb/relay-types";
const HIP5_EXTENSIONS = ["eth", "_eth"]; const HIP5_EXTENSIONS = ["eth", "_eth"];
@ -24,6 +26,12 @@ interface HnsRecord {
ns: string; ns: string;
} }
interface HandshakeRPCResponse extends RPCResponse {
data?: {
records: HnsRecord[];
};
}
export default class Handshake extends AbstractResolverModule { export default class Handshake extends AbstractResolverModule {
private async buildBlacklist(): Promise<Set<string>> { private async buildBlacklist(): Promise<Set<string>> {
const blacklist = new Set<string>(); const blacklist = new Set<string>();
@ -66,13 +74,17 @@ export default class Handshake extends AbstractResolverModule {
} }
const chainRecords = await this.query(tld, bypassCache); const chainRecords = await this.query(tld, bypassCache);
if (!chainRecords) { if (chainRecords.error) {
return resolverError(chainRecords.error);
}
if (!chainRecords.data?.records.length) {
return resolverEmptyResponse(); return resolverEmptyResponse();
} }
let records: DNSRecord[] = []; let records: DNSRecord[] = [];
for (const record of chainRecords as HnsRecord[]) { for (const record of chainRecords.data?.records) {
switch (record.type) { switch (record.type) {
case "NS": { case "NS": {
await this.processNs( await this.processNs(
@ -260,17 +272,14 @@ export default class Handshake extends AbstractResolverModule {
private async query( private async query(
tld: string, tld: string,
bypassCache: boolean bypassCache: boolean
): Promise<[] | boolean> { ): Promise<HandshakeRPCResponse> {
const query = this.resolver.rpcNetwork.wisdomQuery( let query = this.resolver.rpcNetwork.wisdomQuery(
"getnameresource", "getnameresource",
"handshake", "handshake",
[tld], [tld],
bypassCache bypassCache
); );
const resp = await query.result; return (await query.result) as HandshakeRPCResponse;
// @ts-ignore
return resp?.records || [];
} }
private async processTxt( private async processTxt(