{"record":{"id":"deb059ba301dec94","repo":"dotnet/aspnetcore","slug":"the-hubconnection-url-must-be-a-valid-url","errorCode":null,"errorMessage":"The HubConnection url must be a valid url.","messagePattern":"The HubConnection url must be a valid url\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/SignalR/clients/ts/signalr/src/HubConnection.ts","lineNumber":192,"sourceCode":"    }\n\n    /** Indicates the url of the {@link HubConnection} to the server. */\n    get baseUrl(): string {\n        return this.connection.baseUrl || \"\";\n    }\n\n    /**\n     * Sets a new url for the HubConnection. Note that the url can only be changed when the connection is in either the Disconnected or\n     * Reconnecting states.\n     * @param {string} url The url to connect to.\n     */\n    set baseUrl(url: string) {\n        if (this._connectionState !== HubConnectionState.Disconnected && this._connectionState !== HubConnectionState.Reconnecting) {\n            throw new Error(\"The HubConnection must be in the Disconnected or Reconnecting state to change the url.\");\n        }\n\n        if (!url) {\n            throw new Error(\"The HubConnection url must be a valid url.\");\n        }\n\n        this.connection.baseUrl = url;\n    }\n\n    /** Starts the connection.\n     *\n     * @returns {Promise<void>} A Promise that resolves when the connection has been successfully established, or rejects with an error.\n     */\n    public start(): Promise<void> {\n        this._startPromise = this._startWithStateTransitions();\n        return this._startPromise;\n    }\n\n    private async _startWithStateTransitions(): Promise<void> {\n        if (this._connectionState !== HubConnectionState.Disconnected) {\n            return Promise.reject(new Error(\"Cannot start a HubConnection that is not in the 'Disconnected' state.\"));\n        }","sourceCodeStart":174,"sourceCodeEnd":210,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/SignalR/clients/ts/signalr/src/HubConnection.ts#L174-L210","documentation":"Thrown by the `baseUrl` setter on HubConnection when the new value is falsy (`undefined`, `null`, or empty string). The setter runs after the state guard, so it only fires when the state check has passed; reaching this throw means the state is valid but the URL is empty.","triggerScenarios":"Assigning an empty/undefined value to `hubConnection.baseUrl`, typically because a config lookup returned undefined (e.g. `process.env.HUB_URL` not set) or a variable was reset to empty.","commonSituations":"Missing environment variable in production, race where the config loader has not populated the URL yet, or accidentally passing the result of an expression that evaluates to ''.","solutions":["Validate the URL string is non-empty before assigning it to baseUrl.","Source the URL from a typed config object with a default value.","Fail fast at app startup if the required URL env var is missing."],"exampleFix":"// before\nhubConnection.baseUrl = process.env.HUB_URL;\n// after\nconst url = process.env.HUB_URL;\nif (!url) throw new Error(\"HUB_URL not configured\");\nhubConnection.baseUrl = url;","handlingStrategy":"validation","validationCode":"function requireUrl(url: string | undefined): string {\n  if (!url) throw new Error(\"Hub URL is required\");\n  return url;\n}","typeGuard":"function isNonEmptyString(v: unknown): v is string {\n  return typeof v === \"string\" && v.length > 0;\n}","tryCatchPattern":null,"preventionTips":["Validate URL config at startup.","Use typed config objects with required URL fields.","Fail fast on missing env vars."],"tags":["url","validation","configuration"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}