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:
parent
876d9a9de3
commit
23bde38f6e
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue