Skip to content

Commit e41d3cd

Browse files
committed
👍 Add UnsupportedHostingServiceError error
Now `getHostingService` throws `UnsupportedHostingServiceError` error when the hosting service is not supported.
1 parent 642ef10 commit e41d3cd

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

hosting_service/mod.ts

+22-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,26 @@ export async function getHostingService(
2626
): Promise<HostingService> {
2727
const hostname = aliases?.[fetchURL.hostname] ?? fetchURL.hostname;
2828
const svcName = hostname.replace(/\W/g, "_");
29-
const svc = await import(
30-
new URL(`./services/${svcName}.ts`, import.meta.url).href
31-
);
32-
return svc.service;
29+
try {
30+
const svc = await import(
31+
new URL(`./services/${svcName}.ts`, import.meta.url).href
32+
);
33+
return svc.service;
34+
} catch (err: unknown) {
35+
if (err instanceof TypeError) {
36+
// TypeError: Module not found "...".
37+
throw new UnsupportedHostingServiceError(hostname, svcName);
38+
}
39+
throw err;
40+
}
41+
}
42+
43+
export class UnsupportedHostingServiceError extends Error {
44+
constructor(
45+
public hostname: string,
46+
public svcName: string,
47+
) {
48+
super(`Unsupported hosting service: ${hostname} (${svcName})`);
49+
this.name = this.constructor.name;
50+
}
3351
}

hosting_service/mod_test.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
import { assertSnapshot } from "https://deno.land/[email protected]/testing/snapshot.ts";
2-
import { getHostingService } from "./mod.ts";
1+
import { assertRejects } from "https://deno.land/[email protected]/assert/mod.ts";
2+
import { assertSnapshot } from "https://deno.land/[email protected]/testing/snapshot.ts";
3+
import { getHostingService, UnsupportedHostingServiceError } from "./mod.ts";
34

45
Deno.test("getHostingService", async (t) => {
6+
await t.step("throws error for unsupported hosting service", async () => {
7+
const url = new URL("https://example.com/lambdalisue/gin.vim");
8+
await assertRejects(
9+
() => {
10+
return getHostingService(url);
11+
},
12+
UnsupportedHostingServiceError,
13+
);
14+
});
15+
516
const urls = [
617
new URL("ssh://[email protected]/lambdalisue/gin.vim"),
718
new URL("https://github.com/lambdalisue/gin.vim"),
819
];
9-
1020
for (const url of urls) {
1121
const svc = await getHostingService(url);
1222

0 commit comments

Comments
 (0)