Skip to content

Commit

Permalink
Handle invalid urls gracefully.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkBennett authored Jun 28, 2022
1 parent 61bd8e3 commit 1c426da
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
9 changes: 7 additions & 2 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ describe("processEvent", () => {
expect(processedEvent?.properties?.current_url).toBeUndefined();
});

// TODO: Determine the PostHog convention for handling errors in a plugin
it.todo("should raise an error if the current_url is an invalid url");
it("should raise an error if the current_url is an invalid url", () => {
const sourceEvent = buildPageViewEvent("invalid url");

expect(() => processEvent(sourceEvent, getMeta())).toThrowError(
`Unable to normalize invalid URL: "invalid url"`
);
});
});
10 changes: 7 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { PluginEvent, PluginInput, PluginMeta } from "@posthog/plugin-scaffold";

function normalizeUrl(url: string): string {
const parsedUrl = new URL(url.toLocaleLowerCase());
parsedUrl.pathname = parsedUrl.pathname.replace(/\/$/, "");
try {
const parsedUrl = new URL(url.toLocaleLowerCase());
parsedUrl.pathname = parsedUrl.pathname.replace(/\/$/, "");

return parsedUrl.toString();
return parsedUrl.toString();
} catch (err) {
throw `Unable to normalize invalid URL: "${url}"`;
}
}

export function processEvent(
Expand Down

0 comments on commit 1c426da

Please sign in to comment.