From a8dd5e32b303c01c74605b90176cf0f6a6593c7d Mon Sep 17 00:00:00 2001 From: Ivan Folgueira Bande Date: Fri, 27 Sep 2024 11:15:41 +0200 Subject: [PATCH 1/2] waku_keystore: give some more context in case of error --- waku/waku_keystore/conversion_utils.nim | 14 ++++++-- waku/waku_keystore/keystore.nim | 46 +++++++++++++++++++------ waku/waku_keystore/utils.nim | 7 +++- 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/waku/waku_keystore/conversion_utils.nim b/waku/waku_keystore/conversion_utils.nim index c19044cbd5..1cb8ba235c 100644 --- a/waku/waku_keystore/conversion_utils.nim +++ b/waku/waku_keystore/conversion_utils.nim @@ -18,6 +18,16 @@ proc decode*(encodedCredential: seq[byte]): KeystoreResult[KeystoreMembership] = let jsonObject = parseJson(string.fromBytes(encodedCredential)) return ok(to(jsonObject, KeystoreMembership)) except JsonParsingError: - return err(AppKeystoreError(kind: KeystoreJsonError, msg: getCurrentExceptionMsg())) + return err( + AppKeystoreError( + kind: KeystoreJsonError, + msg: "error in conversion_utils decode: " & getCurrentExceptionMsg(), + ) + ) except Exception: #parseJson raises Exception - return err(AppKeystoreError(kind: KeystoreOsError, msg: getCurrentExceptionMsg())) + return err( + AppKeystoreError( + kind: KeystoreOsError, + msg: "error in conversion_utils decode: " & getCurrentExceptionMsg(), + ) + ) diff --git a/waku/waku_keystore/keystore.nim b/waku/waku_keystore/keystore.nim index d79844b084..e46128efa2 100644 --- a/waku/waku_keystore/keystore.nim +++ b/waku/waku_keystore/keystore.nim @@ -31,7 +31,12 @@ proc createAppKeystore*( f.write(separator) ok() except CatchableError: - err(AppKeystoreError(kind: KeystoreOsError, msg: getCurrentExceptionMsg())) + err( + AppKeystoreError( + kind: KeystoreOsError, + msg: "error in createAppKeystore: " & getCurrentExceptionMsg(), + ) + ) finally: f.close() @@ -114,19 +119,40 @@ proc loadAppKeystore*( return ok(data) # TODO: we might continue rather than return for some of these errors except JsonParsingError: - return - err(AppKeystoreError(kind: KeystoreJsonError, msg: getCurrentExceptionMsg())) + return err( + AppKeystoreError( + kind: KeystoreJsonError, + msg: "error in loadAppKeystore: " & getCurrentExceptionMsg(), + ) + ) except ValueError: - return - err(AppKeystoreError(kind: KeystoreJsonError, msg: getCurrentExceptionMsg())) + return err( + AppKeystoreError( + kind: KeystoreJsonError, + msg: "error in loadAppKeystore: " & getCurrentExceptionMsg(), + ) + ) except OSError: - return - err(AppKeystoreError(kind: KeystoreOsError, msg: getCurrentExceptionMsg())) + return err( + AppKeystoreError( + kind: KeystoreOsError, + msg: "error in loadAppKeystore: " & getCurrentExceptionMsg(), + ) + ) except Exception: #parseJson raises Exception - return - err(AppKeystoreError(kind: KeystoreOsError, msg: getCurrentExceptionMsg())) + return err( + AppKeystoreError( + kind: KeystoreOsError, + msg: "error in loadAppKeystore: " & getCurrentExceptionMsg(), + ) + ) except IOError: - return err(AppKeystoreError(kind: KeystoreIoError, msg: getCurrentExceptionMsg())) + return err( + AppKeystoreError( + kind: KeystoreIoError, + msg: "error in loadAppKeystore: " & getCurrentExceptionMsg(), + ) + ) return err( AppKeystoreError( diff --git a/waku/waku_keystore/utils.nim b/waku/waku_keystore/utils.nim index 88c10207e2..bbfbf31cb0 100644 --- a/waku/waku_keystore/utils.nim +++ b/waku/waku_keystore/utils.nim @@ -30,7 +30,12 @@ proc save*(json: JsonNode, path: string, separator: string): KeystoreResult[void # We save the updated json var f: File if not f.open(path, fmAppend): - return err(AppKeystoreError(kind: KeystoreOsError, msg: getCurrentExceptionMsg())) + return err( + AppKeystoreError( + kind: KeystoreOsError, + msg: "error in waku_keystore save: " & getCurrentExceptionMsg(), + ) + ) try: # To avoid other users/attackers to be able to read keyfiles, we make the file readable/writable only by the running user setFilePermissions(path, {fpUserWrite, fpUserRead}) From a9d10e6e97969668bbcf2c373344d94852f3b59f Mon Sep 17 00:00:00 2001 From: Ivan Folgueira Bande Date: Wed, 2 Oct 2024 21:19:16 +0200 Subject: [PATCH 2/2] apply Zoltan suggestion for better error description --- waku/waku_keystore/conversion_utils.nim | 2 +- waku/waku_keystore/keystore.nim | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/waku/waku_keystore/conversion_utils.nim b/waku/waku_keystore/conversion_utils.nim index 1cb8ba235c..4a76811114 100644 --- a/waku/waku_keystore/conversion_utils.nim +++ b/waku/waku_keystore/conversion_utils.nim @@ -21,7 +21,7 @@ proc decode*(encodedCredential: seq[byte]): KeystoreResult[KeystoreMembership] = return err( AppKeystoreError( kind: KeystoreJsonError, - msg: "error in conversion_utils decode: " & getCurrentExceptionMsg(), + msg: "error during decoding credentials: " & getCurrentExceptionMsg(), ) ) except Exception: #parseJson raises Exception diff --git a/waku/waku_keystore/keystore.nim b/waku/waku_keystore/keystore.nim index e46128efa2..9741761ff7 100644 --- a/waku/waku_keystore/keystore.nim +++ b/waku/waku_keystore/keystore.nim @@ -34,7 +34,7 @@ proc createAppKeystore*( err( AppKeystoreError( kind: KeystoreOsError, - msg: "error in createAppKeystore: " & getCurrentExceptionMsg(), + msg: "error while writing keystore: " & getCurrentExceptionMsg(), ) ) finally: @@ -122,35 +122,38 @@ proc loadAppKeystore*( return err( AppKeystoreError( kind: KeystoreJsonError, - msg: "error in loadAppKeystore: " & getCurrentExceptionMsg(), + msg: + "error during loading keystore, JsonParsingError: " & + getCurrentExceptionMsg(), ) ) except ValueError: return err( AppKeystoreError( kind: KeystoreJsonError, - msg: "error in loadAppKeystore: " & getCurrentExceptionMsg(), + msg: + "error during loading keystore, ValueError: " & getCurrentExceptionMsg(), ) ) except OSError: return err( AppKeystoreError( kind: KeystoreOsError, - msg: "error in loadAppKeystore: " & getCurrentExceptionMsg(), + msg: "error during loading keystore, OSError: " & getCurrentExceptionMsg(), ) ) except Exception: #parseJson raises Exception return err( AppKeystoreError( kind: KeystoreOsError, - msg: "error in loadAppKeystore: " & getCurrentExceptionMsg(), + msg: "error during loading keystore, Exception: " & getCurrentExceptionMsg(), ) ) except IOError: return err( AppKeystoreError( kind: KeystoreIoError, - msg: "error in loadAppKeystore: " & getCurrentExceptionMsg(), + msg: "error during loading keystore, IOError: " & getCurrentExceptionMsg(), ) )