Refactor error handling in file creation

The code responsible for error handling during file creation in the desc_source.go file has been streamlined. This modification simplifies the code by reducing unnecessary condition checks and redundant file closure action after an error has occurred.
This commit is contained in:
Eitol 2024-07-10 11:40:46 -04:00
parent 9706903f6e
commit 21c947e107
1 changed files with 3 additions and 5 deletions

View File

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