Skip to content

Commit

Permalink
fix(BatchReader): test response object in getDirectoryFileHashes (#697)
Browse files Browse the repository at this point in the history
fix(BatchReader): eliminate mongodb error for normal behavior

do not attempt to store an empty value to mongodb if the poetAnchorBlock is not yet available.

Fixes: #650 
References: #697
  • Loading branch information
krobi64 authored Oct 31, 2018
1 parent 8daef90 commit ee58df1
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/BatchReader/IPFS.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { inject, injectable } from 'inversify'
import fetch from 'node-fetch'

import { isNotNil } from 'Helpers/isNotNil'

enum Type {
File = 'File',
Directory = 'Directory',
Expand Down Expand Up @@ -48,7 +50,11 @@ export class IPFS {

getDirectoryFileHashes: getDirectoryFileHashes = async (hash: string) => {
const response = await this.ls(hash)
return response.Objects[hash].Links.map(x => x.Hash)
const validResponse = isNotNil(response) &&
isNotNil(response.Objects) &&
isNotNil(response.Objects[hash]) &&
isNotNil(response.Objects[hash].Links)
return validResponse ? response.Objects[hash].Links.map(x => x.Hash) : []
}

ls: ls = async (hash: string) => {
Expand Down
2 changes: 2 additions & 0 deletions src/BatchReader/Router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { inject, injectable } from 'inversify'
import * as Pino from 'pino'
import { isNil } from 'ramda'

import { childWithFileName } from 'Helpers/Logging'
import { BlockDownloaded } from 'Messaging/Messages'
Expand Down Expand Up @@ -55,6 +56,7 @@ export class Router {
)

try {
if (isNil(poetBlockAnchors) || poetBlockAnchors.length === 0) return
await this.claimController.addEntries(poetBlockAnchors)
} catch (error) {
logger.error({ error, poetBlockAnchors }, 'Failed to store directory hashes to DB collection')
Expand Down
51 changes: 51 additions & 0 deletions src/Helpers/isNotNil.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe } from 'riteway'

import { isNotNil } from './isNotNil'

describe('isNotNil', async assert => {

const returnTrue = 'return true'
const returnFalse = 'return false'

assert({
given: 'an empty string',
should: returnTrue,
actual: isNotNil(''),
expected: true,
})

assert({
given: 'an empty array',
should: returnTrue,
actual: isNotNil([]),
expected: true,
})

assert({
given: 'an empty object',
should: returnTrue,
actual: isNotNil({}),
expected: true,
})

assert({
given: 'undefined',
should: returnFalse,
actual: isNotNil(undefined),
expected: false,
})

assert({
given: 'null',
should: returnFalse,
actual: isNotNil(null),
expected: false,
})

assert({
given: 'no value',
should: returnFalse,
actual: isNotNil(),
expected: false,
})
})
6 changes: 6 additions & 0 deletions src/Helpers/isNotNil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { isNil, not, pipe } from 'ramda'

export const isNotNil = pipe(
isNil,
not,
)
1 change: 1 addition & 0 deletions tests/unit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import '../../src/Helpers/Logging.test.ts'
import '../../src/Helpers/Time.test.ts'
import '../../src/Helpers/asyncPipe.test.ts'
import '../../src/Helpers/camelCaseToScreamingSnakeCase.test'
import '../../src/Helpers/isNotNil.test'
import '../../src/Helpers/isTruthy.test'
import '../../src/Helpers/to-promise.test.ts'
import '../../src/Interfaces.test.ts'
Expand Down

0 comments on commit ee58df1

Please sign in to comment.