{"record":{"id":"6df15308dac1d1dd","repo":"Kareadita/Kavita","slug":"errors-oidc-creating-user","errorCode":null,"errorMessage":"errors.oidc.creating-user","messagePattern":"errors\\.oidc\\.creating-user","errorType":"exception","errorClass":"KavitaException","httpStatus":null,"severity":"error","filePath":"Kavita.Services/OidcService.cs","lineNumber":229,"sourceCode":"\n        if (settings.SyncUserSettings && !isAllowedToBeCreated)\n        {\n            logger.LogDebug(\"Login role was not found under claim {Claim} with prefix {Prefix}\", settings.RolesClaim, settings.RolesPrefix);\n            throw new KavitaException(\"errors.oidc.role-not-assigned\");\n        }\n\n        try\n        {\n            return await NewUserFromOpenIdConnect(request, settings, principal, oidcId);\n        }\n        catch (KavitaException)\n        {\n            throw;\n        }\n        catch (Exception e)\n        {\n            logger.LogError(e, \"An error occured creating a new user\");\n            throw new KavitaException(\"errors.oidc.creating-user\");\n        }\n\n    }\n\n    /// <summary>\n    /// Find the best available name from claims\n    /// </summary>\n    /// <param name=\"claimsPrincipal\"></param>\n    /// <param name=\"orEqualTo\">Also return if the claim is equal to this value</param>\n    /// <returns></returns>\n    public async Task<string?> FindBestAvailableName(ClaimsPrincipal claimsPrincipal, string? orEqualTo = null)\n    {\n        var nameCandidates = new[]\n        {\n            claimsPrincipal.FindFirstValue(JwtRegisteredClaimNames.PreferredUsername),\n            claimsPrincipal.FindFirstValue(ClaimTypes.Name),\n            claimsPrincipal.FindFirstValue(ClaimTypes.GivenName),\n            claimsPrincipal.FindFirstValue(ClaimTypes.Surname)","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/Kareadita/Kavita/blob/9c3e5400007f8a0282f7d883f2ad5e71716e514d/Kavita.Services/OidcService.cs#L211-L247","documentation":"Thrown by CreateNewAccount's catch-all: NewUserFromOpenIdConnect threw an exception that was not itself a KavitaException (those are rethrown verbatim). Any unexpected failure during account creation — DB error, Identity manager fault, missing dependency — is normalized to this generic message. The original exception is logged via LogError before the throw.","triggerScenarios":"Any non-KavitaException inside NewUserFromOpenIdConnect during first-time OIDC provisioning: userManager.CreateAsync infra failure, DB constraint violation, null theme from GetDefaultTheme, SeedUser throwing an unexpected exception type.","commonSituations":"DB unreachable or migration pending; unique-constraint collision (username/email race); default site theme missing; transient DB timeout during account creation.","solutions":["Read the preceding LogError entry — it logs the real exception (e) with 'An error occured creating a new user'.","Resolve the underlying cause (DB connectivity, migration, constraint) identified in that log.","If it's a race on duplicate username/email, ensure the principal's claims are unique before retry.","Confirm GetDefaultTheme returns a non-null theme (seed themes / migrations applied)."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { var user = await oidcService.LoginOrCreate(Request, principal, ct); }\ncatch (KavitaException ex) when (ex.Message == \"errors.oidc.creating-user\")\n{\n    // The real exception is logged; surface a generic 'account creation failed, contact admin' message.\n    return StatusCode(500, \"Unable to create account. Check server logs.\");\n}","preventionTips":["Monitor logs for 'An error occured creating a new user' — it carries the real stack trace.","Keep the DB reachable and migrations applied before enabling OIDC.","Ensure the default site theme exists (GetDefaultTheme non-null)."],"tags":["oidc","account-creation","identity","error-wrapping"],"backgroundTag":null,"analyzedSha":"9c3e5400007f8a0282f7d883f2ad5e71716e514d","analyzedAt":"2026-08-13T19:06:05.897Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}