xonecas.io
4436
Sl

sesame-labs-coding-assignment / commit 4026d96

use try/catch when using the async/await patterm

Sean2023-01-17 4026d962aaa719acf28faaca21d007a17f8b7de3 parent 3946df6

1 file changed

api/src/discord/index.ts+59−52
@@ -1,6 +1,6 @@
11 import Router from "koa-router";
22 import Koa from "koa";
3-import axios from "axios";
3+import axios, { AxiosError } from "axios";
44 import { stringify } from "querystring";
55 import { StatusCodes } from "http-status-codes";
66 import { getCouponCode } from "../quest/util";
@@ -13,25 +13,25 @@ router.get(
1313 const code = ctx.request.query.code;
1414 const state = Number(ctx.request.query.state);
1515
16- if (code) {
17- const response = await axios
18- .post(
19- "https://discord.com/api/v10/oauth2/token",
20- stringify({
21- client_id: process.env.DISCORD_CLIENT_ID,
22- client_secret: process.env.DISCORD_CLIENT_SECRET,
23- grant_type: "authorization_code",
24- code: code,
25- redirect_uri: process.env.DISCORD_REDIRECT_URL,
26- }),
27- {
28- headers: { "Content-Type": "application/x-www-form-urlencoded" },
29- }
30- )
31- .catch((e) => {
32- console.log(e);
33- throw e;
34- });
16+ if (!code) {
17+ ctx.status = StatusCodes.UNAUTHORIZED;
18+ return await next();
19+ }
20+
21+ try {
22+ const response = await axios.post(
23+ "https://discord.com/api/v10/oauth2/token",
24+ stringify({
25+ client_id: process.env.DISCORD_CLIENT_ID,
26+ client_secret: process.env.DISCORD_CLIENT_SECRET,
27+ grant_type: "authorization_code",
28+ code: code,
29+ redirect_uri: process.env.DISCORD_REDIRECT_URL,
30+ }),
31+ {
32+ headers: { "Content-Type": "application/x-www-form-urlencoded" },
33+ }
34+ );
3535
3636 const discordToken = await ctx.prisma.discordToken.create({
3737 data: {
@@ -49,9 +49,13 @@ router.get(
4949 });
5050
5151 ctx.status = StatusCodes.CREATED;
52+ ctx.redirect("/discord/join");
53+
54+ await next();
55+ } catch (e) {
56+ console.error(e);
57+ throw e;
5258 }
53- ctx.redirect("/discord/join");
54- await next();
5559 }
5660 );
5761
@@ -79,27 +83,28 @@ router.get("/join", async (ctx: Koa.ParameterizedContext, next: Koa.Next) => {
7983 return await next();
8084 }
8185
82- const inviteCode = await axios
83- .post(
84- `https://discord.com/api/v10/channels/${process.env.QUEST_CHANNEL_ID}/invites`,
85- {},
86- {
87- headers: {
88- Authorization: `Bot ${process.env.DISCORD_BOT_TOKEN}`,
89- "Content-Type": "application/json",
90- },
91- }
92- )
93- .then((response) => response.data.code)
94- .catch((e) => {
95- console.log(e);
96- throw e;
97- });
98-
99- ctx.session.user = user;
100- ctx.redirect(`http://discord.gg/${inviteCode}`);
86+ try {
87+ const inviteCode = await axios
88+ .post(
89+ `https://discord.com/api/v10/channels/${process.env.QUEST_CHANNEL_ID}/invites`,
90+ {},
91+ {
92+ headers: {
93+ Authorization: `Bot ${process.env.DISCORD_BOT_TOKEN}`,
94+ "Content-Type": "application/json",
95+ },
96+ }
97+ )
98+ .then((response) => response.data.code);
99+
100+ ctx.session.user = user;
101+ ctx.redirect(`http://discord.gg/${inviteCode}`);
101102
102- await next();
103+ await next();
104+ } catch (e) {
105+ console.error(e);
106+ throw e;
107+ }
103108 });
104109
105110 router.get("/verify", async (ctx: Koa.ParameterizedContext, next: Koa.Next) => {
@@ -114,20 +119,22 @@ router.get("/verify", async (ctx: Koa.ParameterizedContext, next: Koa.Next) => {
114119 Authorization: `Bearer ${ctx.session.user.discordToken.accessToken}`,
115120 "Content-Type": "application/x-www-form-urlencoded",
116121 };
117- await axios
118- .get(`https://discord.com/api/v10/users/@me/guilds/${guildId}/member`, {
119- headers: authorizationHeader,
120- })
121- .catch((e) => {
122- console.log(e);
123- throw e;
124- });
122+
123+ try {
124+ await axios.get(
125+ `https://discord.com/api/v10/users/@me/guilds/${guildId}/member`,
126+ {
127+ headers: authorizationHeader,
128+ }
129+ );
130+ } catch (e) {
131+ console.error(e);
132+ throw e;
133+ }
125134
126135 // 10 random characters alphanumeric string
127136 const coupon = getCouponCode();
128137
129- console.log(coupon);
130-
131138 ctx.body = ctx.session.user = await ctx.prisma.user.update({
132139 where: { walletAddress: ctx.session.user.walletAddress },
133140 include: { discordToken: true },