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

This commit is contained in:
Ivan FB 2024-10-03 00:05:49 +02:00 committed by GitHub
parent 713aa66a63
commit 3ad613cad4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 57 additions and 13 deletions

View File

@ -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(),
)
)

View File

@ -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()
@ -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(

View File

@ -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})