{"record":{"id":"0e7f751742c52a8b","repo":"plandex-ai/plandex","slug":"error-creating-sign-in-code","errorCode":null,"errorMessage":"Error creating sign in code: ","messagePattern":"Error creating sign in code: ","errorType":"http","errorClass":"http","httpStatus":500,"severity":"error","filePath":"app/server/handlers/sessions.go","lineNumber":201,"sourceCode":"\t}\n\n\t// create pin - 6 alphanumeric characters\n\tpinBytes, err := shared.GetRandomAlphanumeric(6)\n\tif err != nil {\n\t\tlog.Printf(\"Error generating random pin: %v\\n\", err)\n\t\thttp.Error(w, \"Error generating random pin: \"+err.Error(), http.StatusInternalServerError)\n\t\treturn\n\t}\n\n\t// get sha256 hash of pin\n\thashBytes := sha256.Sum256(pinBytes)\n\tpinHash := hex.EncodeToString(hashBytes[:])\n\n\terr = db.CreateSignInCode(auth.User.Id, auth.OrgId, pinHash)\n\n\tif err != nil {\n\t\tlog.Printf(\"Error creating sign in code: %v\\n\", err)\n\t\thttp.Error(w, \"Error creating sign in code: \"+err.Error(), http.StatusInternalServerError)\n\t\treturn\n\t}\n\n\tlog.Println(\"Successfully created sign in code\")\n\n\t// return the pin as a response\n\tw.Write(pinBytes)\n}\n\nfunc SignInHandler(w http.ResponseWriter, r *http.Request) {\n\tlog.Println(\"Received request for SignInHandler\")\n\n\t// read the request body\n\tbody, err := io.ReadAll(r.Body)\n\tif err != nil {\n\t\tlog.Printf(\"Error reading request body: %v\\n\", err)\n\t\thttp.Error(w, \"Error reading request body: \"+err.Error(), http.StatusInternalServerError)\n\t\treturn","sourceCodeStart":183,"sourceCodeEnd":219,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/handlers/sessions.go#L183-L219","documentation":"This error is returned by CreateSignInCodeHandler when db.CreateSignInCode fails to persist the sign-in code (user id, org id, SHA-256-hashed PIN). The handler responds with HTTP 500 prefixed 'Error creating sign in code: '. The PIN was generated fine but the database write failed, so the code cannot be redeemed.","triggerScenarios":"POST to the sign-in-code endpoint after successful authentication when the DB insert in db.CreateSignInCode fails: database unavailable, constraint violation on user/org, missing table from unmigrated schema, or connection limits.","commonSituations":"Postgres not running or misconfigured env in local development; sign_in_codes table missing after a migration skip; foreign key failures for the org id; database disk or connection exhaustion in production.","solutions":["Check the log for the wrapped underlying DB error","Verify database connectivity and env configuration","Run pending migrations so the sign_in_codes table exists","Check for constraint/foreign-key issues with the user's org id","Retry after the database is healthy"],"exampleFix":"// before\nerr = db.CreateSignInCode(auth.User.Id, auth.OrgId, pinHash)\nif err != nil {\n\thttp.Error(w, \"Error creating sign in code: \"+err.Error(), http.StatusInternalServerError)\n\treturn\n}\n// after: log full context and only retry transient failures\nerr = db.CreateSignInCode(auth.User.Id, auth.OrgId, pinHash)\nif err != nil {\n\tlog.Printf(\"Error creating sign in code (user=%s org=%s): %v\\n\", auth.User.Id, auth.OrgId, err)\n\thttp.Error(w, \"Error creating sign in code: \"+err.Error(), http.StatusInternalServerError)\n\treturn\n}","handlingStrategy":"retry","validationCode":"// confirm backend health before requesting a sign-in code\nresp, err := http.Get(baseURL + \"/health\")\nif err != nil || resp.StatusCode != 200 {\n\treturn fmt.Errorf(\"backend/database unavailable: %v\", err)\n}","typeGuard":null,"tryCatchPattern":"// client: bounded retry on 500 for transient DB issues\nresp, err := http.Post(url, \"application/json\", body)\nif err != nil { return err }\nif resp.StatusCode == http.StatusInternalServerError {\n\ttime.Sleep(2 * time.Second)\n\treturn createSignInCode() // retry once or twice\n}","preventionTips":["Run migrations on every deploy so sign_in_codes exists","Validate auth user/org ids before writing codes","Monitor DB availability and connection limits","Log full context (user id, org id) with the wrapped error"],"tags":["database","http-500","go","sign-in-code"],"backgroundTag":"database-insert-failed","analyzedSha":"e2d772072efadbe41d2946d97d79be55532dbab5","analyzedAt":"2026-09-05T20:56:53.631Z","contentChangedAt":"2026-09-05T20:56:53.631Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}