{"record":{"id":"9c4b06b1f561edcc","repo":"charmbracelet/crush","slug":"failed-to-create-session-status-code-d","errorCode":null,"errorMessage":"failed to create session: status code %d","messagePattern":"failed to create session: status code (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/client/proto.go","lineNumber":649,"sourceCode":"\tif rsp.StatusCode != http.StatusOK {\n\t\treturn nil, fmt.Errorf(\"failed to get session history files: status code %d\", rsp.StatusCode)\n\t}\n\tvar files []proto.File\n\tif err := json.NewDecoder(rsp.Body).Decode(&files); err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to decode session history files: %w\", err)\n\t}\n\treturn files, nil\n}\n\n// CreateSession creates a new session in a workspace as a proto type.\nfunc (c *Client) CreateSession(ctx context.Context, id string, title string) (*proto.Session, error) {\n\trsp, err := c.post(ctx, fmt.Sprintf(\"/workspaces/%s/sessions\", id), nil, jsonBody(proto.Session{Title: title}), http.Header{\"Content-Type\": []string{\"application/json\"}})\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to create session: %w\", err)\n\t}\n\tdefer rsp.Body.Close()\n\tif rsp.StatusCode != http.StatusOK {\n\t\treturn nil, fmt.Errorf(\"failed to create session: status code %d\", rsp.StatusCode)\n\t}\n\tvar sess proto.Session\n\tif err := json.NewDecoder(rsp.Body).Decode(&sess); err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to decode session: %w\", err)\n\t}\n\treturn &sess, nil\n}\n\n// ListSessions lists all sessions in a workspace as proto types.\nfunc (c *Client) ListSessions(ctx context.Context, id string) ([]proto.Session, error) {\n\trsp, err := c.get(ctx, fmt.Sprintf(\"/workspaces/%s/sessions\", id), nil, nil)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to get sessions: %w\", err)\n\t}\n\tdefer rsp.Body.Close()\n\tif rsp.StatusCode != http.StatusOK {\n\t\treturn nil, fmt.Errorf(\"failed to get sessions: status code %d\", rsp.StatusCode)\n\t}","sourceCodeStart":631,"sourceCodeEnd":667,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/client/proto.go#L631-L667","documentation":"CreateSession reached the server but received a non-200 status code for the POST to /workspaces/{id}/sessions. The status code is embedded in the error. Common codes: 400 (invalid title/payload), 401/403 (auth), 404 (workspace not found), 409/422 (validation), 5xx (server error).","triggerScenarios":"POST /workspaces/{id}/sessions returns 400 for an empty or invalid title, 401/403 for bad credentials, 404 for a nonexistent workspace ID, or 5xx on server failure.","commonSituations":"Creating a session with an empty title; using a workspace ID that does not exist; expired auth token; server rejecting the payload due to schema changes.","solutions":["Read the status code from the error and branch on it (404: fix workspace ID; 401/403: refresh credentials; 400: validate title).","Ensure the title is non-empty and within server limits.","Verify the workspace ID exists via the API.","Re-authenticate if credentials expired.","Retry with backoff on 429/5xx."],"exampleFix":"// before\nsess, err := client.CreateSession(ctx, wsID, title)\nif err != nil { return err }\n// after\nif title == \"\" { return errors.New(\"session title must not be empty\") }\nsess, err := client.CreateSession(ctx, wsID, title)\nif err != nil {\n    if strings.Contains(err.Error(), \"status code 404\") {\n        return fmt.Errorf(\"workspace %q not found\", wsID)\n    }\n    return err\n}","handlingStrategy":"validation","validationCode":"if strings.TrimSpace(title) == \"\" {\n    return errors.New(\"session title must not be empty\")\n}\nif _, err := client.GetWorkspace(ctx, wsID); err != nil {\n    return fmt.Errorf(\"workspace %q invalid: %w\", wsID, err)\n}","typeGuard":"func isCreateStatusError(err error, code int) bool {\n    return err != nil && strings.Contains(err.Error(), \"failed to create session: status code \"+strconv.Itoa(code))\n}","tryCatchPattern":"sess, err := client.CreateSession(ctx, wsID, title)\nif err != nil {\n    switch {\n    case isCreateStatusError(err, 400):\n        return fmt.Errorf(\"invalid session payload (title=%q)\", title)\n    case isCreateStatusError(err, 404):\n        return fmt.Errorf(\"workspace %q not found\", wsID)\n    case isCreateStatusError(err, 401), isCreateStatusError(err, 403):\n        return fmt.Errorf(\"re-authenticate: %w\", err)\n    }\n    return err\n}","preventionTips":["Validate title length/content client-side.","Confirm the workspace exists before creating sessions.","Keep auth tokens fresh.","Branch on the embedded status code."],"tags":["http","status-code","client"],"backgroundTag":"http-non-200-response","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}