panics -> errors with explanation

This commit is contained in:
Kirill Zhuravlev 2023-02-14 23:10:50 +01:00 committed by Avelino
parent 8e90349cbe
commit a2d1d19caf
No known key found for this signature in database
GPG Key ID: B345B4D52E98180A

24
main.go
View File

@ -50,30 +50,37 @@ var outIndexFile = filepath.Join(outDir, "index.html")
var outSitemapFile = filepath.Join(outDir, "sitemap.xml") var outSitemapFile = filepath.Join(outDir, "sitemap.xml")
func main() { func main() {
if err := renderAll(); err != nil {
panic(err)
}
}
// FIXME: choose a better name
func renderAll() error {
// Cleanup and re-create output directory // Cleanup and re-create output directory
{ {
if err := os.RemoveAll(outDir); err != nil { if err := os.RemoveAll(outDir); err != nil {
panic(err) return fmt.Errorf("unable to remove target dir: %w", err)
} }
if err := mkdirAll(outDir); err != nil { if err := mkdirAll(outDir); err != nil {
panic(err) return fmt.Errorf("unable to create target dir: %w", err)
} }
} }
err := ConvertAndRenderIndex(readmePath, outIndexFile) err := ConvertAndRenderIndex(readmePath, outIndexFile)
if err != nil { if err != nil {
panic(err) return fmt.Errorf("unable to convert markdown to html: %w", err)
} }
input, err := os.ReadFile(outIndexFile) input, err := os.ReadFile(outIndexFile)
if err != nil { if err != nil {
panic(err) return fmt.Errorf("unable to read converted html: %w", err)
} }
query, err := goquery.NewDocumentFromReader(bytes.NewReader(input)) query, err := goquery.NewDocumentFromReader(bytes.NewReader(input))
if err != nil { if err != nil {
panic(err) return fmt.Errorf("unable to create goquery instance: %w", err)
} }
objs := make(map[string]Object) objs := make(map[string]Object)
@ -83,6 +90,7 @@ func main() {
if !exists { if !exists {
return return
} }
obj, err := makeObjByID(selector, query.Find("body")) obj, err := makeObjByID(selector, query.Find("body"))
if err != nil { if err != nil {
return return
@ -94,7 +102,7 @@ func main() {
if err := makeSiteStruct(objs); err != nil { if err := makeSiteStruct(objs); err != nil {
// FIXME: remove all panics // FIXME: remove all panics
panic(err) return fmt.Errorf("unable to render categories: %w", err)
} }
changeLinksInIndex(string(input), query, objs) changeLinksInIndex(string(input), query, objs)
@ -104,9 +112,11 @@ func main() {
dstFilename := filepath.Join(outDir, filepath.Base(srcFilename)) dstFilename := filepath.Join(outDir, filepath.Base(srcFilename))
fmt.Printf("Copy static file: %s -> %s\n", srcFilename, dstFilename) fmt.Printf("Copy static file: %s -> %s\n", srcFilename, dstFilename)
if err := cp.Copy(srcFilename, dstFilename); err != nil { if err := cp.Copy(srcFilename, dstFilename); err != nil {
panic(err) return fmt.Errorf("unable to copy static file `%s` to `%s`: %w", srcFilename, dstFilename, err)
} }
} }
return nil
} }
func mkdirAll(path string) error { func mkdirAll(path string) error {