Skip to content

Commit

Permalink
feat: improve profile sync services logs (#5101)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
Prithpal-Sooriya authored and PatrykLucka committed Jan 13, 2025
1 parent 7ed966f commit 8bbcc60
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import log from 'loglevel';

import { Env, Platform, getEnvUrls, getOidcClientId } from '../../shared/env';

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

const nonceJson: NonceResponse = await nonceResponse.json();
return nonceJson?.nonce ?? null;
} catch (e) {
console.error('authentication-controller/services: unable to get nonce', e);
log.error('authentication-controller/services: unable to get nonce', e);
return null;
}
}
Expand Down Expand Up @@ -107,13 +112,16 @@ export async function login(
});

if (!response.ok) {
log.error(
`authentication-controller/services: unable to login - HTTP ${response.status}`,
);
return null;
}

const loginResponse: LoginResponse = await response.json();
return loginResponse ?? null;
} catch (e) {
console.error('authentication-controller/services: unable to login', e);
log.error('authentication-controller/services: unable to login', e);
return null;
}
}
Expand Down Expand Up @@ -157,13 +165,16 @@ export async function getAccessToken(
});

if (!response.ok) {
log.error(
`authentication-controller/services: unable to get access token - HTTP ${response.status}`,
);
return null;
}

const accessTokenResponse: OAuthTokenResponse = await response.json();
return accessTokenResponse?.access_token ?? null;
} catch (e) {
console.error(
log.error(
'authentication-controller/services: unable to get access token',
e,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ export async function getUserStorage(
}

if (userStorageResponse.status !== 200) {
throw new Error('Unable to get User Storage');
throw new Error(
`Unable to get User Storage - HTTP ${userStorageResponse.status}`,
);
}

const userStorage: GetUserStorageResponse | null =
Expand Down Expand Up @@ -133,7 +135,9 @@ export async function getUserStorageAllFeatureEntries(
}

if (userStorageResponse.status !== 200) {
throw new Error('Unable to get User Storage');
throw new Error(
`Unable to get User Storage - HTTP ${userStorageResponse.status}`,
);
}

const userStorage: GetUserStorageAllFeatureEntriesResponse | null =
Expand Down Expand Up @@ -222,7 +226,9 @@ export async function upsertUserStorage(
});

if (!res.ok) {
throw new Error('user-storage - unable to upsert data');
throw new Error(
`user-storage - unable to upsert data - HTTP ${res.status}`,
);
}
}

Expand Down Expand Up @@ -266,7 +272,9 @@ export async function batchUpsertUserStorage(
});

if (!res.ok) {
throw new Error('user-storage - unable to batch upsert data');
throw new Error(
`user-storage - unable to batch upsert data - HTTP ${res.status}`,
);
}
}

Expand Down Expand Up @@ -301,7 +309,9 @@ export async function batchUpsertUserStorageWithAlreadyHashedAndEncryptedEntries
});

if (!res.ok) {
throw new Error('user-storage - unable to batch upsert data');
throw new Error(
`user-storage - unable to batch upsert data - HTTP ${res.status}`,
);
}
}

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

if (userStorageResponse.status === 404) {
throw new Error('user-storage - feature/entry not found');
throw new Error(
`user-storage - feature/entry not found - HTTP ${userStorageResponse.status}`,
);
}

if (!userStorageResponse.ok) {
throw new Error('user-storage - unable to delete data');
throw new Error(
`user-storage - unable to delete data - HTTP ${userStorageResponse.status}`,
);
}
}

Expand Down Expand Up @@ -370,7 +384,9 @@ export async function batchDeleteUserStorage(
});

if (!res.ok) {
throw new Error('user-storage - unable to batch delete data');
throw new Error(
`user-storage - unable to batch delete data - HTTP ${res.status}`,
);
}
}

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

if (userStorageResponse.status === 404) {
throw new Error('user-storage - feature not found');
throw new Error(
`user-storage - feature not found - HTTP ${userStorageResponse.status}`,
);
}

if (!userStorageResponse.ok) {
throw new Error('user-storage - unable to delete data');
throw new Error(
`user-storage - unable to delete data - HTTP ${userStorageResponse.status}`,
);
}
}

0 comments on commit 8bbcc60

Please sign in to comment.