]> Cypherpunks repositories - gostls13.git/commitdiff
godoc: report Status 404 if a pkg or file is not found
authorRobert Griesemer <gri@golang.org>
Tue, 10 Aug 2010 16:52:02 +0000 (09:52 -0700)
committerRobert Griesemer <gri@golang.org>
Tue, 10 Aug 2010 16:52:02 +0000 (09:52 -0700)
Fixes #1005.

R=rsc, r
CC=golang-dev
https://golang.org/cl/1935041

src/cmd/godoc/codewalk.go
src/cmd/godoc/godoc.go
src/cmd/godoc/main.go

index 806849c00b1f5e5b10e53debe63f0ca08f6459c7..4e5a9b9da46ada35cc98a857eda2feee3f57b289 100644 (file)
@@ -218,6 +218,7 @@ func codewalkFileprint(c *http.Conn, r *http.Request, f string) {
        abspath := absolutePath(f, *goroot)
        data, err := ioutil.ReadFile(abspath)
        if err != nil {
+               log.Stderr(err)
                serveError(c, r, f, err)
                return
        }
index 9daaacdb3f6919463f0810a86f3c088f356076c7..d08fb5beba1ebfcba4e7d6bbd92a08d1a9be564a 100644 (file)
@@ -1186,7 +1186,6 @@ type PageInfoMode uint
 const (
        exportsOnly PageInfoMode = 1 << iota // only keep exported stuff
        genDoc                               // generate documentation
-       tryMode                              // don't log errors
 )
 
 
@@ -1197,6 +1196,7 @@ type PageInfo struct {
        PDoc    *doc.PackageDoc // nil if no single package documentation
        Dirs    *DirList        // nil if no directory information
        IsPkg   bool            // false if this is not documenting a real package
+       Err     os.Error        // directory read error or nil
 }
 
 
@@ -1210,10 +1210,10 @@ type httpHandler struct {
 // getPageInfo returns the PageInfo for a package directory abspath. If the
 // parameter genAST is set, an AST containing only the package exports is
 // computed (PageInfo.PAst), otherwise package documentation (PageInfo.Doc)
-// is extracted from the AST. If the parameter try is set, no errors are
-// logged if getPageInfo fails. If there is no corresponding package in the
-// directory, PageInfo.PDoc and PageInfo.PExp are nil. If there are no sub-
-// directories, PageInfo.Dirs is nil.
+// is extracted from the AST. If there is no corresponding package in the
+// directory, PageInfo.PAst and PageInfo.PDoc are nil. If there are no sub-
+// directories, PageInfo.Dirs is nil. If a directory read error occured,
+// PageInfo.Err is set to the respective error but the error is not logged.
 //
 func (h *httpHandler) getPageInfo(abspath, relpath, pkgname string, mode PageInfoMode) PageInfo {
        // filter function to select the desired .go files
@@ -1225,9 +1225,10 @@ func (h *httpHandler) getPageInfo(abspath, relpath, pkgname string, mode PageInf
 
        // get package ASTs
        pkgs, err := parser.ParseDir(abspath, filter, parser.ParseComments)
-       if err != nil && mode&tryMode != 0 {
-               // TODO: errors should be shown instead of an empty directory
-               log.Stderrf("parser.parseDir: %s", err)
+       if err != nil && pkgs == nil {
+               // only report directory read errors, ignore parse errors
+               // (may be able to extract partial package information)
+               return PageInfo{Dirname: abspath, Err: err}
        }
 
        // select package
@@ -1314,7 +1315,7 @@ func (h *httpHandler) getPageInfo(abspath, relpath, pkgname string, mode PageInf
                dir = newDirectory(abspath, 1)
        }
 
-       return PageInfo{abspath, plist, past, pdoc, dir.listing(true), h.isPkg}
+       return PageInfo{abspath, plist, past, pdoc, dir.listing(true), h.isPkg, nil}
 }
 
 
@@ -1330,6 +1331,11 @@ func (h *httpHandler) ServeHTTP(c *http.Conn, r *http.Request) {
                mode |= genDoc
        }
        info := h.getPageInfo(abspath, relpath, r.FormValue("p"), mode)
+       if info.Err != nil {
+               log.Stderr(info.Err)
+               serveError(c, r, relpath, info.Err)
+               return
+       }
 
        if r.FormValue("f") == "text" {
                contents := applyTemplate(packageText, "packageText", info)
index 7a9279a2f4261ed5a6ec311b84e09980cdb6ab21..9990c338573ec6c13a87b5bffde4a14be45a29b6 100644 (file)
@@ -66,6 +66,7 @@ var (
 
 func serveError(c *http.Conn, r *http.Request, relpath string, err os.Error) {
        contents := applyTemplate(errorHTML, "errorHTML", err) // err may contain an absolute path!
+       c.WriteHeader(http.StatusNotFound)
        servePage(c, "File "+relpath, "", "", contents)
 }
 
@@ -333,15 +334,18 @@ func main() {
        }
        // TODO(gri): Provide a mechanism (flag?) to select a package
        //            if there are multiple packages in a directory.
-       info := pkgHandler.getPageInfo(abspath, relpath, "", mode|tryMode)
+       info := pkgHandler.getPageInfo(abspath, relpath, "", mode)
 
-       if info.PAst == nil && info.PDoc == nil && info.Dirs == nil {
+       if info.Err != nil || info.PAst == nil && info.PDoc == nil && info.Dirs == nil {
                // try again, this time assume it's a command
                if len(path) > 0 && path[0] != '/' {
                        abspath = absolutePath(path, cmdHandler.fsRoot)
                }
                info = cmdHandler.getPageInfo(abspath, relpath, "", mode)
        }
+       if info.Err != nil {
+               log.Exitf("%v", info.Err)
+       }
 
        // If we have more than one argument, use the remaining arguments for filtering
        if flag.NArg() > 1 {