{"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/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/SignalR/clients/ts/signalr/src/HubConnection.ts#L174-L210","documentation":"Thrown in the baseUrl setter (HubConnection.ts:192) when the new url value is falsy (empty string, null, undefined, 0). A HubConnection requires a real target endpoint, so an empty URL is rejected. The check at line 191 uses `if (!url)`.","triggerScenarios":"Setting `hub.baseUrl = ''`, `hub.baseUrl = null`, `hub.baseUrl = undefined`, or passing an empty string variable that came from a failed config load. Runs after the state check passes (Disconnected/Reconnecting).","commonSituations":"Loading URL from config/env that wasn't set (`process.env.HUB_URL` returns undefined); a UI field that allowed empty submission; a redirect/variable shadowing bug producing an empty string; clearing the URL by accident before reassignment.","solutions":["Validate the URL is non-empty before assigning: `if (newUrl) hub.baseUrl = newUrl;`.","Provide a default: `hub.baseUrl = newUrl || fallbackUrl`.","Check the source of the URL (env var, config) for typos or unset values.","Use `new URL(newUrl)` to validate it parses as a real URL before assigning."],"exampleFix":"// before\nhub.baseUrl = process.env.HUB_URL; // undefined -> throws\n\n// after\nconst url = process.env.HUB_URL;\nif (url) hub.baseUrl = url;\nelse throw new Error('HUB_URL not configured');","handlingStrategy":"validation","validationCode":"function setHubUrl(hub, url) {\n  if (!url || typeof url !== 'string' || !url.trim()) {\n    throw new Error('A non-empty url string is required');\n  }\n  hub.baseUrl = url;\n}","typeGuard":"function isNonEmptyUrlString(u: unknown): u is string {\n  return typeof u === 'string' && u.trim().length > 0;\n}","tryCatchPattern":"try { hub.baseUrl = candidate; }\ncatch (e) {\n  if (/must be a valid url/.test(String(e))) {\n    throw new Error('config did not provide a hub url');\n  }\n  throw e;\n}","preventionTips":["Validate URL config at app startup, fail loudly if missing.","Provide a sensible default URL in your config layer.","Never assign undefined/null/empty strings to baseUrl."],"tags":["url","validation","configuration"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}