Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekzaw committed Dec 3, 2024
1 parent 915a9b9 commit a28ce9e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ public void applyMarkdownFormatting(SpannableStringBuilder ssb) {
if (input.equals(mPrevInput) && mParserId == mPrevParserId) {
output = mPrevOutput;
} else {
output = nativeParseMarkdown(input, mParserId);
try {
output = nativeParseMarkdown(input, mParserId);
} catch (Exception e) {
output = "[]";
}
mPrevInput = input;
mPrevOutput = output;
mPrevParserId = mParserId;
Expand Down
31 changes: 13 additions & 18 deletions src/parseExpensiMark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,25 +258,20 @@ function parseExpensiMark(markdown: string): MarkdownRange[] {
if (markdown.length > MAX_PARSABLE_LENGTH) {
return [];
}
try {
const html = parseMarkdownToHTML(markdown);
const tokens = parseHTMLToTokens(html);
const tree = parseTokensToTree(tokens);
const [text, ranges] = parseTreeToTextAndRanges(tree);
if (text !== markdown) {
throw new Error(
`[react-native-live-markdown] Parsing error: the processed text does not match the original Markdown input. This may be caused by incorrect parsing functions or invalid input Markdown.\nProcessed input: '${JSON.stringify(
text,
)}'\nOriginal input: '${JSON.stringify(markdown)}'`,
);
}
const sortedRanges = sortRanges(ranges);
const groupedRanges = groupRanges(sortedRanges);
return groupedRanges;
} catch (error) {
// returning an empty array in case of error
return [];
const html = parseMarkdownToHTML(markdown);
const tokens = parseHTMLToTokens(html);
const tree = parseTokensToTree(tokens);
const [text, ranges] = parseTreeToTextAndRanges(tree);
if (text !== markdown) {
throw new Error(
`[react-native-live-markdown] Parsing error: the processed text does not match the original Markdown input. This may be caused by incorrect parsing functions or invalid input Markdown.\nProcessed input: '${JSON.stringify(
text,
)}'\nOriginal input: '${JSON.stringify(markdown)}'`,
);
}
const sortedRanges = sortRanges(ranges);
const groupedRanges = groupRanges(sortedRanges);
return groupedRanges;
}

export default parseExpensiMark;

0 comments on commit a28ce9e

Please sign in to comment.