Skip to content

Commit

Permalink
refactor: improve do client on handling empty body response
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung committed May 25, 2022
1 parent b94f7af commit 4bb2395
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
9 changes: 7 additions & 2 deletions worker/context/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@ export function createSession(

try {
await userStore.updateProfile(userProfile);
} catch (e) {
console.log('Fail updating user profile:', e);
} catch (ex) {
console.log(
'Fail updating user profile:',
ex instanceof Error ? ex.message : ex,
);

throw ex;
}

return userProfile;
Expand Down
2 changes: 1 addition & 1 deletion worker/store/UserStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export function getUserStore(
async updateProfile(profile: UserProfile): Promise<void> {
const client = createClient(env.USER_STORE, profile.id);

return await client.updateProfile(profile);
await client.updateProfile(profile);
},
async view(
userId: string | null,
Expand Down
12 changes: 6 additions & 6 deletions worker/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ export function configureStore<Env, T extends Record<string, any>>(
const handler = this.handlers[method];

if (typeof handler === 'function') {
return new Response(JSON.stringify(await handler(...args)) ?? null, {
status: 200,
const result = JSON.stringify(await handler(...args)) ?? null;

return new Response(result, {
status: result !== null ? 200 : 204,
});
} else {
return new Response('Not Found', { status: 404 });
Expand Down Expand Up @@ -63,11 +65,9 @@ export function configureStore<Env, T extends Record<string, any>>(

switch (response.status) {
case 200:
if (!response.body) {
return;
}

return await response.json();
case 204:
return;
case 404:
throw new Error(`Method ${method.toString()} is not available`);
default:
Expand Down

0 comments on commit 4bb2395

Please sign in to comment.