Skip to content

Commit 93c8427

Browse files
committed
feat: better error handling
1 parent 7730413 commit 93c8427

File tree

3 files changed

+69
-17
lines changed

3 files changed

+69
-17
lines changed

internal/tex/build.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,21 @@ If no configuration file is provided, it will look for autopdf.yaml in the curre
132132
}
133133
}
134134

135-
// Ensure that the output file exists; if not, create an empty file.
135+
// Ensure that the output file exists and isn't empty
136136
if cfg.Output != "" {
137-
if _, err := os.Stat(cfg.Output.String()); os.IsNotExist(err) {
138-
if err := os.WriteFile(cfg.Output.String(), []byte{}, 0644); err != nil {
139-
return configs.WriteError
137+
// Check if output file exists
138+
fileInfo, err := os.Stat(cfg.Output.String())
139+
if err != nil {
140+
if os.IsNotExist(err) {
141+
log.Printf("Warning: Output file %s doesn't exist after compilation", cfg.Output.String())
142+
// Don't create an empty file here - that would hide the real issue
143+
} else {
144+
log.Printf("Error checking output file: %s", err)
140145
}
146+
} else if fileInfo.Size() == 0 {
147+
log.Printf("Warning: Output file %s exists but is empty", cfg.Output.String())
141148
}
149+
142150
// Use the provided output path
143151
outputPDF = cfg.Output.String()
144152
}

internal/tex/compile.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,25 +73,50 @@ func (c *Compiler) Compile(texFile string) (string, error) {
7373

7474
var cmd *exec.Cmd
7575
// Create command to run
76+
var cmdErr error
77+
7678
if dirOutput == "." {
7779
cmdStr := fmt.Sprintf("%s -interaction=nonstopmode -jobname=%s %s", engine, baseNameOutput, texFile)
7880
cmd = exec.Command("sh", "-c", cmdStr)
7981
log.Printf("Running command: %s", cmd.String())
80-
if err := cmd.Run(); err != nil {
81-
log.Printf("Error running command: %s", err)
82+
cmdErr = cmd.Run()
83+
if cmdErr != nil {
84+
log.Printf("Warning: LaTeX command reported errors: %s", cmdErr)
85+
// Don't return error yet - check if file was created
8286
}
8387
} else {
8488
cmdStr := fmt.Sprintf("%s -interaction=nonstopmode -jobname=%s -output-directory=%s %s", engine, baseNameOutput, dirOutput, texFile)
8589
cmd = exec.Command("sh", "-c", cmdStr)
8690
log.Printf("Running command: %s", cmd.String())
87-
if err := cmd.Run(); err != nil {
88-
log.Printf("Error running command: %s", err)
91+
cmdErr = cmd.Run()
92+
if cmdErr != nil {
93+
log.Printf("Warning: LaTeX command reported errors: %s", cmdErr)
94+
// Don't return error yet - check if file was created
8995
}
9096
}
9197

92-
// Check if output PDF exists
93-
if _, err := os.Stat(fmt.Sprintf("%s.pdf", outputPDF)); os.IsNotExist(err) {
94-
return "", errors.New("PDF output file was not created")
98+
// Normalize output path with .pdf extension
99+
outputPDFWithExt := outputPDF
100+
if !strings.HasSuffix(outputPDF, ".pdf") {
101+
outputPDFWithExt = outputPDF + ".pdf"
102+
}
103+
104+
// Check if output PDF exists and has content
105+
fileInfo, statErr := os.Stat(outputPDFWithExt)
106+
if statErr != nil {
107+
if os.IsNotExist(statErr) {
108+
// File doesn't exist, return the original command error
109+
if cmdErr != nil {
110+
return "", fmt.Errorf("PDF output file was not created: %w", cmdErr)
111+
}
112+
return "", errors.New("PDF output file was not created")
113+
}
114+
return "", fmt.Errorf("error checking output file: %w", statErr)
115+
}
116+
117+
// Check if file is empty
118+
if fileInfo.Size() == 0 {
119+
return "", errors.New("PDF output file was created but is empty")
95120
}
96121

97122
return outputPDF, nil
@@ -188,4 +213,4 @@ var CompileCmd = &bonzai.Cmd{
188213
}
189214
return nil
190215
},
191-
}
216+
}

pkg/api/funcs.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,23 @@ func GeneratePDF(cfg *config.Config, template config.Template) ([]byte, error) {
5252
// Build the pdf using the merged config
5353
err = tex.BuildCmd.Do(nil, cfg.Template.String(), writer.Name())
5454
if err != nil {
55+
// First check if output file exists before trying to read it
56+
if _, statErr := os.Stat(cfg.Output.String()); os.IsNotExist(statErr) {
57+
return nil, err
58+
}
59+
60+
// Now check if file is empty
5561
if futil.FileIsEmpty(cfg.Output.String()) {
5662
return nil, err
5763
}
58-
// Normal to LaTeX to send build verbose info to stderr
59-
// so we need to return the error only if the output file is empty
60-
//
61-
// To print the verbose "error" info, we need to print the stderr
62-
// log.Println("Stderr while building pdf:", err)
64+
65+
// If we get here, file exists and has content despite LaTeX errors
66+
log.Printf("Warning: LaTeX reported errors but a PDF was produced: %v", err)
67+
}
68+
69+
// Verify the file exists before attempting to read it
70+
if _, statErr := os.Stat(cfg.Output.String()); os.IsNotExist(statErr) {
71+
return nil, os.ErrNotExist
6372
}
6473

6574
// Read the generated pdf
@@ -68,6 +77,16 @@ func GeneratePDF(cfg *config.Config, template config.Template) ([]byte, error) {
6877
return nil, err
6978
}
7079

80+
// Verify that the file is not empty and contains valid PDF data
81+
if len(pdfBytes) == 0 {
82+
return nil, os.ErrInvalid
83+
}
84+
85+
// Basic check for PDF header signature
86+
if len(pdfBytes) < 5 || string(pdfBytes[0:5]) != "%PDF-" {
87+
return nil, os.ErrInvalid
88+
}
89+
7190
// Return the generated pdf
7291
return pdfBytes, nil
7392
}

0 commit comments

Comments
 (0)