mirror of
https://github.com/coder/coder.git
synced 2025-07-15 22:20:27 +00:00
fix: improve error messages when the agent token is invalid (#5423)
I'm not sure why this issue is common, but it seems to be based on: https://github.com/coder/coder/issues/4551. This improves the error messages to be unique, and also fixes a small edge-case bug a user ran into.
This commit is contained in:
@ -30,17 +30,18 @@ func ExtractWorkspaceAgent(db database.Store) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
cookieValue := apiTokenFromRequest(r)
|
||||
if cookieValue == "" {
|
||||
tokenValue := apiTokenFromRequest(r)
|
||||
if tokenValue == "" {
|
||||
httpapi.Write(ctx, rw, http.StatusUnauthorized, codersdk.Response{
|
||||
Message: fmt.Sprintf("Cookie %q must be provided.", codersdk.SessionTokenKey),
|
||||
})
|
||||
return
|
||||
}
|
||||
token, err := uuid.Parse(cookieValue)
|
||||
token, err := uuid.Parse(tokenValue)
|
||||
if err != nil {
|
||||
httpapi.Write(ctx, rw, http.StatusUnauthorized, codersdk.Response{
|
||||
Message: "Agent token is invalid.",
|
||||
Message: "Workspace agent token invalid.",
|
||||
Detail: fmt.Sprintf("An agent token must be a valid UUIDv4. (len %d)", len(tokenValue)),
|
||||
})
|
||||
return
|
||||
}
|
||||
@ -48,7 +49,8 @@ func ExtractWorkspaceAgent(db database.Store) func(http.Handler) http.Handler {
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
httpapi.Write(ctx, rw, http.StatusUnauthorized, codersdk.Response{
|
||||
Message: "Agent token is invalid.",
|
||||
Message: "Workspace agent not authorized.",
|
||||
Detail: "The agent cannot authenticate until the workspace provision job has been completed. If the job is no longer running, this agent is invalid.",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user