Skip to content

Commit 64bc5ce

Browse files
committed
feat: add better error handling
1 parent 651b93b commit 64bc5ce

File tree

5 files changed

+32
-19
lines changed

5 files changed

+32
-19
lines changed

apps/api/src/auth/edgedb.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { Effect, Config } from 'effect';
33
import { EdgedbAuthResponseSchema } from './schema';
44
import e, { createClient } from '../../dbschema/edgeql-js';
55

6+
export class CreateUserError extends Error {
7+
_tag = 'CreateUserError';
8+
}
9+
610
export const getTokenResponse = ({
711
code,
812
verifier,
@@ -54,8 +58,7 @@ export const createUser = (user: {
5458

5559
return await insertQuery.run(client);
5660
},
57-
catch(e) {
58-
console.log(e);
59-
return null;
61+
catch(error) {
62+
return new CreateUserError('Failed to create user.', { cause: error });
6063
},
6164
});

apps/api/src/auth/oauth.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { google } from 'googleapis';
44
import { Octokit } from 'octokit';
55
import { assertUnreachable } from '../utils/assert';
66

7+
export class OAuthError extends Error {
8+
_tag = 'OAuthError';
9+
}
10+
711
export const getUserFromOauth = ({
812
provider,
913
providerToken,
@@ -12,7 +16,7 @@ export const getUserFromOauth = ({
1216
providerToken: string;
1317
}) =>
1418
Effect.gen(function* () {
15-
if (provider === OAuthProvider.github) {
19+
if (provider === OAuthProvider.GitHub) {
1620
return yield* Effect.tryPromise({
1721
async try() {
1822
const octokit = new Octokit({ auth: providerToken });
@@ -25,12 +29,12 @@ export const getUserFromOauth = ({
2529
};
2630
},
2731
catch(error) {
28-
console.log('Error getting info about user from GitHub');
29-
30-
return Effect.fail(error);
32+
return new OAuthError("Couldn't get user info from GitHub.", {
33+
cause: error,
34+
});
3135
},
3236
});
33-
} else if (provider === OAuthProvider.google) {
37+
} else if (provider === OAuthProvider.Google) {
3438
return yield* Effect.tryPromise({
3539
async try(signal) {
3640
const googleReponse = await google.oauth2('v2').userinfo.get(
@@ -45,9 +49,9 @@ export const getUserFromOauth = ({
4549
};
4650
},
4751
catch(error) {
48-
console.log('Error getting info about user from Google');
49-
50-
return Effect.fail(error);
52+
return new OAuthError("Couldn't get user info from Google.", {
53+
cause: error,
54+
});
5155
},
5256
});
5357
}

apps/api/src/auth/router.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ import { createUser, getTokenResponse } from './edgedb';
1515
import { getUserFromOauth } from './oauth';
1616

1717
export const AuthRouter = HttpServer.router.empty.pipe(
18-
HttpServer.router.get('/auth/ui/signin', handleAuthUi('signin')),
19-
HttpServer.router.get('/auth/ui/signup', handleAuthUi('signup')),
20-
HttpServer.router.get('/auth/signout', handleSignout()),
21-
HttpServer.router.get('/auth/callback', handleCallback()),
18+
HttpServer.router.get('/ui/signin', handleAuthUi('signin')),
19+
HttpServer.router.get('/ui/signup', handleAuthUi('signup')),
20+
HttpServer.router.get('/signout', handleSignout()),
21+
HttpServer.router.get('/callback', handleCallback()),
22+
HttpServer.router.prefixAll('/auth'),
2223
);
2324

2425
function handleAuthUi(method: 'signin' | 'signup') {

apps/api/src/auth/schema.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export const GoogleUserSchema = Schema.Struct({
1818
});
1919

2020
export enum OAuthProvider {
21-
google = 'builtin::oauth_google',
22-
github = 'builtin::oauth_github',
21+
Google = 'builtin::oauth_google',
22+
GitHub = 'builtin::oauth_github',
2323
}
2424

2525
export const OAuthProviderSchema = Schema.Enums(OAuthProvider);

apps/api/src/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { AuthRouter } from './auth/router';
66
import { corsMiddleware } from './cors';
77
import { HealthRouter } from './health/router';
88
import { LogLevelLive } from './logging';
9+
import { statusCodes } from './utils/response';
910

1011
const ServerLive = BunHttpServer.server.layerConfig({
1112
port: Config.number('PORT').pipe(Config.withDefault(3001)),
@@ -40,13 +41,17 @@ const runnable = WholeRouter.pipe(
4041
Effect.catchAll((error) =>
4142
Effect.gen(function* () {
4243
yield* Console.error('Error', error);
43-
return HttpServer.response.text('Error', { status: 500 });
44+
return HttpServer.response.text('Error', {
45+
status: statusCodes.INTERNAL_SERVER_ERROR,
46+
});
4447
}),
4548
),
4649
Effect.catchAllDefect((defect) =>
4750
Effect.gen(function* () {
4851
yield* Console.error('Defect', defect);
49-
return HttpServer.response.text('Defect', { status: 500 });
52+
return HttpServer.response.text('Defect', {
53+
status: statusCodes.INTERNAL_SERVER_ERROR,
54+
});
5055
}),
5156
),
5257
HttpServer.server.serve(HttpServer.middleware.logger),

0 commit comments

Comments
 (0)