Skip to content

Commit

Permalink
prettier error message
Browse files Browse the repository at this point in the history
  • Loading branch information
arily committed Jan 2, 2024
1 parent a58fd97 commit 9deaf82
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/server/backend/bancho.py/server/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import imageType from 'image-type'
import { glob } from 'glob'
import { TRPCError } from '@trpc/server'
import { Prisma } from 'prisma-client-bancho-py'
import { type QueryError } from 'mysql2'
import type { Id } from '..'
import { getLiveUserStatus } from '../api-client'
import { normal } from '../constants'
Expand Down Expand Up @@ -46,7 +47,7 @@ import { prismaClient } from './source/prisma'
import { client as redisClient } from './source/redis'
import { UserRelationProvider } from './user-relations'
import { useDrizzle, userPriv } from './source/drizzle'
import { oldPasswordMismatch, userNotFound } from '~/server/trpc/messages'
import { conflictEmail, oldPasswordMismatch, userNotFound } from '~/server/trpc/messages'
import { type DynamicSettingStore, Scope, type UserCompact, type UserStatistic, UserStatus } from '~/def/user'
import type { CountryCode } from '~/def/country-code'
import type { ActiveMode, ActiveRuleset, LeaderboardRankingSystem } from '~/def/common'
Expand Down Expand Up @@ -448,7 +449,7 @@ class DBUserProvider extends Base<Id> implements Base<Id> {

if (excludes.profile !== true) {
returnValue.profile = {
html: user.userpageContent || '',
html: user.userpageContent ?? '',
}
}

Expand All @@ -475,11 +476,8 @@ class DBUserProvider extends Base<Id> implements Base<Id> {
) {
input.name && this.assertUsernameAllowed(input.name)

const result = await this.prisma.user.update({
where: {
id: user.id,
},
data: {
await this.drizzle.update(schema.users)
.set({
email: input.email,

name: input.name,
Expand All @@ -491,15 +489,28 @@ class DBUserProvider extends Base<Id> implements Base<Id> {
preferredMode: input.preferredMode
? toBanchoPyMode(input.preferredMode.mode, input.preferredMode.ruleset)
: undefined,
})
.where(eq(schema.users.id, user.id))
.catch((e: QueryError) => {
if (e.code === 'ER_DUP_ENTRY') {
raise(TRPCError, { code: 'CONFLICT', message: conflictEmail })
}
throw e
})

const returning = await this.drizzle.query.users.findFirst({
where(eq, op) {
return op.eq(eq.id, user.id)
},
include: {
with: {
clan: true,
},
})
}) ?? raise(Error, userNotFound)

return {
...toUserCompact(result, this.config),
...toUserClan(result),
...toUserOptional(result),
...toUserCompact(returning, this.config),
...toUserClan(returning),
...toUserOptional(returning),
}
}

Expand Down
1 change: 1 addition & 0 deletions src/server/trpc/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export const youNeedToLogin = 'you need to login.'
export const unknownError
= 'something went wrong on our side. Please report this problem to us.'
export const sessionNotFound = 'session not found.'
export const conflictEmail = 'email already used in other account.'

0 comments on commit 9deaf82

Please sign in to comment.