Skip to content

Commit

Permalink
refactor: better error handling when URL parse throws an error
Browse files Browse the repository at this point in the history
  • Loading branch information
peterknezek committed Oct 3, 2024
1 parent b77498e commit 3a25229
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type ModifierFn<R> = (json: R) => any;
const createMocksRequest =
(options: CreateMockHandlerOptions) =>
<Res>(method: Method, pathname: string, modifier?: ModifierFn<Res>) => {
const { loader, debug, origin: domain } = options;
const { loader, debug, origin } = options;
const getMock = () => {
try {
const data = loader(`${pathname}/${method}`);
Expand All @@ -31,11 +31,16 @@ const createMocksRequest =

// Construct the URL from the given pathname and domain.
const url = ((): string => {
if (domain) {
if (origin) {
try {
return new URL(pathname, domain).href;
return new URL(pathname, origin).href;
} catch (e) {
console.error("Invalid URL", e);
if (debug) {
console.error(
`[mocks-to-msw] Invalid URL. The provided option for a createMockHandler, the URL's origin, has the issue. Values: [origin]: "${origin}" [pathname]: "${pathname}"`,
e
);
}
return pathname;
}
}
Expand Down

0 comments on commit 3a25229

Please sign in to comment.