Skip to content

Commit

Permalink
chore: waku_keystore: give some more context in case of error (#3064)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivansete-status authored Oct 2, 2024
1 parent 713aa66 commit 3ad613c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
14 changes: 12 additions & 2 deletions waku/waku_keystore/conversion_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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 during decoding credentials: " & 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(),
)
)
49 changes: 39 additions & 10 deletions waku/waku_keystore/keystore.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ proc createAppKeystore*(
f.write(separator)
ok()
except CatchableError:
err(AppKeystoreError(kind: KeystoreOsError, msg: getCurrentExceptionMsg()))
err(
AppKeystoreError(
kind: KeystoreOsError,
msg: "error while writing keystore: " & getCurrentExceptionMsg(),
)
)
finally:
f.close()

Expand Down Expand Up @@ -114,19 +119,43 @@ 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 during loading keystore, JsonParsingError: " &
getCurrentExceptionMsg(),
)
)
except ValueError:
return
err(AppKeystoreError(kind: KeystoreJsonError, msg: getCurrentExceptionMsg()))
return err(
AppKeystoreError(
kind: KeystoreJsonError,
msg:
"error during loading keystore, ValueError: " & getCurrentExceptionMsg(),
)
)
except OSError:
return
err(AppKeystoreError(kind: KeystoreOsError, msg: getCurrentExceptionMsg()))
return err(
AppKeystoreError(
kind: KeystoreOsError,
msg: "error during loading keystore, OSError: " & getCurrentExceptionMsg(),
)
)
except Exception: #parseJson raises Exception
return
err(AppKeystoreError(kind: KeystoreOsError, msg: getCurrentExceptionMsg()))
return err(
AppKeystoreError(
kind: KeystoreOsError,
msg: "error during loading keystore, Exception: " & getCurrentExceptionMsg(),
)
)
except IOError:
return err(AppKeystoreError(kind: KeystoreIoError, msg: getCurrentExceptionMsg()))
return err(
AppKeystoreError(
kind: KeystoreIoError,
msg: "error during loading keystore, IOError: " & getCurrentExceptionMsg(),
)
)

return err(
AppKeystoreError(
Expand Down
7 changes: 6 additions & 1 deletion waku/waku_keystore/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down

0 comments on commit 3ad613c

Please sign in to comment.