Skip to content

Commit

Permalink
Refactor writeProtoFile function for better error handling
Browse files Browse the repository at this point in the history
Streamlined the writeProtoFile function in desc_source.go file. Simplified path calculations and improved error messages for file-creation functions, making it easier to trace the exact point of failure and enhance the debugging process.
  • Loading branch information
Eitol committed Jul 12, 2024
1 parent 0bb9027 commit 9901b7a
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions desc_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,22 +320,19 @@ func WriteProtoFiles(outProtoDirPath string, descSource DescriptorSource, symbol
}

func writeProtoFile(outProtoDirPath string, fd *desc.FileDescriptor, pr *protoprint.Printer) error {
fdFQName := fd.GetFullyQualifiedName()
dirPath := filepath.Dir(fdFQName)
outFilepath := filepath.Join(outProtoDirPath, dirPath)
if err := os.MkdirAll(outFilepath, 0755); err != nil {
return fmt.Errorf("failed to create directory %q: %v", outFilepath, err)
}
fileName := filepath.Base(fdFQName)
filePath := filepath.Join(outFilepath, fileName)
outFile := filepath.Join(outProtoDirPath, fd.GetFullyQualifiedName())
outDir := filepath.Dir(outFile)
if err := os.MkdirAll(outDir, 0777); err != nil {
return fmt.Errorf("failed to create directory %q: %w", outDir, err)
}

f, err := os.Create(filePath)
f, err := os.Create(outFile)
if err != nil {
return fmt.Errorf("failed to create proto file: %v", err)
return fmt.Errorf("failed to create proto file %q: %w", outFile, err)
}
defer f.Close()
if err := pr.PrintProtoFile(fd, f); err != nil {
return fmt.Errorf("failed to write proto file: %v", err)
return fmt.Errorf("failed to write proto file %q: %w", outFile, err)
}
return nil
}
Expand Down

0 comments on commit 9901b7a

Please sign in to comment.