Skip to content

Commit

Permalink
chore: replace err-code with CodeError
Browse files Browse the repository at this point in the history
Replaces [err-code](https://github.com/IndigoUnited/js-err-code/blob/master/index.js) with [CodeError](libp2p/js-libp2p-interfaces#314)

Related: #1269

Changes

- removes err-code from dependencies
- adds @libp2p/[email protected] to dependencies
- uses CodeError in place of err-code
  • Loading branch information
tabcat committed Jan 10, 2023
1 parent 367f912 commit bc9f8f0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 64 deletions.
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

0 comments on commit bc9f8f0

Please sign in to comment.