@@ -88,13 +88,11 @@ import {
8888} from "@monkeytype/contracts/users" ;
8989import { MILLISECONDS_IN_DAY } from "@monkeytype/util/date-and-time" ;
9090import { MonkeyRequest } from "../types" ;
91+ import { tryCatch } from "@monkeytype/util/trycatch" ;
9192
9293async function verifyCaptcha ( captcha : string ) : Promise < void > {
93- let verified = false ;
94- try {
95- verified = await verify ( captcha ) ;
96- } catch ( e ) {
97- //fetch to recaptcha api can sometimes fail
94+ const { data : verified , error } = await tryCatch ( verify ( captcha ) ) ;
95+ if ( error ) {
9896 throw new MonkeyError (
9997 422 ,
10098 "Request to the Captcha API failed, please try again later"
@@ -177,18 +175,19 @@ export async function sendVerificationEmail(
177175 ) ;
178176 }
179177
180- let link = "" ;
181- try {
182- link = await FirebaseAdmin ( )
178+ const { data : link , error } = await tryCatch (
179+ FirebaseAdmin ( )
183180 . auth ( )
184181 . generateEmailVerificationLink ( email , {
185182 url : isDevEnvironment ( )
186183 ? "http://localhost:3000"
187184 : "https://monkeytype.com" ,
188- } ) ;
189- } catch ( e ) {
190- if ( isFirebaseError ( e ) ) {
191- if ( e . errorInfo . code === "auth/user-not-found" ) {
185+ } )
186+ ) ;
187+
188+ if ( error ) {
189+ if ( isFirebaseError ( error ) ) {
190+ if ( error . errorInfo . code === "auth/user-not-found" ) {
192191 throw new MonkeyError (
193192 500 ,
194193 "Auth user not found when the user was found in the database. Contact support with this error message and your email" ,
@@ -198,11 +197,11 @@ export async function sendVerificationEmail(
198197 } ) ,
199198 userInfo . uid
200199 ) ;
201- } else if ( e . errorInfo . code === "auth/too-many-requests" ) {
200+ } else if ( error . errorInfo . code === "auth/too-many-requests" ) {
202201 throw new MonkeyError ( 429 , "Too many requests. Please try again later" ) ;
203202 } else if (
204- e . errorInfo . code === "auth/internal-error" &&
205- e . errorInfo . message . toLowerCase ( ) . includes ( "too_many_attempts" )
203+ error . errorInfo . code === "auth/internal-error" &&
204+ error . errorInfo . message . toLowerCase ( ) . includes ( "too_many_attempts" )
206205 ) {
207206 throw new MonkeyError (
208207 429 ,
@@ -212,12 +211,12 @@ export async function sendVerificationEmail(
212211 throw new MonkeyError (
213212 500 ,
214213 "Firebase failed to generate an email verification link: " +
215- e . errorInfo . message ,
216- JSON . stringify ( e )
214+ error . errorInfo . message ,
215+ JSON . stringify ( error )
217216 ) ;
218217 }
219218 } else {
220- const message = getErrorMessage ( e ) ;
219+ const message = getErrorMessage ( error ) ;
221220 if ( message === undefined ) {
222221 throw new MonkeyError (
223222 500 ,
@@ -233,12 +232,13 @@ export async function sendVerificationEmail(
233232 throw new MonkeyError (
234233 500 ,
235234 "Failed to generate an email verification link: " + message ,
236- ( e as Error ) . stack
235+ error . stack
237236 ) ;
238237 }
239238 }
240239 }
241240 }
241+
242242 await emailQueue . sendVerificationEmail ( email , userInfo . name , link ) ;
243243
244244 return new MonkeyResponse ( "Email sent" , null ) ;
@@ -259,22 +259,20 @@ export async function sendForgotPasswordEmail(
259259export async function deleteUser ( req : MonkeyRequest ) : Promise < MonkeyResponse > {
260260 const { uid } = req . ctx . decodedToken ;
261261
262- let userInfo :
263- | Pick < UserDAL . DBUser , "banned" | "name" | "email" | "discordId" >
264- | undefined ;
265-
266- try {
267- userInfo = await UserDAL . getPartialUser ( uid , "delete user" , [
262+ const { data : userInfo , error } = await tryCatch (
263+ UserDAL . getPartialUser ( uid , "delete user" , [
268264 "banned" ,
269265 "name" ,
270266 "email" ,
271267 "discordId" ,
272- ] ) ;
273- } catch ( e ) {
274- if ( e instanceof MonkeyError && e . status === 404 ) {
268+ ] )
269+ ) ;
270+
271+ if ( error ) {
272+ if ( error instanceof MonkeyError && error . status === 404 ) {
275273 //userinfo was already deleted. We ignore this and still try to remove the other data
276274 } else {
277- throw e ;
275+ throw error ;
278276 }
279277 }
280278
@@ -533,12 +531,12 @@ function getRelevantUserInfo(user: UserDAL.DBUser): RelevantUserInfo {
533531export async function getUser ( req : MonkeyRequest ) : Promise < GetUserResponse > {
534532 const { uid } = req . ctx . decodedToken ;
535533
536- let userInfo : UserDAL . DBUser ;
537- try {
538- userInfo = await UserDAL . getUser ( uid , "get user" ) ;
539- } catch ( e ) {
540- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
541- if ( e . status === 404 ) {
534+ const { data : userInfo , error } = await tryCatch (
535+ UserDAL . getUser ( uid , "get user" )
536+ ) ;
537+
538+ if ( error ) {
539+ if ( error instanceof MonkeyError && error . status === 404 ) {
542540 //if the user is in the auth system but not in the db, its possible that the user was created by bypassing captcha
543541 //since there is no data in the database anyway, we can just delete the user from the auth system
544542 //and ask them to sign up again
@@ -564,7 +562,7 @@ export async function getUser(req: MonkeyRequest): Promise<GetUserResponse> {
564562 }
565563 }
566564 } else {
567- throw e ;
565+ throw error ;
568566 }
569567 }
570568
0 commit comments