Skip to content

Commit b37cfa6

Browse files
feat: improve profile sync services logs (#5101)
## Explanation This improves service logs by adding the HTTP response status to the log metadata. This should help us debug and differentiate server errors a client is producing. ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> ## Changelog <!-- If you're making any consumer-facing changes, list those changes here as if you were updating a changelog, using the template below as a guide. (CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or FIXED. For security-related issues, follow the Security Advisory process.) Please take care to name the exact pieces of the API you've added or changed (e.g. types, interfaces, functions, or methods). If there are any breaking changes, make sure to offer a solution for consumers to follow once they upgrade to the changes. Finally, if you're only making changes to development scripts or tests, you may replace the template below with "None". --> ### `@metamask/profile-sync-controller` - **ADDED**: HTTP response status to error logs in services files. ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've highlighted breaking changes using the "BREAKING" category above as appropriate - [x] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes
1 parent e825794 commit b37cfa6

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

packages/profile-sync-controller/src/controllers/authentication/services.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import log from 'loglevel';
2+
13
import { Env, Platform, getEnvUrls, getOidcClientId } from '../../shared/env';
24

35
const ENV_URLS = getEnvUrls(Env.PRD);
@@ -37,13 +39,16 @@ export async function getNonce(publicKey: string): Promise<string | null> {
3739
try {
3840
const nonceResponse = await fetch(nonceUrl.toString());
3941
if (!nonceResponse.ok) {
42+
log.error(
43+
`authentication-controller/services: unable to get nonce - HTTP ${nonceResponse.status}`,
44+
);
4045
return null;
4146
}
4247

4348
const nonceJson: NonceResponse = await nonceResponse.json();
4449
return nonceJson?.nonce ?? null;
4550
} catch (e) {
46-
console.error('authentication-controller/services: unable to get nonce', e);
51+
log.error('authentication-controller/services: unable to get nonce', e);
4752
return null;
4853
}
4954
}
@@ -107,13 +112,16 @@ export async function login(
107112
});
108113

109114
if (!response.ok) {
115+
log.error(
116+
`authentication-controller/services: unable to login - HTTP ${response.status}`,
117+
);
110118
return null;
111119
}
112120

113121
const loginResponse: LoginResponse = await response.json();
114122
return loginResponse ?? null;
115123
} catch (e) {
116-
console.error('authentication-controller/services: unable to login', e);
124+
log.error('authentication-controller/services: unable to login', e);
117125
return null;
118126
}
119127
}
@@ -157,13 +165,16 @@ export async function getAccessToken(
157165
});
158166

159167
if (!response.ok) {
168+
log.error(
169+
`authentication-controller/services: unable to get access token - HTTP ${response.status}`,
170+
);
160171
return null;
161172
}
162173

163174
const accessTokenResponse: OAuthTokenResponse = await response.json();
164175
return accessTokenResponse?.access_token ?? null;
165176
} catch (e) {
166-
console.error(
177+
log.error(
167178
'authentication-controller/services: unable to get access token',
168179
e,
169180
);

packages/profile-sync-controller/src/controllers/user-storage/services.ts

+30-10
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ export async function getUserStorage(
7676
}
7777

7878
if (userStorageResponse.status !== 200) {
79-
throw new Error('Unable to get User Storage');
79+
throw new Error(
80+
`Unable to get User Storage - HTTP ${userStorageResponse.status}`,
81+
);
8082
}
8183

8284
const userStorage: GetUserStorageResponse | null =
@@ -133,7 +135,9 @@ export async function getUserStorageAllFeatureEntries(
133135
}
134136

135137
if (userStorageResponse.status !== 200) {
136-
throw new Error('Unable to get User Storage');
138+
throw new Error(
139+
`Unable to get User Storage - HTTP ${userStorageResponse.status}`,
140+
);
137141
}
138142

139143
const userStorage: GetUserStorageAllFeatureEntriesResponse | null =
@@ -222,7 +226,9 @@ export async function upsertUserStorage(
222226
});
223227

224228
if (!res.ok) {
225-
throw new Error('user-storage - unable to upsert data');
229+
throw new Error(
230+
`user-storage - unable to upsert data - HTTP ${res.status}`,
231+
);
226232
}
227233
}
228234

@@ -266,7 +272,9 @@ export async function batchUpsertUserStorage(
266272
});
267273

268274
if (!res.ok) {
269-
throw new Error('user-storage - unable to batch upsert data');
275+
throw new Error(
276+
`user-storage - unable to batch upsert data - HTTP ${res.status}`,
277+
);
270278
}
271279
}
272280

@@ -301,7 +309,9 @@ export async function batchUpsertUserStorageWithAlreadyHashedAndEncryptedEntries
301309
});
302310

303311
if (!res.ok) {
304-
throw new Error('user-storage - unable to batch upsert data');
312+
throw new Error(
313+
`user-storage - unable to batch upsert data - HTTP ${res.status}`,
314+
);
305315
}
306316
}
307317

@@ -326,11 +336,15 @@ export async function deleteUserStorage(
326336
});
327337

328338
if (userStorageResponse.status === 404) {
329-
throw new Error('user-storage - feature/entry not found');
339+
throw new Error(
340+
`user-storage - feature/entry not found - HTTP ${userStorageResponse.status}`,
341+
);
330342
}
331343

332344
if (!userStorageResponse.ok) {
333-
throw new Error('user-storage - unable to delete data');
345+
throw new Error(
346+
`user-storage - unable to delete data - HTTP ${userStorageResponse.status}`,
347+
);
334348
}
335349
}
336350

@@ -370,7 +384,9 @@ export async function batchDeleteUserStorage(
370384
});
371385

372386
if (!res.ok) {
373-
throw new Error('user-storage - unable to batch delete data');
387+
throw new Error(
388+
`user-storage - unable to batch delete data - HTTP ${res.status}`,
389+
);
374390
}
375391
}
376392

@@ -394,10 +410,14 @@ export async function deleteUserStorageAllFeatureEntries(
394410
});
395411

396412
if (userStorageResponse.status === 404) {
397-
throw new Error('user-storage - feature not found');
413+
throw new Error(
414+
`user-storage - feature not found - HTTP ${userStorageResponse.status}`,
415+
);
398416
}
399417

400418
if (!userStorageResponse.ok) {
401-
throw new Error('user-storage - unable to delete data');
419+
throw new Error(
420+
`user-storage - unable to delete data - HTTP ${userStorageResponse.status}`,
421+
);
402422
}
403423
}

0 commit comments

Comments
 (0)