From 7f5dfae904583919a33d29c0a52e03f1573dbfc6 Mon Sep 17 00:00:00 2001 From: iyz Date: Thu, 20 Feb 2025 13:34:34 -0500 Subject: [PATCH] fix: correct Errorf/Fatalf argument handling - Fix incorrect argument passing to log.Errorf and log.Fatalf functions - Remove debugging comments, temporary tests, and print statements - Address linter warnings related to error logging --- error_handler.go | 13 ++++---- main.go | 46 +---------------------------- testdata/batch-tests/broken-test.md | 15 ---------- testdata/batch-tests/irfan-test.md | 10 ------- testdata/batch-tests/mark-test.md | 15 ---------- testdata/batch-tests/rich-test.md | 19 ------------ 6 files changed, 7 insertions(+), 111 deletions(-) delete mode 100644 testdata/batch-tests/broken-test.md delete mode 100644 testdata/batch-tests/irfan-test.md delete mode 100644 testdata/batch-tests/mark-test.md delete mode 100644 testdata/batch-tests/rich-test.md diff --git a/error_handler.go b/error_handler.go index 181b305..03e7b67 100644 --- a/error_handler.go +++ b/error_handler.go @@ -17,19 +17,18 @@ func NewErrorHandler(continueOnError bool) *FatalErrorHandler { } func (h *FatalErrorHandler) Handle(err error, format string, args ...interface{}) { - errorMesage := fmt.Sprintf(format, args...) if err == nil { if h.ContinueOnError { - log.Error(errorMesage) + log.Error(fmt.Sprintf(format, args...)) return } - log.Fatal(errorMesage) + log.Fatal(fmt.Sprintf(format, args...)) } - if h.ContinueOnError { - log.Errorf(err, errorMesage) - return + if h.ContinueOnError { + log.Errorf(err, format, args...) + return } - log.Fatalf(err, errorMesage) + log.Fatalf(err, format, args...) } diff --git a/main.go b/main.go index 5364121..db59ddd 100644 --- a/main.go +++ b/main.go @@ -47,7 +47,7 @@ var flags = []cli.Flag{ altsrc.NewBoolFlag(&cli.BoolFlag{ Name: "continue-on-error", Value: false, - Usage: "dont exit if an error occurs while processing a file, continue processing remaining files.", + Usage: "don't exit if an error occurs while processing a file, continue processing remaining files.", EnvVars: []string{"MARK_CONTINUE_ON_ERROR"}, }), altsrc.NewBoolFlag(&cli.BoolFlag{ @@ -284,9 +284,6 @@ func RunMark(cCtx *cli.Context) error { fatalErrorHandler := NewErrorHandler(cCtx.Bool("continue-on-error")) - fmt.Printf("Processing %d files\n", len(files)) - fmt.Printf("continue-on-error: %t\n", cCtx.Bool("continue-on-error")) - // Loop through files matched by glob pattern for _, file := range files { log.Infof( @@ -321,7 +318,6 @@ func processFile( if err != nil { fatalErrorHandler.Handle(err, "unable to read file %q", file) return nil - // log.Fatal(err) } markdown = bytes.ReplaceAll(markdown, []byte("\r\n"), []byte("\n")) @@ -332,7 +328,6 @@ func processFile( if err != nil { fatalErrorHandler.Handle(err, "unable to extract metadata from file %q", file) return nil - // log.Fatal(err) } if pageID != "" && meta != nil { @@ -347,35 +342,22 @@ func processFile( if pageID == "" && meta == nil { fatalErrorHandler.Handle(nil, "specified file doesn't contain metadata and URL is not specified via command line or doesn't contain pageId GET-parameter") return nil - // log.Fatal( - // `specified file doesn't contain metadata ` + - // `and URL is not specified via command line ` + - // `or doesn't contain pageId GET-parameter`, - // ) } if meta.Space == "" { fatalErrorHandler.Handle(nil, "space is not set ('Space' header is not set and '--space' option is not set)") return nil - // log.Fatal( - // "space is not set ('Space' header is not set and '--space' option is not set)", - // ) } if meta.Title == "" { fatalErrorHandler.Handle(nil, "page title is not set ('Title' header is not set and '--title-from-h1' option and 'h1_title' config is not set or there is no H1 in the file)") return nil - // log.Fatal( - // `page title is not set ('Title' header is not set ` + - // `and '--title-from-h1' option and 'h1_title' config is not set or there is no H1 in the file)`, - // ) } stdlib, err := stdlib.New(api) if err != nil { fatalErrorHandler.Handle(err, "unable to retrieve standard library") return nil - // log.Fatal(err) } templates := stdlib.Templates @@ -392,7 +374,6 @@ func processFile( if err != nil { fatalErrorHandler.Handle(err, "unable to process includes") return nil - // log.Fatal(err) } if !recurse { @@ -409,7 +390,6 @@ func processFile( if err != nil { fatalErrorHandler.Handle(err, "unable to extract macros") return nil - // log.Fatal(err) } macros = append(macros, stdlib.Macros...) @@ -419,7 +399,6 @@ func processFile( if err != nil { fatalErrorHandler.Handle(err, "unable to apply macro") return nil - // log.Fatal(err) } } @@ -427,7 +406,6 @@ func processFile( if err != nil { fatalErrorHandler.Handle(err, "unable to resolve relative links") return nil - // log.Fatalf(err, "unable to resolve relative links") } markdown = page.SubstituteLinks(markdown, links) @@ -437,7 +415,6 @@ func processFile( if err != nil { fatalErrorHandler.Handle(err, "unable to resolve page location") return nil - // log.Fatalf(err, "unable to resolve page location") } } @@ -457,11 +434,6 @@ func processFile( if meta != nil { parent, page, err := page.ResolvePage(cCtx.Bool("dry-run"), api, meta) if err != nil { - // log.Fatalf( - // karma.Describe("title", meta.Title).Reason(err), - // "unable to resolve %s", - // meta.Type, - // ) fatalErrorHandler.Handle(karma.Describe("title", meta.Title).Reason(err), "unable to resolve %s", meta.Type) return nil } @@ -477,12 +449,6 @@ func processFile( if err != nil { fatalErrorHandler.Handle(err, "can't create %s %q", meta.Type, meta.Title) return nil - // log.Fatalf( - // err, - // "can't create %s %q", - // meta.Type, - // meta.Title, - // ) } // (issues/139): A delay between the create and update call // helps mitigate a 409 conflict that can occur when attempting @@ -495,14 +461,12 @@ func processFile( if pageID == "" { fatalErrorHandler.Handle(nil, "URL should provide 'pageId' GET-parameter") return nil - // log.Fatalf(nil, "URL should provide 'pageId' GET-parameter") } page, err := api.GetPageByID(pageID) if err != nil { fatalErrorHandler.Handle(err, "unable to retrieve page by id") return nil - // log.Fatalf(err, "unable to retrieve page by id") } target = page @@ -513,7 +477,6 @@ func processFile( if err != nil { fatalErrorHandler.Handle(err, "unable to locate attachments") return nil - // log.Fatalf(err, "unable to locate attachments") } attaches, err := attachment.ResolveAttachments( @@ -524,7 +487,6 @@ func processFile( if err != nil { fatalErrorHandler.Handle(err, "unable to create/update attachments") return nil - // log.Fatalf(err, "unable to create/update attachments") } markdown = attachment.CompileAttachmentLinks(markdown, attaches) @@ -546,7 +508,6 @@ func processFile( if err != nil { fatalErrorHandler.Handle(err, "unable to create/update attachments") return nil - // log.Fatalf(err, "unable to create/update attachments") } { @@ -568,7 +529,6 @@ func processFile( if err != nil { fatalErrorHandler.Handle(err, "unable to execute layout template") return nil - // log.Fatal(err) } html = buffer.String() @@ -618,7 +578,6 @@ func processFile( if err != nil { fatalErrorHandler.Handle(err, "unable to update page") return nil - // log.Fatal(err) } } @@ -638,7 +597,6 @@ func processFile( if err != nil { fatalErrorHandler.Handle(err, "unable to restrict page updates") return nil - // log.Fatal(err) } } @@ -670,7 +628,6 @@ func updateLabels(api *confluence.API, target *confluence.PageInfo, meta *metada if err != nil { fatalErrorHandler.Handle(err, "error adding labels") return false - // log.Fatal(err) } } @@ -679,7 +636,6 @@ func updateLabels(api *confluence.API, target *confluence.PageInfo, meta *metada if err != nil { fatalErrorHandler.Handle(err, "error deleting labels") return false - // log.Fatal(err) } } return true diff --git a/testdata/batch-tests/broken-test.md b/testdata/batch-tests/broken-test.md deleted file mode 100644 index a37a5d6..0000000 --- a/testdata/batch-tests/broken-test.md +++ /dev/null @@ -1,15 +0,0 @@ -# a - -## b - -### c - -#### d - -##### e - -# f - -## g - -# This/is some_Heading.yml diff --git a/testdata/batch-tests/irfan-test.md b/testdata/batch-tests/irfan-test.md deleted file mode 100644 index 7335254..0000000 --- a/testdata/batch-tests/irfan-test.md +++ /dev/null @@ -1,10 +0,0 @@ - - - - -## Foo - -> **TL;DR:** Thingy! -> More stuff - -Foo diff --git a/testdata/batch-tests/mark-test.md b/testdata/batch-tests/mark-test.md deleted file mode 100644 index a37a5d6..0000000 --- a/testdata/batch-tests/mark-test.md +++ /dev/null @@ -1,15 +0,0 @@ -# a - -## b - -### c - -#### d - -##### e - -# f - -## g - -# This/is some_Heading.yml diff --git a/testdata/batch-tests/rich-test.md b/testdata/batch-tests/rich-test.md deleted file mode 100644 index d3ac661..0000000 --- a/testdata/batch-tests/rich-test.md +++ /dev/null @@ -1,19 +0,0 @@ - - - - -# a - -## b - -### c - -#### d - -##### e - -# f - -## g - -# This/is some_Heading.yml