diff --git a/bin/stitch/export-jpeg.go b/bin/stitch/export-jpeg.go index b719c85..cbdaf2e 100644 --- a/bin/stitch/export-jpeg.go +++ b/bin/stitch/export-jpeg.go @@ -1,16 +1,17 @@ package main import ( + "fmt" "image/jpeg" "log" "os" ) -func exportJPEG(stitchedImage *StitchedImage) error { - log.Printf("Creating output file %q.", *flagOutputPath) - f, err := os.Create(*flagOutputPath) +func exportJPEG(stitchedImage *StitchedImage, outputPath string) error { + log.Printf("Creating output file %q.", outputPath) + f, err := os.Create(outputPath) if err != nil { - log.Panic(err) + return fmt.Errorf("failed to create file: %w", err) } defer f.Close() @@ -19,7 +20,7 @@ func exportJPEG(stitchedImage *StitchedImage) error { } if err := jpeg.Encode(f, stitchedImage, options); err != nil { - log.Panic(err) + return fmt.Errorf("failed to encode image %q: %w", outputPath, err) } return nil diff --git a/bin/stitch/export-png.go b/bin/stitch/export-png.go index cde5d4c..64ccb76 100644 --- a/bin/stitch/export-png.go +++ b/bin/stitch/export-png.go @@ -1,16 +1,17 @@ package main import ( + "fmt" "image/png" "log" "os" ) -func exportPNG(stitchedImage *StitchedImage) error { - log.Printf("Creating output file %q.", *flagOutputPath) - f, err := os.Create(*flagOutputPath) +func exportPNG(stitchedImage *StitchedImage, outputPath string) error { + log.Printf("Creating output file %q.", outputPath) + f, err := os.Create(outputPath) if err != nil { - log.Panic(err) + return fmt.Errorf("failed to create file: %w", err) } defer f.Close() @@ -19,7 +20,7 @@ func exportPNG(stitchedImage *StitchedImage) error { } if err := encoder.Encode(f, stitchedImage); err != nil { - log.Panic(err) + return fmt.Errorf("failed to encode image %q: %w", outputPath, err) } return nil diff --git a/bin/stitch/main.go b/bin/stitch/main.go index 67f25cb..c7a2e61 100644 --- a/bin/stitch/main.go +++ b/bin/stitch/main.go @@ -269,9 +269,13 @@ func main() { fileExtension := strings.ToLower(filepath.Ext(*flagOutputPath)) switch fileExtension { case ".png": - exportPNG(stitchedImage) + if err := exportPNG(stitchedImage, *flagOutputPath); err != nil { + log.Panicf("Export of PNG file failed: %v", err) + } case ".jpg", ".jpeg": - exportJPEG(stitchedImage) + if err := exportJPEG(stitchedImage, *flagOutputPath); err != nil { + log.Panicf("Export of JPEG file failed: %v", err) + } default: log.Panicf("Unknown output format %q.", fileExtension) }