Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: waku_keystore: give some more context in case of error #3064

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading