Skip to content

Commit

Permalink
error msgs
Browse files Browse the repository at this point in the history
  • Loading branch information
arily committed Jun 8, 2024
1 parent 110ebab commit 111eafc
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 30 deletions.
11 changes: 9 additions & 2 deletions src/def/messages.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export enum GucchoError {
UnknownError = 1,
UnknownError = -1,
AssertionError,
MissingServerAvatarConfig,

// basic
ModeNotSupported = 1000,
ModeOrRulesetNotSupported,
InvalidId,

// user
Expand All @@ -20,7 +22,6 @@ export enum GucchoError {

// auth
IncorrectPassword = 3000,

PasswordNotMatch,
OldPasswordMismatch,
EmailTokenNotFound,
Expand Down Expand Up @@ -48,4 +49,10 @@ export enum GucchoError {
ScoreNotFound = 8000,
// clan
ClanNotFound = 9000,

// article
ArticleNotFound = 10000,
InsufficientPrivilegeToEditArticle,
FileSystemArticlePathOutsideArticleRoot,
TryingToDeleteFallbackContents,
}
29 changes: 18 additions & 11 deletions src/locales/base/de-DE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ export default {
[GucchoError.ScoreNotFound]: 'Score nicht gefunden.',
[GucchoError.UnableToUpdateSession]: 'Fehler beim aktualisieren der Session.',
[GucchoError.ClanNotFound]: 'Clan nicht gefunden.',
// TODO DE translation (hint: use TS autocomplete)
// [GucchoError.AssertionError]: '',
// [GucchoError.ModeOrRulesetNotSupported]: '',
// [GucchoError.InsufficientPrivilegeToEditArticle]: '',
// [GucchoError.FileSystemArticlePathOutsideArticleRoot]: '',
// [GucchoError.TryingToDeleteFallbackContents]: ''
// [GucchoError.ArticleNotFound]: '',
},
country: {
[CountryCode.Unknown]: 'Unbekannt',
Expand Down Expand Up @@ -405,43 +412,43 @@ export default {
subject: '{serverName} - Konto Verifikation',
content: `
Hallo,
um deine Email für {serverName} zu verifizieren, bitte folge dem Link:
{link}
Alternativ, kannst du auch den folgenden Code benutzen: {otp}
Die Verifikation ist gültig für {ttl} Minuten.
Lass uns wissen, wenn du irgendwelche Fragen hast.
{serverName}
`,
},
[Mail.Variant.AccountRecovery]: {
subject: '{serverName} - Konto Wiederherstellung',
content: `
Hallo {name},
um dein Passwort für {serverName} zurückzusetzen, bitte folge dem folgenden Link:
{link}
Alternativ kannst du auch den folgenden Code benutzen: {otp}
Die Verifikation ist gültig für {ttl} Minuten.
{serverName}
`,
},
[Mail.Variant.ChangeMail]: {
subject: '{serverName} - E-Mail-Adresse ändern',
content: `
Hallo {name},
um deine E-Mail-Adresse für {serverName} zu ändern, benutze bitte den folgenden Code:
{otp}
Die Verifikation ist gültig für {ttl} Minuten.
{serverName}
`,
},
Expand Down
7 changes: 6 additions & 1 deletion src/locales/base/en-GB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export default {
role: {
[UserRole.Disabled]: 'Disabled',
[UserRole.Restricted]: 'Restricted',
// [UserRole.Registered]: 'Registered',
[UserRole.Inactive]: 'Inactive',
[UserRole.Supported]: 'Supported',
[UserRole.Supporter]: 'Supporter',
Expand Down Expand Up @@ -133,6 +132,7 @@ export default {
[GucchoError.ConflictRelation]: 'You already have a relation with this player.',
[GucchoError.MissingServerAvatarConfig]: 'The server is not configured correctly; avatar location is missing.',
[GucchoError.ModeNotSupported]: 'Mode not supported.',
[GucchoError.ModeOrRulesetNotSupported]: 'Mode or ruleset not supported.',
[GucchoError.UpdateUserpageFailed]: 'Failed to update user page.',
[GucchoError.MimeNotImage]: 'The provided file is not an image.',
[GucchoError.HackerTryingToDeleteAllAvatars]: 'SOMEONE IS ATTEMPTING TO DELETE ALL AVATARS.',
Expand All @@ -147,6 +147,11 @@ export default {
[GucchoError.ScoreNotFound]: 'Score not found.',
[GucchoError.UnableToUpdateSession]: 'Unable to update session.',
[GucchoError.ClanNotFound]: 'Clan not found.',
[GucchoError.AssertionError]: 'Assertion Error: Something unexpected happened!',
[GucchoError.InsufficientPrivilegeToEditArticle]: 'You can not edit this article due to insufficient privilege.',
[GucchoError.FileSystemArticlePathOutsideArticleRoot]: 'Prohibited Saving article outside articles path.',
[GucchoError.TryingToDeleteFallbackContents]: 'Trying to delete fallback contents.',
[GucchoError.ArticleNotFound]: 'Article not found.',
},
country: {
[CountryCode.Unknown]: 'Unknown',
Expand Down
6 changes: 6 additions & 0 deletions src/locales/base/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export default {
[GucchoError.UnknownError]: '未知错误。',
[GucchoError.MissingServerAvatarConfig]: '头像配置缺失。',
[GucchoError.ModeNotSupported]: '不支持的模式。',
[GucchoError.ModeOrRulesetNotSupported]: '不支持的模式或玩法。',
[GucchoError.UserNotFound]: '找不到用户。',
[GucchoError.UserExists]: '已有此用户。',
[GucchoError.ConflictEmail]: '邮箱已被使用。',
Expand Down Expand Up @@ -140,6 +141,11 @@ export default {
[GucchoError.ScoreNotFound]: '找不到成绩。',
[GucchoError.UnableToUpdateSession]: '无法更新会话。',
[GucchoError.ClanNotFound]: '找不到家人。',
[GucchoError.AssertionError]: '断言发生错误。不应该出现此状态!',
[GucchoError.InsufficientPrivilegeToEditArticle]: '您没有权限修改此文章。',
[GucchoError.FileSystemArticlePathOutsideArticleRoot]: '保存路径在文章目录以外。',
[GucchoError.TryingToDeleteFallbackContents]: '正在尝试删除缺省的内容。',
[GucchoError.ArticleNotFound]: '找不到文章。',
},

country: {
Expand Down
14 changes: 12 additions & 2 deletions src/pages/article/[...id].vue
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
<script setup lang="ts">
const route = useRoute('article-id')
const { t } = useI18n()
const id = route.params.id
if (!id) {
throw new Error('id required')
throw new Error(t('error.no-id'))
}
const app$ = useNuxtApp()
const content = await app$.$client.article.getStaticOrDynamic.query(id)
const { t } = useI18n()
</script>

<i18n lang="yaml">
en-GB:
edit: Edit
error:
no-id: No article Id provided.

zh-CN:
edit: 编辑
error:
no-id: 没有提供文章ID。

fr-FR:
edit: Modifier
# TODO fr translation
error:
no-id: No article Id provided.

de-DE:
edit: Bearbeiten
# TODO update de translation
error:
no-id: No article Id provided.
</i18n>

<template>
Expand Down
9 changes: 5 additions & 4 deletions src/server/backend/$base/server/article/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { latest, paths, v0, versions } from './v'
import useEditorExtensions from '~/composables/useEditorExtensionsServer'
import type { UserCompact, UserRole } from '~/def/user'
import { Logger } from '$base/logger'
import { GucchoError } from '~/def/messages'

const logger = Logger.child({ label: 'article' })

Expand Down Expand Up @@ -116,7 +117,7 @@ export abstract class ArticleProvider {
dynamic: boolean
}): Promise<void> {
if (!opt.user.roles.find(role => ['admin', 'owner'].includes(role))) {
throw new Error('you have insufficient privilege to edit this article')
throwGucchoError(GucchoError.InsufficientPrivilegeToEditArticle)
}
const pContent = ArticleProvider.createContent(opt)
let meta: ArticleProvider.Meta
Expand Down Expand Up @@ -150,14 +151,14 @@ export abstract class ArticleProvider {
static async deleteLocal(opt: { slug: string; user: UserCompact<any> }) {
const { user, slug } = opt
if (!user.roles.find(role => ['admin', 'owner'].includes(role))) {
throw new Error('you have insufficient privilege to edit this article')
throwGucchoError(GucchoError.InsufficientPrivilegeToEditArticle)
}
const loc = join(ArticleProvider.articles, slug)
if (!ArticleProvider.inside(loc)) {
throw new Error('dangerous operation')
throwGucchoError(GucchoError.FileSystemArticlePathOutsideArticleRoot)
}
if (!relative(join(ArticleProvider.articles, './fallbacks'), loc).startsWith('..')) {
throw new Error('trying to delete fallback contents')
throwGucchoError(GucchoError.TryingToDeleteFallbackContents)
}
return await fs.rm(loc)
}
Expand Down
5 changes: 0 additions & 5 deletions src/server/backend/bancho.py/server/rank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,6 @@ export class RedisRankProvider extends DatabaseRankProvider implements Monitored
const start = page * pageSize
const bPyMode = toBanchoPyMode(mode, ruleset)

if (bPyMode === undefined) {
// throw new Error('no mode')
raise(Error, 'no mode')
}

// user.id[]
const rank = await this.getPPv2LiveLeaderboard(bPyMode, 0, start + pageSize * 2).then(res => res.map(Number))

Expand Down
3 changes: 2 additions & 1 deletion src/server/backend/bancho.py/transforms/mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { match } from 'switch-pattern'
import { BanchoMode, BanchoPyMode } from '../enums'
import { Mode, Ruleset } from '~/def'
import type { ActiveMode, ActiveRuleset } from '~/def/common'
import { GucchoError } from '~/def/messages'

export const BPyMode = {
[BanchoPyMode.OsuStandard]: [Mode.Osu, Ruleset.Standard],
Expand All @@ -25,7 +26,7 @@ export function toBanchoPyMode(

const str = BPyModeEntries.find(([_, mr]) => patterns.exact(mr))?.[0]
if (!str) {
throw new Error('not supported')
throwGucchoError(GucchoError.ModeOrRulesetNotSupported)
}
return Number.parseInt(str)
}
Expand Down
4 changes: 2 additions & 2 deletions src/server/routes/mail/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ const jumpMap: Record<Mail.Variant, { failed(t: string): string; succeed(t: stri
},
[Mail.Variant.AccountRecovery]: {
failed(_t: string): string {
return 'auth/login'
return '/auth/login'
},
succeed(t: string): string {
return `/auth/account-recovery?t=${t}`
},
},
[Mail.Variant.ChangeMail]: {
failed(_t: string): string {
throw new Error('/me-settings')
return '/me-settings'
},
succeed(_t: string): string {
throw new Error('Function not implemented. Please use one time code instead.')
Expand Down
5 changes: 3 additions & 2 deletions src/server/trpc/routers/article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { optionalUserProcedure } from '../middleware/optional-user'
import { userProcedure } from '../middleware/user'
import { type ArticleProvider as BaseArticleProvider } from '$base/server/article'
import { ArticleProvider, articles } from '~/server/singleton/service'
import { GucchoError } from '~/def/messages'

export const router = _router({
get: userProcedure
Expand All @@ -21,7 +22,7 @@ export const router = _router({
if (!r) {
const notFound = ArticleProvider.fallbacks.get('404')
if (!notFound) {
throw new Error('404 not found')
throwGucchoError(GucchoError.ArticleNotFound)
}
const html = notFound.dynamic ? await ArticleProvider.render(notFound.json) : notFound.html

Expand Down Expand Up @@ -49,7 +50,7 @@ export const router = _router({
if (!r) {
const notFound = ArticleProvider.fallbacks.get('404')
if (!notFound) {
throw new Error('404 not found')
throwGucchoError(GucchoError.ArticleNotFound)
}
const html = notFound.dynamic ? await ArticleProvider.render(notFound.json) : notFound.html

Expand Down
8 changes: 8 additions & 0 deletions src/server/utils/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function createGucchoError(code: GucchoError): TRPCError {
case GucchoError.RequireAdminPrivilege:
case GucchoError.OldPasswordMismatch:
case GucchoError.IncorrectPassword:
case GucchoError.InsufficientPrivilegeToEditArticle:
{
return new TRPCError(merge({ code: 'UNAUTHORIZED' }))
}
Expand All @@ -36,6 +37,8 @@ export function createGucchoError(code: GucchoError): TRPCError {
case GucchoError.EmptyPassword:
case GucchoError.InvalidId:
case GucchoError.HackerTryingToDeleteAllAvatars:
case GucchoError.FileSystemArticlePathOutsideArticleRoot:
case GucchoError.TryingToDeleteFallbackContents:
{
return new TRPCError(merge({ code: 'BAD_REQUEST' }))
}
Expand All @@ -50,6 +53,7 @@ export function createGucchoError(code: GucchoError): TRPCError {
}

case GucchoError.ModeNotSupported:
case GucchoError.ModeOrRulesetNotSupported:
case GucchoError.EmailTokenNotFound:
case GucchoError.RelationNotFound:
case GucchoError.SessionNotFound:
Expand All @@ -58,6 +62,7 @@ export function createGucchoError(code: GucchoError): TRPCError {
case GucchoError.AtLeastOneUserNotExists:
case GucchoError.ScoreNotFound:
case GucchoError.ClanNotFound:
case GucchoError.ArticleNotFound:
{
return new TRPCError(merge({ code: 'NOT_FOUND' }))
}
Expand All @@ -73,6 +78,9 @@ export function createGucchoError(code: GucchoError): TRPCError {
return new TRPCError(merge({ code: 'INTERNAL_SERVER_ERROR' }))
}

case GucchoError.AssertionError: {
return assertNotReachable()
}
default: {
assertNotReachable(code)
}
Expand Down

0 comments on commit 111eafc

Please sign in to comment.