Skip to content

Commit

Permalink
Handle permission (and other) errors during os.Stat(imposters_path)
Browse files Browse the repository at this point in the history
  • Loading branch information
joanlopez committed Jun 13, 2024
1 parent a066757 commit 7ffa57d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
13 changes: 10 additions & 3 deletions internal/server/http/imposter.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,16 @@ type ImposterFs struct {
}

func NewImposterFS(path string) (ImposterFs, error) {
// TODO: What if user lacks permissions?
if _, err := os.Stat(path); os.IsNotExist(err) {
return ImposterFs{}, fmt.Errorf("%w: the directory %s doesn't exists", err, path)
_, err := os.Stat(path)
if err != nil {
switch {
case os.IsNotExist(err):
return ImposterFs{}, fmt.Errorf("the directory '%s' does not exist", path)
case os.IsPermission(err):
return ImposterFs{}, fmt.Errorf("could not read the directory '%s': permission denied", path)
default:
return ImposterFs{}, fmt.Errorf("could not read the directory '%s': %w", path, err)
}
}

return ImposterFs{
Expand Down
2 changes: 1 addition & 1 deletion internal/server/http/imposter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestNewImposterFS(t *testing.T) {
t.Run("imposters directory not found", func(t *testing.T) {
_, err := NewImposterFS("failImposterPath")
assert.Error(t, err)
assert.Contains(t, err.Error(), "the directory failImposterPath doesn't exists")
assert.Contains(t, err.Error(), "the directory 'failImposterPath' does not exist")
})

t.Run("existing imposters directory", func(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions internal/server/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"context"
"crypto/tls"
_ "embed"
"log"
"net/http"

killgrave "github.com/friendsofgo/killgrave/internal"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"log"
"net/http"
)

//go:embed cert/server.key
Expand Down

0 comments on commit 7ffa57d

Please sign in to comment.