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)) let jsonObject = parseJson(string.fromBytes(encodedCredential))
return ok(to(jsonObject, KeystoreMembership)) return ok(to(jsonObject, KeystoreMembership))
except JsonParsingError: 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 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) f.write(separator)
ok() ok()
except CatchableError: except CatchableError:
err(AppKeystoreError(kind: KeystoreOsError, msg: getCurrentExceptionMsg())) err(
AppKeystoreError(
kind: KeystoreOsError,
msg: "error while writing keystore: " & getCurrentExceptionMsg(),
)
)
finally: finally:
f.close() f.close()
@ -114,19 +119,43 @@ proc loadAppKeystore*(
return ok(data) return ok(data)
# TODO: we might continue rather than return for some of these errors # TODO: we might continue rather than return for some of these errors
except JsonParsingError: except JsonParsingError:
return return err(
err(AppKeystoreError(kind: KeystoreJsonError, msg: getCurrentExceptionMsg())) AppKeystoreError(
kind: KeystoreJsonError,
msg:
"error during loading keystore, JsonParsingError: " &
getCurrentExceptionMsg(),
)
)
except ValueError: except ValueError:
return return err(
err(AppKeystoreError(kind: KeystoreJsonError, msg: getCurrentExceptionMsg())) AppKeystoreError(
kind: KeystoreJsonError,
msg:
"error during loading keystore, ValueError: " & getCurrentExceptionMsg(),
)
)
except OSError: except OSError:
return return err(
err(AppKeystoreError(kind: KeystoreOsError, msg: getCurrentExceptionMsg())) AppKeystoreError(
kind: KeystoreOsError,
msg: "error during loading keystore, OSError: " & getCurrentExceptionMsg(),
)
)
except Exception: #parseJson raises Exception except Exception: #parseJson raises Exception
return return err(
err(AppKeystoreError(kind: KeystoreOsError, msg: getCurrentExceptionMsg())) AppKeystoreError(
kind: KeystoreOsError,
msg: "error during loading keystore, Exception: " & getCurrentExceptionMsg(),
)
)
except IOError: except IOError:
return err(AppKeystoreError(kind: KeystoreIoError, msg: getCurrentExceptionMsg())) return err(
AppKeystoreError(
kind: KeystoreIoError,
msg: "error during loading keystore, IOError: " & getCurrentExceptionMsg(),
)
)
return err( return err(
AppKeystoreError( AppKeystoreError(

View File

@ -30,7 +30,12 @@ proc save*(json: JsonNode, path: string, separator: string): KeystoreResult[void
# We save the updated json # We save the updated json
var f: File var f: File
if not f.open(path, fmAppend): 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: try:
# To avoid other users/attackers to be able to read keyfiles, we make the file readable/writable only by the running user # 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}) setFilePermissions(path, {fpUserWrite, fpUserRead})