mirror of
https://github.com/coder/coder.git
synced 2025-07-09 11:45:56 +00:00
fix: vite fatals on receiving HTTP4xx (#7306)
* fix: vite fatals on receiving HTTP4xx * tune Vite * fmt * rewrite * fmt
This commit is contained in:
@ -26,7 +26,7 @@ func parseUUID(rw http.ResponseWriter, r *http.Request, param string) (uuid.UUID
|
|||||||
parsed, err := uuid.Parse(rawID)
|
parsed, err := uuid.Parse(rawID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpapi.Write(r.Context(), rw, http.StatusBadRequest, codersdk.Response{
|
httpapi.Write(r.Context(), rw, http.StatusBadRequest, codersdk.Response{
|
||||||
Message: fmt.Sprintf("Invalid UUID %q.", param),
|
Message: fmt.Sprintf("Invalid UUID %q.", rawID),
|
||||||
Detail: err.Error(),
|
Detail: err.Error(),
|
||||||
})
|
})
|
||||||
return uuid.UUID{}, false
|
return uuid.UUID{}, false
|
||||||
|
55
coderd/httpmw/httpmw_internal_test.go
Normal file
55
coderd/httpmw/httpmw_internal_test.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package httpmw
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/coder/coder/codersdk"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
testParam = "workspaceagent"
|
||||||
|
testWorkspaceAgentID = "8a70c576-12dc-42bc-b791-112a32b5bd43"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseUUID_Valid(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
rw := httptest.NewRecorder()
|
||||||
|
r := httptest.NewRequest("GET", "/{workspaceagent}", nil)
|
||||||
|
|
||||||
|
ctx := chi.NewRouteContext()
|
||||||
|
ctx.URLParams.Add(testParam, testWorkspaceAgentID)
|
||||||
|
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, ctx))
|
||||||
|
|
||||||
|
parsed, ok := parseUUID(rw, r, "workspaceagent")
|
||||||
|
assert.True(t, ok, "UUID should be parsed")
|
||||||
|
assert.Equal(t, testWorkspaceAgentID, parsed.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseUUID_Invalid(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
rw := httptest.NewRecorder()
|
||||||
|
r := httptest.NewRequest("GET", "/{workspaceagent}", nil)
|
||||||
|
|
||||||
|
ctx := chi.NewRouteContext()
|
||||||
|
ctx.URLParams.Add(testParam, "wrong-id")
|
||||||
|
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, ctx))
|
||||||
|
|
||||||
|
_, ok := parseUUID(rw, r, "workspaceagent")
|
||||||
|
assert.False(t, ok, "UUID should not be parsed")
|
||||||
|
assert.Equal(t, http.StatusBadRequest, rw.Code)
|
||||||
|
|
||||||
|
var response codersdk.Response
|
||||||
|
err := json.Unmarshal(rw.Body.Bytes(), &response)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Contains(t, response.Message, `Invalid UUID "wrong-id"`)
|
||||||
|
}
|
@ -37,6 +37,23 @@ export default defineConfig({
|
|||||||
changeOrigin: true,
|
changeOrigin: true,
|
||||||
target: process.env.CODER_HOST || "http://localhost:3000",
|
target: process.env.CODER_HOST || "http://localhost:3000",
|
||||||
secure: process.env.NODE_ENV === "production",
|
secure: process.env.NODE_ENV === "production",
|
||||||
|
configure: (proxy) => {
|
||||||
|
// Vite does not catch socket errors, and stops the webserver.
|
||||||
|
// As /startup-logs endpoint can return HTTP 4xx status, we need to embrace
|
||||||
|
// Vite with a custom error handler to prevent from quitting.
|
||||||
|
proxy.on("proxyReqWs", (proxyReq, req, socket) => {
|
||||||
|
if (process.env.NODE_ENV === "development") {
|
||||||
|
proxyReq.setHeader(
|
||||||
|
"origin",
|
||||||
|
process.env.CODER_HOST || "http://localhost:3000",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
socket.on("error", (error) => {
|
||||||
|
console.error(error)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
},
|
||||||
},
|
},
|
||||||
"/swagger": {
|
"/swagger": {
|
||||||
target: process.env.CODER_HOST || "http://localhost:3000",
|
target: process.env.CODER_HOST || "http://localhost:3000",
|
||||||
|
Reference in New Issue
Block a user