Skip to content

Commit

Permalink
alternative method of handling errors when batching
Browse files Browse the repository at this point in the history
  • Loading branch information
miniscruff committed Sep 12, 2024
1 parent 0748d73 commit dd089d1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changes/unreleased/fixed-20240912-000012.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: fixed
body: Clean up release notes files if any error takes place during the batch process
time: 2024-09-12T00:00:12.120726403-07:00
custom:
Issue: "714"
34 changes: 20 additions & 14 deletions cmd/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"

Expand Down Expand Up @@ -36,10 +37,9 @@ type Batch struct {
TemplateCache *core.TemplateCache

// Computed values
config *core.Config // current configuration
writer io.Writer // writer we are batching to
version string // the version we are bumping to
versionFilePath string // path to the file we're writing to
config *core.Config // current configuration
writer io.Writer // writer we are batching to
version string // the version we are bumping to
}

func NewBatch(
Expand Down Expand Up @@ -192,9 +192,7 @@ func (b *Batch) getBatchData() (*core.BatchData, error) {
}

//nolint:gocyclo
func (b *Batch) Run(cmd *cobra.Command, args []string) error {
var err error

func (b *Batch) Run(cmd *cobra.Command, args []string) (err error) {
// save our version for later use
b.version = args[0]

Expand Down Expand Up @@ -227,19 +225,28 @@ func (b *Batch) Run(cmd *cobra.Command, args []string) error {
if b.DryRun {
b.writer = cmd.OutOrStdout()
} else {
b.versionFilePath = filepath.Join(b.config.ChangesDir, b.Project, data.Version+"."+b.config.VersionExt)
versionFilePath := filepath.Join(b.config.ChangesDir, b.Project, data.Version+"."+b.config.VersionExt)

if !b.Force {
if exists, existErr := core.FileExists(b.versionFilePath); exists || existErr != nil {
return fmt.Errorf("%w: %v", errVersionExists, b.versionFilePath)
if exists, existErr := core.FileExists(versionFilePath); exists || existErr != nil {
return fmt.Errorf("%w: %v", errVersionExists, versionFilePath)
}
}

versionFile, createErr := os.Create(b.versionFilePath)
versionFile, createErr := os.Create(versionFilePath)
if createErr != nil {
return createErr
}

defer func() {
if err != nil {
removeErr := os.Remove(versionFilePath)
if removeErr != nil {
err = fmt.Errorf("batching error: %w, removing new file error: %w", err, removeErr)
}
}
}()

defer versionFile.Close()
b.writer = versionFile
}
Expand Down Expand Up @@ -361,7 +368,6 @@ func (b *Batch) WriteTemplate(
}

if err := b.TemplateCache.Execute(template, b.writer, templateData); err != nil {
os.Remove(b.versionFilePath)
return err
}

Expand All @@ -385,11 +391,11 @@ func (b *Batch) WriteTemplateFile(
var fileBytes []byte

fileBytes, readErr := os.ReadFile(fullPath)
if readErr != nil && !os.IsNotExist(readErr) {
if readErr != nil && !errors.Is(readErr, fs.ErrNotExist) {
return readErr
}

if os.IsNotExist(readErr) {
if errors.Is(readErr, fs.ErrNotExist) {
return nil
}

Expand Down
4 changes: 4 additions & 0 deletions cmd/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -489,6 +490,9 @@ func TestBatchErrorBadKindFormat(t *testing.T) {

err := batch.Run(batch.Command, []string{"v0.2.3"})
then.NotNil(t, err)

_, err = os.Stat(filepath.Join(cfg.ChangesDir, "v0.2.3.md"))
then.Err(t, fs.ErrNotExist, err)
}

func TestBatchErrorBadComponentFormat(t *testing.T) {
Expand Down

0 comments on commit dd089d1

Please sign in to comment.