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.
This commit is contained in:
Eitol 2024-07-10 09:05:04 -04:00
parent 876d9a9de3
commit 23bde38f6e
1 changed files with 4 additions and 1 deletions

View File

@ -321,10 +321,13 @@ func WriteProtoFiles(outProtoDirPath string, descSource DescriptorSource, symbol
fileName := filepath.Base(fdFQName) fileName := filepath.Base(fdFQName)
filePath := filepath.Join(outFilepath, fileName) filePath := filepath.Join(outFilepath, fileName)
f, err := os.Create(filePath) f, err := os.Create(filePath)
defer f.Close()
if err != nil { if err != nil {
if f != nil {
_ = f.Close()
}
return fmt.Errorf("failed to create file %q: %v", filePath, err) return fmt.Errorf("failed to create file %q: %v", filePath, err)
} }
_ = f.Close()
if err := pr.PrintProtoFile(fd, f); err != nil { if err := pr.PrintProtoFile(fd, f); err != nil {
return fmt.Errorf("failed to write file %q: %v", filePath, err) return fmt.Errorf("failed to write file %q: %v", filePath, err)
} }