Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
chore: Better error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
fourjuaneight committed Feb 7, 2024
1 parent 8e07eb2 commit 0500fa4
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 38 deletions.
7 changes: 2 additions & 5 deletions src/bookmark-podcasts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ const paramCleaner = (data: string, pattern: RegExp): string => {
return match[0].replace(pattern, '$1');
}

const error = '(paramCleaner): Unable to find match.';

console.error('Unable to find match.');
throw error;
throw '(paramCleaner): Unable to find match.';
};

/**
Expand Down Expand Up @@ -139,7 +136,7 @@ const getPodcastDetails = async (url: string): Promise<BookmarkData> => {
url: link,
};
} catch (error) {
throw `(getPodcastDetails):\n ${error}`;
throw `(getPodcastDetails): ${error}`;
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/bookmark-reddits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const getRedditDetails = async (url: string): Promise<RedditData> => {
url,
};
} catch (error) {
throw `(getRedditDetails): \n ${error}`;
throw `(getRedditDetails): ${error}`;
}
};

Expand Down
9 changes: 3 additions & 6 deletions src/bookmark-tweets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ const expandLinks = async (url: string): Promise<string> => {

return request.url;
} catch (error) {
console.log('(expandLinks):', error);
throw `(expandLinks):\n${error}`;
throw `(expandLinks): ${error}`;
}
};

Expand Down Expand Up @@ -97,8 +96,7 @@ const expandShortLink = async (str: string, regex: RegExp): Promise<string> => {

return replacer ? str.replace(regex, replacer) : str;
} catch (error) {
console.log('(expandShortLink):', error);
throw `(expandShortLink):\n${error}`;
throw `(expandShortLink): ${error}`;
}
};

Expand Down Expand Up @@ -161,8 +159,7 @@ const getTweetDetails = async (url: string): Promise<TwitterData> => {
url: `https://twitter.com/${username}/status/${response.data.id}`,
};
} catch (error) {
console.log('(getTweetDetails):', error);
throw `(getTweetDetails):\n${error}`;
throw `(getTweetDetails): ${error}`;
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/bookmark-vimeos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const getVimeoDetails = async (url: string): Promise<BookmarkData> => {
url,
};
} catch (error) {
throw `(getVimeoDetails):\n${error}`;
throw `(getVimeoDetails): ${error}`;
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ const handleAction = async (
version,
});
} catch (error) {
console.log('handleAction', error);
console.log('[handleAction]: ', error);
ctx.status(500);
return ctx.json<BookmarkResponse>({
error,
Expand Down
42 changes: 18 additions & 24 deletions src/hasura.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ export const queryTags = async (
});

if (request.status !== 200) {
throw `(queryTags): ${request.status} - ${request.statusText}`;
throw `(request): ${request.status} - ${request.statusText}`;
}

const response: HasuraQueryTagsResp | HasuraErrors = await request.json();

if (response.errors) {
const { errors } = response as HasuraErrors;

throw `(queryTags) - ${table}: \n ${errors
throw `(response) - ${table}: \n ${errors
.map(err => `${err.extensions.path}: ${err.message}`)
.join('\n')} \n ${query}`;
}
Expand All @@ -121,8 +121,7 @@ export const queryTags = async (

return tags;
} catch (error) {
console.log(error);
throw error;
throw `(queryTags): ${error}`;
}
};

Expand Down Expand Up @@ -164,15 +163,15 @@ export const queryBookmarkItems = async (
});

if (request.status !== 200) {
throw `(queryBookmarkItems): ${request.status} - ${request.statusText}`;
throw `(request): ${request.status} - ${request.statusText}`;
}

const response: HasuraQueryResp | HasuraErrors = await request.json();

if (response.errors) {
const { errors } = response as HasuraErrors;

throw `(queryBookmarkItems) - ${table}: \n ${errors
throw `(response) - ${table}: \n ${errors
.map(err => `${err.extensions.path}: ${err.message}`)
.join('\n')} \n ${query}`;
}
Expand All @@ -190,8 +189,7 @@ export const queryBookmarkItems = async (

return keyedRecords;
} catch (error) {
console.log(error);
throw error;
throw `(queryBookmarkItems): ${error}`;
}
};

Expand Down Expand Up @@ -237,15 +235,15 @@ export const queryBookmarkItemsByTable = async (
});

if (request.status !== 200) {
throw `(queryBookmarkItems): ${request.status} - ${request.statusText}`;
throw `(request): ${request.status} - ${request.statusText}`;
}

const response: HasuraQueryResp | HasuraErrors = await request.json();

if (response.errors) {
const { errors } = response as HasuraErrors;

throw `(queryBookmarkItems) - ${table}: \n ${errors
throw `(response) - ${table}: \n ${errors
.map(err => `${err.extensions.path}: ${err.message}`)
.join('\n')} \n ${query}`;
}
Expand All @@ -254,8 +252,7 @@ export const queryBookmarkItemsByTable = async (

return records;
} catch (error) {
console.log(error);
throw error;
throw `(queryBookmarkItemsByTable): ${error}`;
}
};

Expand Down Expand Up @@ -296,15 +293,15 @@ export const queryBookmarkAggregateCount = async (
});

if (request.status !== 200) {
throw `(queryBookmarkAggregateCount): ${request.status} - ${request.statusText}`;
throw `(request): ${request.status} - ${request.statusText}`;
}

const response: any = await request.json();

if (response.errors) {
const { errors } = response as HasuraErrors;

throw `(queryBookmarkAggregateCount) - ${table}: \n ${errors
throw `(response) - ${table}: \n ${errors
.map(err => `${err.extensions.path}: ${err.message}`)
.join('\n')} \n ${query}`;
}
Expand All @@ -322,8 +319,7 @@ export const queryBookmarkAggregateCount = async (

return countUniqueSorted(collection);
} catch (error) {
console.log(error);
throw error;
throw `(queryBookmarkAggregateCount): ${error}`;
}
};

Expand Down Expand Up @@ -370,15 +366,15 @@ export const searchBookmarkItems = async (
});

if (request.status !== 200) {
throw `(searchBookmarkItems): ${request.status} - ${request.statusText}`;
throw `(request): ${request.status} - ${request.statusText}`;
}

const response: HasuraQueryResp | HasuraErrors = await request.json();

if (response.errors) {
const { errors } = response as HasuraErrors;

throw `(searchBookmarkItems) - ${table}: \n ${errors
throw `(response) - ${table}: \n ${errors
.map(err => `${err.extensions.path}: ${err.message}`)
.join('\n')} \n ${query}`;
}
Expand All @@ -396,8 +392,7 @@ export const searchBookmarkItems = async (

return keyedRecords;
} catch (error) {
console.log(error);
throw error;
throw `(searchBookmarkItems): ${error}`;
}
};

Expand Down Expand Up @@ -455,23 +450,22 @@ export const addHasuraRecord = async (
});

if (request.status !== 200) {
throw `(addHasuraRecord): ${request.status} - ${request.statusText}`;
throw `(request): ${request.status} - ${request.statusText}`;
}

const response: HasuraInsertResp | HasuraErrors = await request.json();

if (response.errors) {
const { errors } = response as HasuraErrors;

throw `(addHasuraRecord) - ${table}: \n ${errors
throw `(response) - ${table}: \n ${errors
.map(err => `${err.extensions.path}: ${err.message}`)
.join('\n')} \n ${query}`;
}

return (response as HasuraInsertResp).data[`insert_bookmarks_${table}_one`]
.id;
} catch (error) {
console.log(error);
throw error;
throw `(addHasuraRecord): ${error}`;
}
};

0 comments on commit 0500fa4

Please sign in to comment.