🐛 Added logic for non-existant users

Fixes an internal server error when users do not exist
This commit is contained in:
Wazbat 2023-06-22 23:49:44 +02:00
parent 80207fc0d8
commit c196207fc3
3 changed files with 25 additions and 1 deletions

View file

@ -60,7 +60,12 @@ export const userAPI = async (
flags?: InputFlags flags?: InputFlags
): Promise<UserAPIResponse> => { ): Promise<UserAPIResponse> => {
const userResponse = await fetchUser(username, event); const userResponse = await fetchUser(username, event);
if (!userResponse || !Object.keys(userResponse).length) {
return {
code: 404,
message: 'User not found'
};
}
/* Creating the response objects */ /* Creating the response objects */
const response: UserAPIResponse = { code: 200, message: 'OK' } as UserAPIResponse; const response: UserAPIResponse = { code: 200, message: 'OK' } as UserAPIResponse;
const apiUser: APIUser = (await populateUserProperties(userResponse)) as APIUser; const apiUser: APIUser = (await populateUserProperties(userResponse)) as APIUser;

View file

@ -231,6 +231,10 @@ export const fetchUser = async (
// Validator function // Validator function
(_res: unknown) => { (_res: unknown) => {
const response = _res as GraphQLUserResponse; const response = _res as GraphQLUserResponse;
// If _res.data is an empty object, we have no user
if (!Object.keys(response?.data).length) {
return false;
}
return !( return !(
response?.data?.user?.result?.__typename !== 'User' || response?.data?.user?.result?.__typename !== 'User' ||
typeof response.data.user.result.legacy === 'undefined' typeof response.data.user.result.legacy === 'undefined'

View file

@ -270,3 +270,18 @@ test('API fetch user', async () => {
expect(user.birthday.month).toEqual(3); expect(user.birthday.month).toEqual(3);
expect(user.birthday.year).toBeUndefined(); expect(user.birthday.year).toBeUndefined();
}); });
test.only('API fetch user that does not exist', async () => {
const result = await cacheWrapper(
new Request('https://api.fxtwitter.com/usesaahah123', {
method: 'GET',
headers: botHeaders
})
);
expect(result.status).toEqual(404);
const response = (await result.json()) as UserAPIResponse;
expect(response).toBeTruthy();
expect(response.code).toEqual(404);
expect(response.message).toEqual('User not found');
expect(response.user).toBeUndefined();
});