{"record":{"id":"f7b489e209b4b03c","repo":"gotify/server","slug":"could-not-get-user-s","errorCode":null,"errorMessage":"could not get user: %s","messagePattern":"could not get user: (.+?)","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"api/user.go","lineNumber":215,"sourceCode":"\t\t\tctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf(\"failed to prepare password: %s\", err))\n\t\t\treturn\n\t\t}\n\t\tinternal := &model.User{\n\t\t\tName:  user.Name,\n\t\t\tAdmin: user.Admin,\n\t\t\tPass:  pw,\n\t\t}\n\t\texistingUser, err := a.DB.GetUserByName(internal.Name)\n\t\tif success := successOrAbort(ctx, 500, err); !success {\n\t\t\treturn\n\t\t}\n\n\t\tvar requestedBy *model.User\n\t\tuid := auth.TryGetUserID(ctx)\n\t\tif uid != nil {\n\t\t\trequestedBy, err = a.DB.GetUserByID(*uid)\n\t\t\tif err != nil {\n\t\t\t\tctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf(\"could not get user: %s\", err))\n\t\t\t\treturn\n\t\t\t}\n\t\t}\n\n\t\tif requestedBy == nil || !requestedBy.Admin {\n\t\t\tstatus := http.StatusUnauthorized\n\t\t\tif requestedBy != nil {\n\t\t\t\tstatus = http.StatusForbidden\n\t\t\t}\n\t\t\tif !a.Registration {\n\t\t\t\tctx.AbortWithError(status, errors.New(\"you are not allowed to access this api\"))\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif internal.Admin {\n\t\t\t\tctx.AbortWithError(status, errors.New(\"you are not allowed to create an admin user\"))\n\t\t\t\treturn\n\t\t\t}\n\t\t}","sourceCodeStart":197,"sourceCodeEnd":233,"githubUrl":"https://github.com/gotify/server/blob/14bfc256276775c425f988d621dccfe705de18ac/api/user.go#L197-L233","documentation":"During CreateUser the handler resolves the acting user via auth.TryGetUserID and a.DB.GetUserByID to decide authorization (admin vs non-admin vs anonymous). If the user ID from the auth context cannot be fetched from the database, the request is aborted with a 500 and \"could not get user: <err>\". This signals a stale/invalid session identity or a database failure.","triggerScenarios":"Any authenticated CreateUser request whose token/session user ID no longer resolves in the DB (user deleted after token issuance, DB read error, wrong DB backend), or where GetUserByID returns an error.","commonSituations":"Tokens issued before a database restore/migration; user removed while a session was still valid; database connectivity problems or a misconfigured DB driver.","solutions":["Re-authenticate to obtain a fresh token bound to an existing user.","Verify the referenced user still exists in the database (check the users table for the ID).","Check DB connectivity/logs for the underlying error appended after 'could not get user:'.","If migrating/restoring data, regenerate or reconcile sessions and foreign keys."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if resp.StatusCode == http.StatusInternalServerError && strings.Contains(body, \"could not get user\") {\n    // identity no longer resolvable: clear session and re-authenticate\n    session.Invalidate()\n    return ErrReauthRequired\n}","preventionTips":["Re-authenticate after user deletion or DB restore instead of reusing old tokens.","Monitor DB health; treat 500s with user-lookup messages as DB issues.","Clean up sessions/tokens when users are removed."],"tags":["database","auth","http-500"],"backgroundTag":"user-lookup-failed","analyzedSha":"14bfc256276775c425f988d621dccfe705de18ac","analyzedAt":"2026-09-05T12:52:36.781Z","contentChangedAt":"2026-09-05T12:52:36.781Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}