fix(http): handle os.Getwd() error in AddStaticFiles#3600
fix(http): handle os.Getwd() error in AddStaticFiles#3600NitinKumar004 wants to merge 3 commits into
Conversation
Previously the error from os.Getwd() was discarded with _, so a failure would leave currentWorkingDir empty and silently resolve the static file path against the filesystem root, with no log to aid debugging. Log the error via the container logger and return early, matching the existing error handling for the os.Stat failure path in the same function. Fixes gofr-dev#3322
aryanmehrotra
left a comment
There was a problem hiding this comment.
Production code is correct — Errorf + return is the right pattern, consistent with the rest of AddStaticFiles. One bug in the test before this can merge.
|
|
||
| // The app (and its logger) must be created inside StderrOutputForFunc so the | ||
| // logger writes to the captured stderr rather than the real one. | ||
| logs := testutil.StderrOutputForFunc(func() { |
There was a problem hiding this comment.
Wrong output stream — GoFr's logger writes to stdout, not stderr. With StderrOutputForFunc the captured string is always empty, so on Linux (where os.Getwd() does fail after os.Remove) both assertions run against "" and the test fails. On macOS the test skips, hiding the bug.
The existing test immediately above this one (TestStaticHandlerInvalidFilePath) exercises the same Errorf call and uses StdoutOutputForFunc — follow that pattern:
logs := testutil.StdoutOutputForFunc(func() {
app := New()
app.AddStaticFiles("gofrTest", "./somedir")
})|
Thanks for the review! I double-checked this against the logger implementation, and I think the stream is actually reversed here. GoFr's logger routes ERROR-level logs to out := l.normalOut // normalOut = os.Stdout
if level >= ERROR {
out = l.errorOut // errorOut = os.Stderr
}
I also verified empirically on Linux (where
So |
My mistake — you're correct. ERROR-level logs go to errorOut = os.Stderr, and TestStaticHandlerInvalidFilePath immediately above already uses StderrOutputForFunc for the same reason. The test is right.
aryanmehrotra
left a comment
There was a problem hiding this comment.
You're right, my mistake. ERROR-level logs route to errorOut = os.Stderr in the logger, so StderrOutputForFunc is correct — and TestStaticHandlerInvalidFilePath right above follows the same pattern. Production code is clean. LGTM.
Description
AddStaticFilesinpkg/gofr/gofr.godiscarded the error returned byos.Getwd():If
os.Getwd()fails,currentWorkingDirbecomes empty and the static file path issilently resolved against the filesystem root, with no log to help the developer diagnose
why the endpoint didn't register correctly.
Change
Capture the error, log it via the container logger, and return early — mirroring the
existing error handling for the
os.Statfailure path a few lines below in the samefunction.
Testing
go build ./pkg/gofr/go vet ./pkg/gofr/go test ./pkg/gofr/ -run 'TestStaticHandler|TestStaticHandlerInvalidFilePath|TestNewSetsHTTPRegisteredWhenStaticDirExists'— all passgolangci-lint run ./pkg/gofr/— no new findings in the changed regionFixes #3322