Skip to content

Commit ec8c7f4

Browse files
jonsimantova-maurice
authored andcommitted
When printing libsecret errors on Linux, suppress the error if the same one
happens multiple times in a row. The user only needs to see it once. PiperOrigin-RevId: 290152261
1 parent 5410f26 commit ec8c7f4

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

app/src/secure/user_secure_linux_internal.cc

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ SecretSchema BuildSchema(const char key_namespace[]) {
4747

4848
UserSecureLinuxInternal::UserSecureLinuxInternal(const char* domain,
4949
const char* key_namespace)
50-
: domain_(domain), key_namespace_(key_namespace) {
50+
: domain_(domain), key_namespace_(key_namespace), known_error_code_(0) {
5151
storage_schema_ = BuildSchema(key_namespace_.c_str());
5252
}
5353

@@ -68,14 +68,8 @@ std::string UserSecureLinuxInternal::LoadUserData(const std::string& app_name) {
6868
/* key1= */ kAppNameKey,
6969
/* value1= */ app_name.c_str(), /* key2= */ kStorageDomainKey,
7070
/* value2= */ domain_.c_str(), nullptr);
71-
if (error) {
72-
LogWarning("Secret lookup failed. Error %d: %s", error->code,
73-
error->message);
74-
g_error_free(error);
75-
return empty_str;
76-
}
7771

78-
if (result == nullptr) {
72+
if (CheckForError(&error, "lookup") || result == nullptr) {
7973
return empty_str;
8074
}
8175
std::string str_result(result);
@@ -97,11 +91,7 @@ void UserSecureLinuxInternal::SaveUserData(const std::string& app_name,
9791
/* value1= */ app_name.c_str(),
9892
/* key2= */ kStorageDomainKey, /* value2= */ domain_.c_str(), nullptr);
9993

100-
if (error) {
101-
LogWarning("Secret store failed. Error %d: %s", error->code,
102-
error->message);
103-
g_error_free(error);
104-
}
94+
CheckForError(&error, "store");
10595
}
10696

10797
void UserSecureLinuxInternal::DeleteUserData(const std::string& app_name) {
@@ -116,11 +106,7 @@ void UserSecureLinuxInternal::DeleteUserData(const std::string& app_name) {
116106
/* key2= */ kStorageDomainKey,
117107
/* value2= */ domain_.c_str(), nullptr);
118108

119-
if (error) {
120-
LogWarning("Secret clear failed. Error %d: %s", error->code,
121-
error->message);
122-
g_error_free(error);
123-
}
109+
CheckForError(&error, "clear");
124110
}
125111

126112
void UserSecureLinuxInternal::DeleteAllData() {
@@ -132,11 +118,20 @@ void UserSecureLinuxInternal::DeleteAllData() {
132118
/* error= */ &error,
133119
/* key2= */ kStorageDomainKey,
134120
/* value2= */ domain_.c_str(), nullptr);
135-
if (error) {
136-
LogWarning("Secret clear failed. Error %d: %s", error->code,
137-
error->message);
138-
g_error_free(error);
121+
CheckForError(&error, "clear");
122+
}
123+
124+
bool UserSecureLinuxInternal::CheckForError(GError** error,
125+
const char* function_name) {
126+
if (error == nullptr || *error == nullptr) return false;
127+
if ((*error)->code != known_error_code_) {
128+
LogWarning("Secret %s failed. Error %d: %s", function_name, (*error)->code,
129+
(*error)->message);
130+
known_error_code_ = (*error)->code;
139131
}
132+
g_error_free(*error);
133+
*error = nullptr;
134+
return true;
140135
}
141136

142137
} // namespace secure

app/src/secure/user_secure_linux_internal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,15 @@ class UserSecureLinuxInternal : public UserSecureInternal {
4545
void DeleteAllData() override;
4646

4747
private:
48+
// Check if there is an error, log it and free it if there is.
49+
// Returns true iff there was an error.
50+
bool CheckForError(GError** error, const char* function_name);
51+
4852
const std::string domain_;
4953
const std::string key_namespace_;
5054
SecretSchema storage_schema_;
55+
int known_error_code_; // If nonzero, this error was already logged, so
56+
// don't be too spammy about it.
5157
};
5258

5359
} // namespace secure

0 commit comments

Comments
 (0)