Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions coderd/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func (api *api) projects(rw http.ResponseWriter, r *http.Request) {
})
return
}
// Don't return 'null'
if projects == nil {
projects = []database.Project{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather us to this inside the sql.ErrNoRows like before, and depend on tests to catch the incorrect behavior.

I'm worried about a developer making a mistake for something else, and this masking the error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this change is also already in main with #166 🎉

}
render.Status(r, http.StatusOK)
render.JSON(rw, r, projects)
}
Expand All @@ -66,6 +70,10 @@ func (api *api) projectsByOrganization(rw http.ResponseWriter, r *http.Request)
})
return
}
// Don't return 'null'
if projects == nil {
projects = []database.Project{}
}
render.Status(r, http.StatusOK)
render.JSON(rw, r, projects)
}
Expand Down
2 changes: 2 additions & 0 deletions coderd/projects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestProjects(t *testing.T) {
_ = server.RandomInitialUser(t)
projects, err := server.Client.Projects(context.Background(), "")
require.NoError(t, err)
require.NotNil(t, projects)
require.Len(t, projects, 0)
})

Expand Down Expand Up @@ -77,6 +78,7 @@ func TestProjects(t *testing.T) {

projects, err := server.Client.Projects(context.Background(), user.Organization)
require.NoError(t, err)
require.NotNil(t, projects)
require.Len(t, projects, 0)
})

Expand Down
6 changes: 1 addition & 5 deletions site/pages/projects/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@ const ProjectsPage: React.FC = () => {
const styles = useStyles()
const router = useRouter()
const { me, signOut } = useUser(true)
const { data, error } = useSWR<Project[] | null, Error>("/api/v2/projects")
const { data: projects, error } = useSWR<Project[] | null, Error>("/api/v2/projects")
const { data: orgs, error: orgsError } = useSWR<Organization[], Error>("/api/v2/users/me/organizations")

// TODO: The API call is currently returning `null`, which isn't ideal
// - it breaks checking for data presence with SWR.
const projects = data || []

if (error) {
return <ErrorSummary error={error} />
}
Expand Down