Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: replace err-code with CodeError #172

Merged
merged 1 commit into from
Jan 13, 2023
Merged
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
2 changes: 1 addition & 1 deletion packages/libp2p-daemon-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@
"@libp2p/interface-peer-id": "^2.0.0",
"@libp2p/interface-peer-info": "^1.0.1",
"@libp2p/interface-transport": "^2.0.0",
"@libp2p/interfaces": "^3.2.0",
"@libp2p/logger": "^2.0.0",
"@libp2p/peer-id": "^2.0.0",
"@libp2p/tcp": "^6.0.8",
"@multiformats/multiaddr": "^11.0.0",
"err-code": "^3.0.1",
"it-stream-types": "^1.0.4",
"multiformats": "^11.0.0",
"uint8arraylist": "^2.3.2"
Expand Down
60 changes: 30 additions & 30 deletions packages/libp2p-daemon-client/src/dht.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CID } from 'multiformats/cid'
import { multiaddr } from '@multiformats/multiaddr'
import errcode from 'err-code'
import { CodeError } from '@libp2p/interfaces/errors'
import {
Request,
Response,
Expand All @@ -24,11 +24,11 @@ export class DHT {
*/
async put (key: Uint8Array, value: Uint8Array): Promise<void> {
if (!(key instanceof Uint8Array)) {
throw errcode(new Error('invalid key received'), 'ERR_INVALID_KEY')
throw new CodeError('invalid key received', 'ERR_INVALID_KEY')
}

if (!(value instanceof Uint8Array)) {
throw errcode(new Error('value received is not a Uint8Array'), 'ERR_INVALID_VALUE')
throw new CodeError('value received is not a Uint8Array', 'ERR_INVALID_VALUE')
}

const sh = await this.client.send({
Expand All @@ -43,15 +43,15 @@ export class DHT {
const message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
throw new CodeError('Empty response from remote', 'ERR_EMPTY_RESPONSE')
}

const response = Response.decode(message)

await sh.close()

if (response.type !== Response.Type.OK) {
throw errcode(new Error(response.error?.msg ?? 'DHT put failed'), 'ERR_DHT_PUT_FAILED')
throw new CodeError(response.error?.msg ?? 'DHT put failed', 'ERR_DHT_PUT_FAILED')
}
}

Expand All @@ -60,7 +60,7 @@ export class DHT {
*/
async get (key: Uint8Array): Promise<Uint8Array> {
if (!(key instanceof Uint8Array)) {
throw errcode(new Error('invalid key received'), 'ERR_INVALID_KEY')
throw new CodeError('invalid key received', 'ERR_INVALID_KEY')
}

const sh = await this.client.send({
Expand All @@ -74,19 +74,19 @@ export class DHT {
const message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
throw new CodeError('Empty response from remote', 'ERR_EMPTY_RESPONSE')
}

const response = Response.decode(message)

await sh.close()

if (response.type !== Response.Type.OK) {
throw errcode(new Error(response.error?.msg ?? 'DHT get failed'), 'ERR_DHT_GET_FAILED')
throw new CodeError(response.error?.msg ?? 'DHT get failed', 'ERR_DHT_GET_FAILED')
}

if (response.dht == null || response.dht.value == null) {
throw errcode(new Error('Invalid DHT get response'), 'ERR_DHT_GET_FAILED')
throw new CodeError('Invalid DHT get response', 'ERR_DHT_GET_FAILED')
}

return response.dht.value
Expand All @@ -97,7 +97,7 @@ export class DHT {
*/
async findPeer (peerId: PeerId): Promise<PeerInfo> {
if (!isPeerId(peerId)) {
throw errcode(new Error('invalid peer id received'), 'ERR_INVALID_PEER_ID')
throw new CodeError('invalid peer id received', 'ERR_INVALID_PEER_ID')
}

const sh = await this.client.send({
Expand All @@ -111,19 +111,19 @@ export class DHT {
const message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
throw new CodeError('Empty response from remote', 'ERR_EMPTY_RESPONSE')
}

const response = Response.decode(message)

await sh.close()

if (response.type !== Response.Type.OK) {
throw errcode(new Error(response.error?.msg ?? 'DHT find peer failed'), 'ERR_DHT_FIND_PEER_FAILED')
throw new CodeError(response.error?.msg ?? 'DHT find peer failed', 'ERR_DHT_FIND_PEER_FAILED')
}

if (response.dht == null || response.dht.peer == null || response.dht.peer.addrs == null) {
throw errcode(new Error('Invalid response'), 'ERR_DHT_FIND_PEER_FAILED')
throw new CodeError('Invalid response', 'ERR_DHT_FIND_PEER_FAILED')
}

return {
Expand All @@ -138,7 +138,7 @@ export class DHT {
*/
async provide (cid: CID) {
if (cid == null || CID.asCID(cid) == null) {
throw errcode(new Error('invalid cid received'), 'ERR_INVALID_CID')
throw new CodeError('invalid cid received', 'ERR_INVALID_CID')
}

const sh = await this.client.send({
Expand All @@ -152,15 +152,15 @@ export class DHT {
const message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
throw new CodeError('Empty response from remote', 'ERR_EMPTY_RESPONSE')
}

const response = Response.decode(message)

await sh.close()

if (response.type !== Response.Type.OK) {
throw errcode(new Error(response.error?.msg ?? 'DHT provide failed'), 'ERR_DHT_PROVIDE_FAILED')
throw new CodeError(response.error?.msg ?? 'DHT provide failed', 'ERR_DHT_PROVIDE_FAILED')
}
}

Expand All @@ -169,7 +169,7 @@ export class DHT {
*/
async * findProviders (cid: CID, count: number = 1): AsyncIterable<PeerInfo> {
if (cid == null || CID.asCID(cid) == null) {
throw errcode(new Error('invalid cid received'), 'ERR_INVALID_CID')
throw new CodeError('invalid cid received', 'ERR_INVALID_CID')
}

const sh = await this.client.send({
Expand All @@ -184,22 +184,22 @@ export class DHT {
let message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
throw new CodeError('Empty response from remote', 'ERR_EMPTY_RESPONSE')
}

// stream begin message
const response = Response.decode(message)

if (response.type !== Response.Type.OK) {
await sh.close()
throw errcode(new Error(response.error?.msg ?? 'DHT find providers failed'), 'ERR_DHT_FIND_PROVIDERS_FAILED')
throw new CodeError(response.error?.msg ?? 'DHT find providers failed', 'ERR_DHT_FIND_PROVIDERS_FAILED')
}

while (true) {
message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
throw new CodeError('Empty response from remote', 'ERR_EMPTY_RESPONSE')
}

const response = DHTResponse.decode(message)
Expand All @@ -220,7 +220,7 @@ export class DHT {
} else {
// Unexpected message received
await sh.close()
throw errcode(new Error('unexpected message received'), 'ERR_UNEXPECTED_MESSAGE_RECEIVED')
throw new CodeError('unexpected message received', 'ERR_UNEXPECTED_MESSAGE_RECEIVED')
}
}
}
Expand All @@ -230,7 +230,7 @@ export class DHT {
*/
async * getClosestPeers (key: Uint8Array): AsyncIterable<PeerInfo> {
if (!(key instanceof Uint8Array)) {
throw errcode(new Error('invalid key received'), 'ERR_INVALID_KEY')
throw new CodeError('invalid key received', 'ERR_INVALID_KEY')
}

const sh = await this.client.send({
Expand All @@ -245,21 +245,21 @@ export class DHT {
let message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
throw new CodeError('Empty response from remote', 'ERR_EMPTY_RESPONSE')
}

const response = Response.decode(message)

if (response.type !== Response.Type.OK) {
await sh.close()
throw errcode(new Error(response.error?.msg ?? 'DHT find providers failed'), 'ERR_DHT_FIND_PROVIDERS_FAILED')
throw new CodeError(response.error?.msg ?? 'DHT find providers failed', 'ERR_DHT_FIND_PROVIDERS_FAILED')
}

while (true) {
message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
throw new CodeError('Empty response from remote', 'ERR_EMPTY_RESPONSE')
}

const response = DHTResponse.decode(message)
Expand All @@ -282,7 +282,7 @@ export class DHT {
} else {
// Unexpected message received
await sh.close()
throw errcode(new Error('unexpected message received'), 'ERR_UNEXPECTED_MESSAGE_RECEIVED')
throw new CodeError('unexpected message received', 'ERR_UNEXPECTED_MESSAGE_RECEIVED')
}
}
}
Expand All @@ -292,7 +292,7 @@ export class DHT {
*/
async getPublicKey (peerId: PeerId) {
if (!isPeerId(peerId)) {
throw errcode(new Error('invalid peer id received'), 'ERR_INVALID_PEER_ID')
throw new CodeError('invalid peer id received', 'ERR_INVALID_PEER_ID')
}

const sh = await this.client.send({
Expand All @@ -306,19 +306,19 @@ export class DHT {
const message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
throw new CodeError('Empty response from remote', 'ERR_EMPTY_RESPONSE')
}

const response = Response.decode(message)

await sh.close()

if (response.type !== Response.Type.OK) {
throw errcode(new Error(response.error?.msg ?? 'DHT get public key failed'), 'ERR_DHT_GET_PUBLIC_KEY_FAILED')
throw new CodeError(response.error?.msg ?? 'DHT get public key failed', 'ERR_DHT_GET_PUBLIC_KEY_FAILED')
}

if (response.dht == null) {
throw errcode(new Error('Invalid response'), 'ERR_DHT_GET_PUBLIC_KEY_FAILED')
throw new CodeError('Invalid response', 'ERR_DHT_GET_PUBLIC_KEY_FAILED')
}

return response.dht.value
Expand Down
Loading