Jfheins/improve error message (#21295)
* Add test for empty URL * Throw if URL is empty, adjust test
This commit is contained in:
parent
8271204317
commit
63415b7dd2
|
|
@ -122,6 +122,7 @@ export class HubConnectionBuilder {
|
||||||
public withUrl(url: string, options: IHttpConnectionOptions): HubConnectionBuilder;
|
public withUrl(url: string, options: IHttpConnectionOptions): HubConnectionBuilder;
|
||||||
public withUrl(url: string, transportTypeOrOptions?: IHttpConnectionOptions | HttpTransportType): HubConnectionBuilder {
|
public withUrl(url: string, transportTypeOrOptions?: IHttpConnectionOptions | HttpTransportType): HubConnectionBuilder {
|
||||||
Arg.isRequired(url, "url");
|
Arg.isRequired(url, "url");
|
||||||
|
Arg.isNotEmpty(url, "url");
|
||||||
|
|
||||||
this.url = url;
|
this.url = url;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,11 @@ export class Arg {
|
||||||
throw new Error(`The '${name}' argument is required.`);
|
throw new Error(`The '${name}' argument is required.`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public static isNotEmpty(val: string, name: string): void {
|
||||||
|
if (!val || val.match(/^\s*$/)) {
|
||||||
|
throw new Error(`The '${name}' argument should not be empty.`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static isIn(val: any, values: any, name: string): void {
|
public static isIn(val: any, values: any, name: string): void {
|
||||||
// TypeScript enums have keys for **both** the name and the value of each enum member on the type itself.
|
// TypeScript enums have keys for **both** the name and the value of each enum member on the type itself.
|
||||||
|
|
|
||||||
|
|
@ -73,12 +73,14 @@ class CapturingConsole {
|
||||||
registerUnhandledRejectionHandler();
|
registerUnhandledRejectionHandler();
|
||||||
|
|
||||||
describe("HubConnectionBuilder", () => {
|
describe("HubConnectionBuilder", () => {
|
||||||
eachMissingValue((val, name) => {
|
for (const val of [undefined, null, ""]) {
|
||||||
it(`withUrl throws if url is ${name}`, () => {
|
it(`withUrl throws if url is ${String(val)}`, () => {
|
||||||
const builder = new HubConnectionBuilder();
|
const builder = new HubConnectionBuilder();
|
||||||
expect(() => builder.withUrl(val!)).toThrow("The 'url' argument is required.");
|
expect(() => builder.withUrl(val!)).toThrow(/The 'url' argument (is required|should not be empty)./);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
eachMissingValue((val, name) => {
|
||||||
it(`withHubProtocol throws if protocol is ${name}`, () => {
|
it(`withHubProtocol throws if protocol is ${name}`, () => {
|
||||||
const builder = new HubConnectionBuilder();
|
const builder = new HubConnectionBuilder();
|
||||||
expect(() => builder.withHubProtocol(val!)).toThrow("The 'protocol' argument is required.");
|
expect(() => builder.withHubProtocol(val!)).toThrow("The 'protocol' argument is required.");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue