From 089295e57f726d8ae4aec703215ce245cd712030 Mon Sep 17 00:00:00 2001 From: Gabriel Rocheleau Date: Tue, 30 Aug 2022 09:26:33 -0400 Subject: [PATCH] trie: simplify get method logic --- packages/trie/benchmarks/engines/level.ts | 9 +++++---- packages/trie/recipes/level-legacy.ts | 9 +++++---- packages/trie/recipes/level.ts | 9 +++++---- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/packages/trie/benchmarks/engines/level.ts b/packages/trie/benchmarks/engines/level.ts index 4f5a66a1a0..73623019d6 100644 --- a/packages/trie/benchmarks/engines/level.ts +++ b/packages/trie/benchmarks/engines/level.ts @@ -32,13 +32,14 @@ export class LevelDB implements DB { try { value = await this._leveldb.get(key, ENCODING_OPTS) } catch (error: any) { - if (error.notFound !== undefined) { - // not found, returning null - } else { + // https://github.com/Level/abstract-level/blob/915ad1317694d0ce8c580b5ab85d81e1e78a3137/abstract-level.js#L309 + // This should be `true` if the error came from LevelDB + // so we can check for `NOT true` to identify any non-404 errors + if (error.notFound !== true) { throw error } } - return value as Buffer + return value } /** diff --git a/packages/trie/recipes/level-legacy.ts b/packages/trie/recipes/level-legacy.ts index bcd2d22e24..c06c008e0a 100644 --- a/packages/trie/recipes/level-legacy.ts +++ b/packages/trie/recipes/level-legacy.ts @@ -17,13 +17,14 @@ export class LevelDB implements DB { try { value = await this._leveldb.get(key, ENCODING_OPTS) } catch (error: any) { - if (error.notFound !== undefined) { - // not found, returning null - } else { + // https://github.com/Level/abstract-level/blob/915ad1317694d0ce8c580b5ab85d81e1e78a3137/abstract-level.js#L309 + // This should be `true` if the error came from LevelDB + // so we can check for `NOT true` to identify any non-404 errors + if (error.notFound !== true) { throw error } } - return value as Buffer + return value } async put(key: Buffer, val: Buffer): Promise { diff --git a/packages/trie/recipes/level.ts b/packages/trie/recipes/level.ts index e0b04f7bd5..62ac888cdb 100644 --- a/packages/trie/recipes/level.ts +++ b/packages/trie/recipes/level.ts @@ -19,13 +19,14 @@ export class LevelDB implements DB { try { value = await this._leveldb.get(key, ENCODING_OPTS) } catch (error: any) { - if (error.notFound !== undefined) { - // not found, returning null - } else { + // https://github.com/Level/abstract-level/blob/915ad1317694d0ce8c580b5ab85d81e1e78a3137/abstract-level.js#L309 + // This should be `true` if the error came from LevelDB + // so we can check for `NOT true` to identify any non-404 errors + if (error.notFound !== true) { throw error } } - return value as Buffer + return value } async put(key: Buffer, val: Buffer): Promise {