Skip to content

Commit 1aa3a44

Browse files
authored
feature: neverthrow (#4474)
1 parent 7575cbe commit 1aa3a44

8 files changed

Lines changed: 95 additions & 48 deletions

File tree

pnpm-lock.yaml

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ catalog:
139139
vitest-browser-react: 2.2.0
140140
vitest-browser-svelte: 2.1.1
141141
zimmerframe: 1.1.4
142+
neverthrow: 8.2.0
142143

143144
catalogMode: strict
144145

sites/plus.skeleton.dev/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"better-auth": "catalog:",
3535
"drizzle-kit": "catalog:",
3636
"drizzle-orm": "catalog:",
37+
"neverthrow": "catalog:",
3738
"playwright": "catalog:",
3839
"postgres": "catalog:",
3940
"sharp": "catalog:",
Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
11
import { getRequestEvent, query } from '$app/server';
22
import { getUser } from './get-user.remote';
3-
import { redirect } from '@sveltejs/kit';
3+
import { error, redirect } from '@sveltejs/kit';
44
import { auth } from '$lib/server/auth/auth';
55
import { resolve } from '$app/paths';
6+
import { ResultAsync } from 'neverthrow';
67

78
export const getAccounts = query(async () => {
89
const event = getRequestEvent();
9-
const user = await getUser();
1010

11-
if (!user) {
11+
const userResult = await ResultAsync.fromPromise(getUser(), (e) => new Error('Failed to fetch user', { cause: e }));
12+
13+
if (userResult.isErr()) {
14+
error(500, userResult.error.message);
15+
}
16+
17+
if (!userResult.value) {
1218
redirect(303, resolve('/auth/sign-in'));
1319
}
1420

15-
const accounts = await auth.api.listUserAccounts({
16-
headers: event.request.headers,
17-
});
21+
const accountsResult = await ResultAsync.fromPromise(
22+
auth.api.listUserAccounts({
23+
headers: event.request.headers,
24+
}),
25+
(e) => new Error('Failed to fetch user accounts', { cause: e }),
26+
);
27+
28+
if (accountsResult.isErr()) {
29+
error(500, accountsResult.error.message);
30+
}
1831

19-
return accounts;
32+
return accountsResult.value;
2033
});

sites/plus.skeleton.dev/src/lib/remote/auth/link-account.remote.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,25 @@ import { form, getRequestEvent } from '$app/server';
22
import { error, redirect } from '@sveltejs/kit';
33
import { auth } from '$lib/server/auth/auth';
44
import { LinkAccountSchema } from '$lib/schemas/auth/link-account-schema';
5+
import { ResultAsync } from 'neverthrow';
56

67
export const linkAccount = form(LinkAccountSchema, async (data) => {
78
const event = getRequestEvent();
89

9-
try {
10-
const linkAccount = await auth.api.linkSocialAccount({
10+
const linkAccountResult = await ResultAsync.fromPromise(
11+
auth.api.linkSocialAccount({
1112
headers: event.request.headers,
1213
body: {
1314
provider: data.providerId,
1415
callbackURL: data.callbackURL,
1516
},
16-
});
17+
}),
18+
(e) => new Error('Failed to link account', { cause: e }),
19+
);
1720

18-
if (!linkAccount.redirect || !linkAccount.url) {
19-
error(500, 'Failed to link account');
20-
}
21-
22-
redirect(303, linkAccount.url);
23-
} catch (e) {
24-
if (e instanceof Error) {
25-
throw error(400, e.message);
26-
}
27-
throw error(400, 'An unknown error occurred while linking the account');
21+
if (linkAccountResult.isErr()) {
22+
error(500, linkAccountResult.error.message);
2823
}
24+
25+
redirect(303, linkAccountResult.value.url);
2926
});

sites/plus.skeleton.dev/src/lib/remote/auth/sign-in.remote.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,31 @@ import { form, getRequestEvent } from '$app/server';
22
import { auth } from '$lib/server/auth/auth';
33
import { error, redirect } from '@sveltejs/kit';
44
import { SignInSchema } from '$lib/schemas/auth/sign-in-schema';
5+
import { ResultAsync } from 'neverthrow';
56

67
export const signIn = form(SignInSchema, async (data) => {
78
const event = getRequestEvent();
89

9-
const signIn = await auth.api.signInSocial({
10-
headers: event.request.headers,
11-
body: {
12-
provider: data.providerId,
13-
callbackURL: data.callbackURL,
10+
const signInResult = await ResultAsync.fromPromise(
11+
auth.api.signInSocial({
12+
headers: event.request.headers,
13+
body: {
14+
provider: data.providerId,
15+
callbackURL: data.callbackURL,
16+
},
17+
}),
18+
(e) => {
19+
return new Error('Failed to initiate social sign in', {
20+
cause: e,
21+
});
1422
},
15-
});
23+
);
1624

17-
if (!signIn.redirect || !signIn.url) {
18-
error(500, 'Failed to initiate social sign-in');
25+
if (signInResult.isErr()) {
26+
error(400, signInResult.error.message);
1927
}
2028

21-
redirect(303, signIn.url);
29+
if (signInResult.value.redirect && signInResult.value.url) {
30+
redirect(303, signInResult.value.url);
31+
}
2232
});
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import { form, getRequestEvent } from '$app/server';
22
import { error } from '@sveltejs/kit';
33
import { auth } from '$lib/server/auth/auth';
4+
import { ResultAsync } from 'neverthrow';
45

56
export const signOut = form('unchecked', async () => {
67
const event = getRequestEvent();
78

8-
try {
9-
const signOut = await auth.api.signOut({
9+
const signOutResult = await ResultAsync.fromPromise(
10+
auth.api.signOut({
1011
headers: event.request.headers,
11-
});
12+
}),
13+
(e) => new Error('Failed to sign out', { cause: e }),
14+
);
1215

13-
if (!signOut.success) {
14-
error(500, 'Failed to sign out');
15-
}
16-
} catch (e) {
17-
if (e instanceof Error) {
18-
throw error(400, e.message);
19-
}
20-
throw error(400, 'An unknown error occurred while signing out');
16+
if (signOutResult.isErr()) {
17+
error(500, signOutResult.error.message);
18+
}
19+
20+
if (!signOutResult.value.success) {
21+
error(500, 'Failed to sign out');
2122
}
2223
});

sites/plus.skeleton.dev/src/lib/remote/auth/unlink-account.remote.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@ import { form, getRequestEvent } from '$app/server';
22
import { error } from '@sveltejs/kit';
33
import { auth } from '$lib/server/auth/auth';
44
import { UnlinkAccountSchema } from '$lib/schemas/auth/unlink-account-schema';
5+
import { ResultAsync } from 'neverthrow';
56

67
export const unlinkAccount = form(UnlinkAccountSchema, async (data) => {
78
const event = getRequestEvent();
89

9-
try {
10-
await auth.api.unlinkAccount({
10+
const unlinkAccountResult = await ResultAsync.fromPromise(
11+
auth.api.unlinkAccount({
1112
headers: event.request.headers,
1213
body: {
1314
providerId: data.providerId,
1415
},
15-
});
16-
} catch (e) {
17-
if (e instanceof Error) {
18-
throw error(400, e.message);
19-
}
20-
throw error(400, 'An unknown error occurred while unlinking the account');
16+
}),
17+
(e) => new Error('Failed to unlink account', { cause: e }),
18+
);
19+
20+
if (unlinkAccountResult.isErr()) {
21+
error(400, unlinkAccountResult.error.message);
2122
}
2223
});

0 commit comments

Comments
 (0)