Skip to content

Commit 77d9937

Browse files
authored
fix: vite fatals on receiving HTTP4xx (#7306)
* fix: vite fatals on receiving HTTP4xx * tune Vite * fmt * rewrite * fmt
1 parent ed1b33a commit 77d9937

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

coderd/httpmw/httpmw.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func parseUUID(rw http.ResponseWriter, r *http.Request, param string) (uuid.UUID
2626
parsed, err := uuid.Parse(rawID)
2727
if err != nil {
2828
httpapi.Write(r.Context(), rw, http.StatusBadRequest, codersdk.Response{
29-
Message: fmt.Sprintf("Invalid UUID %q.", param),
29+
Message: fmt.Sprintf("Invalid UUID %q.", rawID),
3030
Detail: err.Error(),
3131
})
3232
return uuid.UUID{}, false

coderd/httpmw/httpmw_internal_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package httpmw
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"net/http"
7+
"net/http/httptest"
8+
"testing"
9+
10+
"github.com/go-chi/chi/v5"
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
14+
"github.com/coder/coder/codersdk"
15+
)
16+
17+
const (
18+
testParam = "workspaceagent"
19+
testWorkspaceAgentID = "8a70c576-12dc-42bc-b791-112a32b5bd43"
20+
)
21+
22+
func TestParseUUID_Valid(t *testing.T) {
23+
t.Parallel()
24+
25+
rw := httptest.NewRecorder()
26+
r := httptest.NewRequest("GET", "/{workspaceagent}", nil)
27+
28+
ctx := chi.NewRouteContext()
29+
ctx.URLParams.Add(testParam, testWorkspaceAgentID)
30+
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, ctx))
31+
32+
parsed, ok := parseUUID(rw, r, "workspaceagent")
33+
assert.True(t, ok, "UUID should be parsed")
34+
assert.Equal(t, testWorkspaceAgentID, parsed.String())
35+
}
36+
37+
func TestParseUUID_Invalid(t *testing.T) {
38+
t.Parallel()
39+
40+
rw := httptest.NewRecorder()
41+
r := httptest.NewRequest("GET", "/{workspaceagent}", nil)
42+
43+
ctx := chi.NewRouteContext()
44+
ctx.URLParams.Add(testParam, "wrong-id")
45+
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, ctx))
46+
47+
_, ok := parseUUID(rw, r, "workspaceagent")
48+
assert.False(t, ok, "UUID should not be parsed")
49+
assert.Equal(t, http.StatusBadRequest, rw.Code)
50+
51+
var response codersdk.Response
52+
err := json.Unmarshal(rw.Body.Bytes(), &response)
53+
require.NoError(t, err)
54+
assert.Contains(t, response.Message, `Invalid UUID "wrong-id"`)
55+
}

site/vite.config.ts

+17
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@ export default defineConfig({
3737
changeOrigin: true,
3838
target: process.env.CODER_HOST || "http://localhost:3000",
3939
secure: process.env.NODE_ENV === "production",
40+
configure: (proxy) => {
41+
// Vite does not catch socket errors, and stops the webserver.
42+
// As /startup-logs endpoint can return HTTP 4xx status, we need to embrace
43+
// Vite with a custom error handler to prevent from quitting.
44+
proxy.on("proxyReqWs", (proxyReq, req, socket) => {
45+
if (process.env.NODE_ENV === "development") {
46+
proxyReq.setHeader(
47+
"origin",
48+
process.env.CODER_HOST || "http://localhost:3000",
49+
)
50+
}
51+
52+
socket.on("error", (error) => {
53+
console.error(error)
54+
})
55+
})
56+
},
4057
},
4158
"/swagger": {
4259
target: process.env.CODER_HOST || "http://localhost:3000",

0 commit comments

Comments
 (0)