Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dabigjoe6 committed Jan 15, 2024
1 parent f262dee commit b471c94
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
13 changes: 8 additions & 5 deletions src/send-feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ class SendFeed {


async summarizeResources(resources: ResourceI[]) {
const newResources: ResourceI[] = [];

try {
const newResources: ResourceI[] = [];
for (const resource of resources) {
resource.readLength = readingTime(resource.content || resource.description || "").text
if ((!resource.summary || resource.summary.length < 100) && (resource.content || resource.description)) {
Expand All @@ -52,14 +53,16 @@ class SendFeed {
}
newResources.push(resource)
}

return newResources;
} catch (err) {
// Handle errors with summarizing, allowing it to fail silently
// so users can still get the digests without summarization
console.error("Something went wrong with summarizing", err);
} finally {
return resources;
// check if newResources is the same length as resources
// if it isn't, it means something must have gotten when setting summaries
// in new resources, hence return resources

return newResources.length === resources.length ? newResources : resources;
}
}

Expand Down Expand Up @@ -180,4 +183,4 @@ class SendFeed {
}
};

export default SendFeed;
export default SendFeed;
5 changes: 3 additions & 2 deletions src/utils/email/generateEmailTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ResourceI } from "../constants.js";
import { handleContentOrDescription } from "../preprocessing.js";
import moment from 'moment';
const generatePost = (post: ResourceI, isSummaryEnabled: boolean) => `
<table style="font-family:trebuchet ms,geneva;" role="presentation" cellpadding="0" cellspacing="0" width="100%" border="0">
Expand Down Expand Up @@ -26,8 +27,8 @@ const generatePost = (post: ResourceI, isSummaryEnabled: boolean) => `
? post?.summary[0] === ":"
? "Summary" + post.summary
: "Summary: " + post.summary
: (post.content || post.description)
: (post.content || post.description)}</p>
: handleContentOrDescription(post.content || "", post.description || "")
: handleContentOrDescription(post.content || "", post.description || "")}</p>
</div>
</td>
</tr>
Expand Down
13 changes: 13 additions & 0 deletions src/utils/preprocessing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,16 @@ export const cleanHTMLContent = (str: string) => {

return content;
};

export const handleContentOrDescription = (content: string, description: string): string => {
let result: string
if (content && description) {
result = content.length < description.length ? content : description;
} else if (content) {
result = content;
} else {
result = description;
}

return result.substring(0, 25);
}
3 changes: 2 additions & 1 deletion src/utils/summarize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export class Summarizer {

return response?.data?.choices[0]?.text?.trim() || "";
} catch (err) {
console.error("Error summarizing: ", err)
console.warn("Error summarizing: ", err);
return "";
}
}

Expand Down

0 comments on commit b471c94

Please sign in to comment.