gosrc: handle not a directory error as not found error

We were correctly handling the error where the file did not exist by
mapping it to NotFoundError, which in turn was served as a normal 404
page to users. We were not handling the error where the file did exist
but was not a directory, which led to user-facing 500 errors.

This change fixes that by adding a check that the opened file is a
directory before attempting to read its contents.

Also, the file that was opened via os.Open was never closed. Fix that.

Fixes golang/gddo#640

Change-Id: I2b0cee6431a81294757b6fd24191fdd27f41ef63
Reviewed-on: https://go-review.googlesource.com/c/gddo/+/190397
Reviewed-by: Tuo Shan <shantuo@google.com>
1 file changed