From 23bde38f6e3d9b8b2214feccccc6ece1997534a1 Mon Sep 17 00:00:00 2001 From: Eitol Date: Wed, 10 Jul 2024 09:05:04 -0400 Subject: [PATCH] Refactor file creation error handling The code for file creation and error handling in desc_source.go has been refactored. Previously, the file closure operation was executed irrespective of whether the file was created successfully or not. Now, the file will only be closed if it was successfully created, improving error handling. --- desc_source.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/desc_source.go b/desc_source.go index 89a7ccc..4f91714 100644 --- a/desc_source.go +++ b/desc_source.go @@ -321,10 +321,13 @@ func WriteProtoFiles(outProtoDirPath string, descSource DescriptorSource, symbol fileName := filepath.Base(fdFQName) filePath := filepath.Join(outFilepath, fileName) f, err := os.Create(filePath) - defer f.Close() if err != nil { + if f != nil { + _ = f.Close() + } return fmt.Errorf("failed to create file %q: %v", filePath, err) } + _ = f.Close() if err := pr.PrintProtoFile(fd, f); err != nil { return fmt.Errorf("failed to write file %q: %v", filePath, err) }