refactor: replace karma-go with standard error handling

This commit is contained in:
Manuel Rüger
2026-03-28 10:16:29 +01:00
parent e160121005
commit 0859bf4d08
12 changed files with 69 additions and 215 deletions

View File

@@ -5,7 +5,6 @@ import (
"strings"
"github.com/kovetskiy/mark/v16/confluence"
"github.com/reconquest/karma-go"
"github.com/rs/zerolog/log"
)
@@ -22,11 +21,7 @@ func EnsureAncestry(
for i, title := range ancestry {
page, err := api.FindPage(space, title, "page")
if err != nil {
return nil, karma.Format(
err,
`error during finding parent page with title %q`,
title,
)
return nil, fmt.Errorf("error during finding parent page with title %q: %w", title, err)
}
if page == nil {
@@ -44,11 +39,7 @@ func EnsureAncestry(
} else {
page, err := api.FindRootPage(space)
if err != nil {
return nil, karma.Format(
err,
"can't find root page for space %q",
space,
)
return nil, fmt.Errorf("can't find root page for space %q: %w", space, err)
}
parent = page
@@ -68,11 +59,7 @@ func EnsureAncestry(
for _, title := range rest {
page, err := api.CreatePage(space, "page", parent, title, ``)
if err != nil {
return nil, karma.Format(
err,
`error during creating parent page with title %q`,
title,
)
return nil, fmt.Errorf("error during creating parent page with title %q: %w", title, err)
}
parent = page
@@ -108,11 +95,7 @@ func ValidateAncestry(
if len(page.Ancestors) < 1 {
homepage, err := api.FindHomePage(space)
if err != nil {
return nil, karma.Format(
err,
"can't obtain home page from space %q",
space,
)
return nil, fmt.Errorf("can't obtain home page from space %q: %w", space, err)
}
if page.ID == homepage.ID {
@@ -148,10 +131,10 @@ func ValidateAncestry(
}
if !valid {
return nil, karma.Describe("title", page.Title).
Describe("actual", strings.Join(actual, " > ")).
Describe("expected", strings.Join(ancestry, " > ")).
Format(nil, "the page has fewer parents than expected")
return nil, fmt.Errorf(
"the page has fewer parents than expected: title=%q, actual=[%s], expected=[%s]",
page.Title, strings.Join(actual, " > "), strings.Join(ancestry, " > "),
)
}
}
@@ -173,12 +156,10 @@ func ValidateAncestry(
list = append(list, ancestor.Title)
}
return nil, karma.Describe("expected parent", parent).
Describe("list", strings.Join(list, "; ")).
Format(
nil,
"unexpected ancestry tree, did not find expected parent page in the tree",
)
return nil, fmt.Errorf(
"unexpected ancestry tree, did not find expected parent page %q in the tree: actual=[%s]",
parent, strings.Join(list, "; "),
)
}
}

View File

@@ -14,7 +14,6 @@ import (
"github.com/kovetskiy/mark/v16/confluence"
"github.com/kovetskiy/mark/v16/metadata"
"github.com/reconquest/karma-go"
"github.com/rs/zerolog/log"
)
@@ -60,7 +59,7 @@ func ResolveRelativeLinks(
)
resolved, err := resolveLink(api, base, match, spaceForLinks, titleFromH1, titleFromFilename, parents, titleAppendGeneratedHash)
if err != nil {
return nil, karma.Format(err, "resolve link: %q", match.full)
return nil, fmt.Errorf("resolve link %q: %w", match.full, err)
}
if resolved == "" {
@@ -103,7 +102,7 @@ func resolveLink(
linkContents, err := os.ReadFile(filepath)
if err != nil {
return "", karma.Format(err, "read file: %s", filepath)
return "", fmt.Errorf("read file %s: %w", filepath, err)
}
contentType := http.DetectContentType(linkContents)
@@ -146,13 +145,7 @@ func resolveLink(
result, err = getConfluenceLink(api, linkMeta.Space, linkMeta.Title)
if err != nil {
return "", karma.Format(
err,
"find confluence page: %s / %s / %s",
filepath,
linkMeta.Space,
linkMeta.Title,
)
return "", fmt.Errorf("find confluence page (file=%s, space=%s, title=%s): %w", filepath, linkMeta.Space, linkMeta.Title, err)
}
if result == "" {
@@ -217,14 +210,14 @@ func getConfluenceLink(
// Try to find as a page first
page, err := api.FindPage(space, title, "page")
if err != nil {
return "", karma.Format(err, "api: find page")
return "", fmt.Errorf("api: find page %q in space %q: %w", title, space, err)
}
// If not found as a page, try to find as a blog post
if page == nil {
page, err = api.FindPage(space, title, "blogpost")
if err != nil {
return "", karma.Format(err, "api: find blogpost")
return "", fmt.Errorf("api: find blogpost %q in space %q: %w", title, space, err)
}
}
@@ -242,7 +235,7 @@ func getConfluenceLink(
tiny, err := GenerateTinyLink(baseURL, page.ID)
if err != nil {
return "", karma.Format(err, "generate tiny link for page %s", page.ID)
return "", fmt.Errorf("generate tiny link for page %s: %w", page.ID, err)
}
return tiny, nil

View File

@@ -1,11 +1,11 @@
package page
import (
"fmt"
"strings"
"github.com/kovetskiy/mark/v16/confluence"
"github.com/kovetskiy/mark/v16/metadata"
"github.com/reconquest/karma-go"
"github.com/rs/zerolog/log"
)
@@ -15,15 +15,11 @@ func ResolvePage(
meta *metadata.Meta,
) (*confluence.PageInfo, *confluence.PageInfo, error) {
if meta == nil {
return nil, nil, karma.Format(nil, "metadata is empty")
return nil, nil, fmt.Errorf("metadata is empty")
}
page, err := api.FindPage(meta.Space, meta.Title, meta.Type)
if err != nil {
return nil, nil, karma.Format(
err,
"error while finding page %q",
meta.Title,
)
return nil, nil, fmt.Errorf("error while finding page %q: %w", meta.Title, err)
}
if meta.Type == "blogpost" {
@@ -39,11 +35,7 @@ func ResolvePage(
// check to see if home page is in Parents
homepage, err := api.FindHomePage(meta.Space)
if err != nil {
return nil, nil, karma.Format(
err,
"can't obtain home page from space %q",
meta.Space,
)
return nil, nil, fmt.Errorf("can't obtain home page from space %q: %w", meta.Space, err)
}
skipHomeAncestry := false
@@ -93,11 +85,7 @@ func ResolvePage(
meta.Parents,
)
if err != nil {
return nil, nil, karma.Format(
err,
"can't create ancestry tree: %s",
strings.Join(meta.Parents, ` > `),
)
return nil, nil, fmt.Errorf("can't create ancestry tree %q: %w", strings.Join(meta.Parents, ` > `), err)
}
titles := []string{}