Skip to content

Commit

Permalink
go server, wrap internal error
Browse files Browse the repository at this point in the history
* Do not assert on system error messages
  • Loading branch information
BuJo committed Sep 30, 2024
1 parent e59a839 commit 7b549bb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pkg/project/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (fr *FilesystemRepository) imageFile(slug, filename string) string {
func (fr *FilesystemRepository) Exists(slug string) (bool, error) {
dir, err := os.Stat(fr.ProjectDir)
if err != nil {
return false, err
return false, fmt.Errorf("project directory %s not accessible: %w", fr.ProjectDir, err)
}
if !dir.IsDir() {
return false, fmt.Errorf("project directory %s is not a directory", fr.ProjectDir)
Expand Down
18 changes: 9 additions & 9 deletions pkg/web/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestAPI_createProject(t *testing.T) {

resp := tc.Request("POST", "/api/tales/", project.Project{Slug: "foo"})

AssertError(t, resp, "stat "+tc.dir+": no such file or directory\n", 500)
AssertError(t, resp, tc.dir+" not accessible", 500)
})
}

Expand Down Expand Up @@ -136,7 +136,7 @@ func TestAPI_loadProject(t *testing.T) {

resp := tc.Request("GET", "/api/tales/foo", nil)

AssertError(t, resp, "stat "+tc.dir+": no such file or directory\n", 500)
AssertError(t, resp, tc.dir+" not accessible", 500)
})
}

Expand All @@ -158,7 +158,7 @@ func TestAPI_updateProject(t *testing.T) {

resp := tc.Request("PUT", "/api/tales/foo", nil)

AssertError(t, resp, "invalid project\n", 400)
AssertError(t, resp, "invalid project", 400)
})
t.Run("unknown project", func(t *testing.T) {
tc := NewTestClient(t)
Expand All @@ -175,7 +175,7 @@ func TestAPI_updateProject(t *testing.T) {

resp := tc.Request("PUT", "/api/tales/foo", project.Project{Slug: "foo"})

AssertError(t, resp, "stat "+tc.dir+": no such file or directory\n", 500)
AssertError(t, resp, tc.dir+" not accessible", 500)
})
}

Expand All @@ -199,15 +199,15 @@ func TestAPI_deleteProject(t *testing.T) {

resp := tc.Request("DELETE", "/api/tales/foo", nil)

AssertError(t, resp, "project not found\n", 404)
AssertError(t, resp, "project not found", 404)
})
t.Run("internal error", func(t *testing.T) {
tc := NewTestClient(t)
tc.Cleanup()

resp := tc.Request("DELETE", "/api/tales/foo", nil)

AssertError(t, resp, "stat "+tc.dir+": no such file or directory\n", 500)
AssertError(t, resp, tc.dir+" not accessible", 500)
})
}

Expand All @@ -234,15 +234,15 @@ func TestAPI_updateProjectImage(t *testing.T) {

resp := tc.Request("PUT", "/api/tales/foo/image", nil)

AssertError(t, resp, "unsupported content-type: \n", 500)
AssertError(t, resp, "unsupported content-type", 500)
})
t.Run("unknown project", func(t *testing.T) {
tc := NewTestClient(t)
defer tc.Cleanup()

resp := tc.Request("PUT", "/api/tales/foo/image", nil)

AssertError(t, resp, "project not found\n", 404)
AssertError(t, resp, "project not found", 404)
})
}

Expand All @@ -259,7 +259,7 @@ func AssertProjectResponse(t *testing.T, resp *http.Response, expected project.P
func AssertError(t *testing.T, resp *http.Response, errorMsg string, statusCode int) {
AssertHTTPError(t, resp, statusCode)
body, _ := io.ReadAll(resp.Body)
assert.Equal(t, errorMsg, string(body))
assert.Contains(t, string(body), errorMsg)
}

func AssertHTTPError(t *testing.T, resp *http.Response, statusCode int) {
Expand Down

0 comments on commit 7b549bb

Please sign in to comment.