Skip to content

Commit

Permalink
refactor: add improved services logs by adding response status to errors
Browse files Browse the repository at this point in the history
this should help us debug and separate server errors a client is producing
  • Loading branch information
Prithpal-Sooriya committed Jan 6, 2025
1 parent e825794 commit 3fa51ab
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 3fa51ab

Please sign in to comment.