From a481424f7b37f62422dd6216ffd6568636b2f000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20R=C3=BCger?= Date: Fri, 13 Mar 2026 01:13:51 +0100 Subject: [PATCH] fix: return error instead of panic from CompileMarkdown Markdown conversion failures called panic(err), crashing the process rather than allowing graceful error handling. Change the return type to (string, []attachment.Attachment, error) and propagate the error. Update all callers (mark.go, markdown_test.go) accordingly. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- mark.go | 10 ++++++++-- markdown/markdown.go | 6 +++--- markdown/markdown_test.go | 6 +++--- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/mark.go b/mark.go index 82a4f70..cb8882f 100644 --- a/mark.go +++ b/mark.go @@ -265,7 +265,10 @@ func ProcessFile(file string, api *confluence.API, config Config) (*confluence.P Features: config.Features, ImageAlign: imageAlign, } - html, _ := markmd.CompileMarkdown(markdown, std, file, cfg) + html, _, err := markmd.CompileMarkdown(markdown, std, file, cfg) + if err != nil { + return nil, fmt.Errorf("unable to compile markdown: %w", err) + } if _, err := fmt.Fprintln(config.output(), html); err != nil { return nil, err } @@ -340,7 +343,10 @@ func ProcessFile(file string, api *confluence.API, config Config) (*confluence.P ImageAlign: imageAlign, } - html, inlineAttachments := markmd.CompileMarkdown(markdown, std, file, cfg) + html, inlineAttachments, err := markmd.CompileMarkdown(markdown, std, file, cfg) + if err != nil { + return nil, fmt.Errorf("unable to compile markdown: %w", err) + } if _, err = attachment.ResolveAttachments(api, target, inlineAttachments); err != nil { return nil, fmt.Errorf("unable to create/update attachments: %w", err) diff --git a/markdown/markdown.go b/markdown/markdown.go index da7e78b..e28e1db 100644 --- a/markdown/markdown.go +++ b/markdown/markdown.go @@ -90,7 +90,7 @@ func (c *ConfluenceExtension) Extend(m goldmark.Markdown) { )) } -func CompileMarkdown(markdown []byte, stdlib *stdlib.Lib, path string, cfg types.MarkConfig) (string, []attachment.Attachment) { +func CompileMarkdown(markdown []byte, stdlib *stdlib.Lib, path string, cfg types.MarkConfig) (string, []attachment.Attachment, error) { log.Tracef(nil, "rendering markdown:\n%s", string(markdown)) confluenceExtension := NewConfluenceExtension(stdlib, path, cfg) @@ -119,12 +119,12 @@ func CompileMarkdown(markdown []byte, stdlib *stdlib.Lib, path string, cfg types err := converter.Convert(markdown, &buf, parser.WithContext(ctx)) if err != nil { - panic(err) + return "", nil, err } html := buf.Bytes() log.Tracef(nil, "rendered markdown to html:\n%s", string(html)) - return string(html), confluenceExtension.Attachments + return string(html), confluenceExtension.Attachments, nil } diff --git a/markdown/markdown_test.go b/markdown/markdown_test.go index 91fe041..eb0313e 100644 --- a/markdown/markdown_test.go +++ b/markdown/markdown_test.go @@ -67,7 +67,7 @@ func TestCompileMarkdown(t *testing.T) { Features: []string{"mkdocsadmonitions", "mention"}, } - actual, _ := mark.CompileMarkdown(markdown, lib, filename, cfg) + actual, _, _ := mark.CompileMarkdown(markdown, lib, filename, cfg) test.EqualValues(strings.TrimSuffix(string(html), "\n"), strings.TrimSuffix(actual, "\n"), filename+" vs "+htmlname) } } @@ -109,7 +109,7 @@ func TestCompileMarkdownDropH1(t *testing.T) { Features: []string{"mkdocsadmonitions", "mention"}, } - actual, _ := mark.CompileMarkdown(markdown, lib, filename, cfg) + actual, _, _ := mark.CompileMarkdown(markdown, lib, filename, cfg) test.EqualValues(strings.TrimSuffix(string(html), "\n"), strings.TrimSuffix(actual, "\n"), filename+" vs "+htmlname) } @@ -153,7 +153,7 @@ func TestCompileMarkdownStripNewlines(t *testing.T) { Features: []string{"mkdocsadmonitions", "mention"}, } - actual, _ := mark.CompileMarkdown(markdown, lib, filename, cfg) + actual, _, _ := mark.CompileMarkdown(markdown, lib, filename, cfg) test.EqualValues(strings.TrimSuffix(string(html), "\n"), strings.TrimSuffix(actual, "\n"), filename+" vs "+htmlname) }