From 0500fa452dd0279c0fb1cbc900cda7830838b8f1 Mon Sep 17 00:00:00 2001 From: Juan Villela Date: Wed, 7 Feb 2024 12:49:56 -0500 Subject: [PATCH] chore: Better error handling. --- src/bookmark-podcasts.ts | 7 ++----- src/bookmark-reddits.ts | 2 +- src/bookmark-tweets.ts | 9 +++------ src/bookmark-vimeos.ts | 2 +- src/handler.ts | 2 +- src/hasura.ts | 42 +++++++++++++++++----------------------- 6 files changed, 26 insertions(+), 38 deletions(-) diff --git a/src/bookmark-podcasts.ts b/src/bookmark-podcasts.ts index 70a5ffe..e794b9d 100644 --- a/src/bookmark-podcasts.ts +++ b/src/bookmark-podcasts.ts @@ -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.'; }; /** @@ -139,7 +136,7 @@ const getPodcastDetails = async (url: string): Promise => { url: link, }; } catch (error) { - throw `(getPodcastDetails):\n ${error}`; + throw `(getPodcastDetails): ${error}`; } }; diff --git a/src/bookmark-reddits.ts b/src/bookmark-reddits.ts index da84667..610a9f1 100644 --- a/src/bookmark-reddits.ts +++ b/src/bookmark-reddits.ts @@ -39,7 +39,7 @@ const getRedditDetails = async (url: string): Promise => { url, }; } catch (error) { - throw `(getRedditDetails): \n ${error}`; + throw `(getRedditDetails): ${error}`; } }; diff --git a/src/bookmark-tweets.ts b/src/bookmark-tweets.ts index 270ca33..a787bfd 100644 --- a/src/bookmark-tweets.ts +++ b/src/bookmark-tweets.ts @@ -65,8 +65,7 @@ const expandLinks = async (url: string): Promise => { return request.url; } catch (error) { - console.log('(expandLinks):', error); - throw `(expandLinks):\n${error}`; + throw `(expandLinks): ${error}`; } }; @@ -97,8 +96,7 @@ const expandShortLink = async (str: string, regex: RegExp): Promise => { return replacer ? str.replace(regex, replacer) : str; } catch (error) { - console.log('(expandShortLink):', error); - throw `(expandShortLink):\n${error}`; + throw `(expandShortLink): ${error}`; } }; @@ -161,8 +159,7 @@ const getTweetDetails = async (url: string): Promise => { url: `https://twitter.com/${username}/status/${response.data.id}`, }; } catch (error) { - console.log('(getTweetDetails):', error); - throw `(getTweetDetails):\n${error}`; + throw `(getTweetDetails): ${error}`; } }; diff --git a/src/bookmark-vimeos.ts b/src/bookmark-vimeos.ts index 2d59466..4067223 100644 --- a/src/bookmark-vimeos.ts +++ b/src/bookmark-vimeos.ts @@ -49,7 +49,7 @@ const getVimeoDetails = async (url: string): Promise => { url, }; } catch (error) { - throw `(getVimeoDetails):\n${error}`; + throw `(getVimeoDetails): ${error}`; } }; diff --git a/src/handler.ts b/src/handler.ts index a1e9921..516b22f 100644 --- a/src/handler.ts +++ b/src/handler.ts @@ -187,7 +187,7 @@ const handleAction = async ( version, }); } catch (error) { - console.log('handleAction', error); + console.log('[handleAction]: ', error); ctx.status(500); return ctx.json({ error, diff --git a/src/hasura.ts b/src/hasura.ts index e26b1cc..143ba6f 100644 --- a/src/hasura.ts +++ b/src/hasura.ts @@ -102,7 +102,7 @@ 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(); @@ -110,7 +110,7 @@ export const queryTags = async ( 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}`; } @@ -121,8 +121,7 @@ export const queryTags = async ( return tags; } catch (error) { - console.log(error); - throw error; + throw `(queryTags): ${error}`; } }; @@ -164,7 +163,7 @@ 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(); @@ -172,7 +171,7 @@ export const queryBookmarkItems = async ( 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}`; } @@ -190,8 +189,7 @@ export const queryBookmarkItems = async ( return keyedRecords; } catch (error) { - console.log(error); - throw error; + throw `(queryBookmarkItems): ${error}`; } }; @@ -237,7 +235,7 @@ 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(); @@ -245,7 +243,7 @@ export const queryBookmarkItemsByTable = async ( 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}`; } @@ -254,8 +252,7 @@ export const queryBookmarkItemsByTable = async ( return records; } catch (error) { - console.log(error); - throw error; + throw `(queryBookmarkItemsByTable): ${error}`; } }; @@ -296,7 +293,7 @@ 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(); @@ -304,7 +301,7 @@ export const queryBookmarkAggregateCount = async ( 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}`; } @@ -322,8 +319,7 @@ export const queryBookmarkAggregateCount = async ( return countUniqueSorted(collection); } catch (error) { - console.log(error); - throw error; + throw `(queryBookmarkAggregateCount): ${error}`; } }; @@ -370,7 +366,7 @@ 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(); @@ -378,7 +374,7 @@ export const searchBookmarkItems = async ( 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}`; } @@ -396,8 +392,7 @@ export const searchBookmarkItems = async ( return keyedRecords; } catch (error) { - console.log(error); - throw error; + throw `(searchBookmarkItems): ${error}`; } }; @@ -455,7 +450,7 @@ 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(); @@ -463,7 +458,7 @@ export const addHasuraRecord = async ( 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}`; } @@ -471,7 +466,6 @@ export const addHasuraRecord = async ( return (response as HasuraInsertResp).data[`insert_bookmarks_${table}_one`] .id; } catch (error) { - console.log(error); - throw error; + throw `(addHasuraRecord): ${error}`; } };