Structure advertised endpoint errors (#3289)
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
025fd6f5e8
commit
6715d1651f
2 files changed
+110
-2
No files matched your search
@@ -0,0 +1,55 @@
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import {
|
||||
AdvertisedEndpointProtocolError,
|
||||
AdvertisedEndpointUrlParseError,
|
||||
normalizeHttpBaseUrl,
|
||||
} from "./advertisedEndpoint.ts";
|
||||
|
||||
const captureError = (run: () => unknown): unknown => {
|
||||
try {
|
||||
run();
|
||||
} catch (cause) {
|
||||
return cause;
|
||||
}
|
||||
throw new Error("Expected operation to throw");
|
||||
};
|
||||
|
||||
describe("advertised endpoints", () => {
|
||||
it("normalizes websocket endpoints to an HTTP base URL", () => {
|
||||
expect(normalizeHttpBaseUrl("wss://relay.example.com/path?query=value#fragment")).toBe(
|
||||
"https://relay.example.com/",
|
||||
);
|
||||
});
|
||||
|
||||
it("preserves URL parser failures without retaining their raw input", () => {
|
||||
const input = "not a URL";
|
||||
const error = captureError(() => normalizeHttpBaseUrl(input));
|
||||
|
||||
expect(error).toBeInstanceOf(AdvertisedEndpointUrlParseError);
|
||||
expect(error).toMatchObject({ inputLength: input.length });
|
||||
expect(error).not.toHaveProperty("input");
|
||||
expect((error as AdvertisedEndpointUrlParseError).cause).toBeInstanceOf(Error);
|
||||
expect((error as Error).message).toBe("Invalid advertised endpoint URL.");
|
||||
});
|
||||
|
||||
it("reports unsupported protocols without inventing a cause", () => {
|
||||
const input =
|
||||
"ftp://endpoint-user:[email protected]/private/path?token=secret#fragment";
|
||||
const error = captureError(() => normalizeHttpBaseUrl(input));
|
||||
|
||||
expect(error).toBeInstanceOf(AdvertisedEndpointProtocolError);
|
||||
expect(error).toMatchObject({
|
||||
inputLength: input.length,
|
||||
inputProtocol: "ftp:",
|
||||
inputHostname: "relay.example.com",
|
||||
protocol: "ftp:",
|
||||
});
|
||||
expect(error).not.toHaveProperty("input");
|
||||
expect((error as Error & { cause?: unknown }).cause).toBeUndefined();
|
||||
expect((error as Error).message).toBe("Endpoint must use HTTP or HTTPS. Received ftp:");
|
||||
expect((error as Error).message).not.toContain("endpoint-password");
|
||||
expect((error as Error).message).not.toContain("/private/path");
|
||||
expect((error as Error).message).not.toContain("token=secret");
|
||||
});
|
||||
});
|
||||
@@ -6,6 +6,48 @@ import type {
|
||||
AdvertisedEndpointSource,
|
||||
AdvertisedEndpointStatus,
|
||||
} from "@t3tools/contracts";
|
||||
import * as Schema from "effect/Schema";
|
||||
|
||||
import { getUrlDiagnostics } from "./urlDiagnostics.ts";
|
||||
|
||||
const AdvertisedEndpointInputDiagnosticFields = {
|
||||
inputLength: Schema.Number,
|
||||
inputProtocol: Schema.optional(Schema.String),
|
||||
inputHostname: Schema.optional(Schema.String),
|
||||
};
|
||||
|
||||
function advertisedEndpointInputDiagnostics(input: string) {
|
||||
const diagnostics = getUrlDiagnostics(input);
|
||||
return {
|
||||
inputLength: diagnostics.inputLength,
|
||||
...(diagnostics.protocol === undefined ? {} : { inputProtocol: diagnostics.protocol }),
|
||||
...(diagnostics.hostname === undefined ? {} : { inputHostname: diagnostics.hostname }),
|
||||
};
|
||||
}
|
||||
|
||||
export class AdvertisedEndpointUrlParseError extends Schema.TaggedErrorClass<AdvertisedEndpointUrlParseError>()(
|
||||
"AdvertisedEndpointUrlParseError",
|
||||
{
|
||||
...AdvertisedEndpointInputDiagnosticFields,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return "Invalid advertised endpoint URL.";
|
||||
}
|
||||
}
|
||||
|
||||
export class AdvertisedEndpointProtocolError extends Schema.TaggedErrorClass<AdvertisedEndpointProtocolError>()(
|
||||
"AdvertisedEndpointProtocolError",
|
||||
{
|
||||
...AdvertisedEndpointInputDiagnosticFields,
|
||||
protocol: Schema.String,
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Endpoint must use HTTP or HTTPS. Received ${this.protocol}`;
|
||||
}
|
||||
}
|
||||
|
||||
export interface CreateAdvertisedEndpointInput {
|
||||
readonly id: string;
|
||||
@@ -22,7 +64,15 @@ export interface CreateAdvertisedEndpointInput {
|
||||
}
|
||||
|
||||
export function normalizeHttpBaseUrl(rawValue: string): string {
|
||||
const url = new URL(rawValue);
|
||||
let url: URL;
|
||||
try {
|
||||
url = new URL(rawValue);
|
||||
} catch (cause) {
|
||||
throw new AdvertisedEndpointUrlParseError({
|
||||
...advertisedEndpointInputDiagnostics(rawValue),
|
||||
cause,
|
||||
});
|
||||
}
|
||||
if (url.protocol === "ws:") {
|
||||
url.protocol = "http:";
|
||||
} else if (url.protocol === "wss:") {
|
||||
@@ -30,7 +80,10 @@ export function normalizeHttpBaseUrl(rawValue: string): string {
|
||||
}
|
||||
|
||||
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
||||
throw new Error(`Endpoint must use HTTP or HTTPS. Received ${url.protocol}`);
|
||||
throw new AdvertisedEndpointProtocolError({
|
||||
...advertisedEndpointInputDiagnostics(rawValue),
|
||||
protocol: url.protocol,
|
||||
});
|
||||
}
|
||||
|
||||
url.pathname = "/";
|
||||
|
||||
Reference in new issue
Block a user