diff --git a/commafeed-client/src/app/client.test.ts b/commafeed-client/src/app/client.test.ts new file mode 100644 index 00000000..0983cf5f --- /dev/null +++ b/commafeed-client/src/app/client.test.ts @@ -0,0 +1,36 @@ +import { i18n } from "@lingui/core" +import type { AxiosError } from "axios" +import { afterEach, describe, expect, it, vi } from "vitest" +import { errorToStrings } from "./client" + +const axiosError = (status: number, data: unknown) => + ({ + isAxiosError: true, + response: { status, data }, + }) as AxiosError + +describe("errorToStrings", () => { + afterEach(() => vi.restoreAllMocks()) + + it("translates known application error types", () => { + vi.spyOn(i18n, "_").mockReturnValue("Translated authentication error") + const error = axiosError(401, { + type: "WRONG_USERNAME_OR_PASSWORD", + message: "wrong username or password", + }) + + expect(errorToStrings(error)).toEqual(["Translated authentication error"]) + }) + + it("preserves backend messages for unexpected errors", () => { + const error = axiosError(500, { message: "unexpected error" }) + + expect(errorToStrings(error)).toEqual(["unexpected error"]) + }) + + it("preserves backend messages for unknown application error types", () => { + const error = axiosError(400, { type: "UNKNOWN_ERROR", message: "unknown error" }) + + expect(errorToStrings(error)).toEqual(["unknown error"]) + }) +}) diff --git a/commafeed-client/src/app/client.ts b/commafeed-client/src/app/client.ts index 25661e34..2e68b68b 100644 --- a/commafeed-client/src/app/client.ts +++ b/commafeed-client/src/app/client.ts @@ -1,3 +1,5 @@ +import { i18n, type MessageDescriptor } from "@lingui/core" +import { msg } from "@lingui/core/macro" import axios, { type AxiosError } from "axios" import type { AddCategoryRequest, @@ -6,6 +8,8 @@ import type { Category, CategoryModificationRequest, CollapseRequest, + CommaFeedApplicationError, + CommaFeedExceptionType, Entries, FeedInfo, FeedInfoRequest, @@ -31,6 +35,10 @@ import type { UserModel, } from "./types" +const applicationErrorMessages = { + WRONG_USERNAME_OR_PASSWORD: msg`Wrong username or password`, +} satisfies Record + const axiosInstance = axios.create({ baseURL: "./rest", withCredentials: true }) axiosInstance.interceptors.response.use( response => response, @@ -128,14 +136,25 @@ export const errorToStrings = (err: unknown) => { let strings: string[] = [] if (axios.isAxiosError(err) && err.response) { - if (typeof err.response.data === "string") strings.push(err.response.data) - if (isMessageError(err)) strings.push(err.response.data.message) - if (isMessageArrayError(err)) strings = [...strings, ...err.response.data.errors] + if (isCommaFeedApplicationError(err)) { + strings.push(i18n._(applicationErrorMessages[err.response.data.type])) + } else { + if (typeof err.response.data === "string") strings.push(err.response.data) + if (isMessageError(err)) strings.push(err.response.data.message) + if (isMessageArrayError(err)) strings = [...strings, ...err.response.data.errors] + } } return strings } +function isCommaFeedApplicationError(err: AxiosError): err is AxiosError { + const data = err.response?.data + if (!data || typeof data !== "object" || !("type" in data)) return false + const type = data.type + return typeof type === "string" && Object.hasOwn(applicationErrorMessages, type) +} + function isMessageError(err: AxiosError): err is AxiosError<{ message: string }> { return !!err.response && !!err.response.data && typeof err.response.data === "object" && "message" in err.response.data } diff --git a/commafeed-client/src/app/types.ts b/commafeed-client/src/app/types.ts index 0e8c81b8..1b7486d5 100644 --- a/commafeed-client/src/app/types.ts +++ b/commafeed-client/src/app/types.ts @@ -338,3 +338,10 @@ export interface AuthenticationError { message: string allowRegistrations: boolean } + +export type CommaFeedExceptionType = "WRONG_USERNAME_OR_PASSWORD" + +export interface CommaFeedApplicationError { + type: CommaFeedExceptionType + message: string +} diff --git a/commafeed-client/src/locales/ar/messages.po b/commafeed-client/src/locales/ar/messages.po index 0b8aadea..0de2a476 100644 --- a/commafeed-client/src/locales/ar/messages.po +++ b/commafeed-client/src/locales/ar/messages.po @@ -1150,6 +1150,10 @@ msgstr "موقع الكتروني" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "مرحباً! يبدو أن هذه هي المرة الأولى التي تقوم فيها بتشغيل CommaFeed. يرجى إنشاء حساب مسؤول للبدء." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "أصفر" diff --git a/commafeed-client/src/locales/ca/messages.po b/commafeed-client/src/locales/ca/messages.po index b5f77395..f9b4c6af 100644 --- a/commafeed-client/src/locales/ca/messages.po +++ b/commafeed-client/src/locales/ca/messages.po @@ -1150,6 +1150,10 @@ msgstr "Lloc web" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "Benvingut! Sembla que aquesta és la primera vegada que executeu CommaFeed. Creeu un compte d'administrador per començar." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Groc" diff --git a/commafeed-client/src/locales/cs/messages.po b/commafeed-client/src/locales/cs/messages.po index 17f8c49f..20b4402b 100644 --- a/commafeed-client/src/locales/cs/messages.po +++ b/commafeed-client/src/locales/cs/messages.po @@ -1150,6 +1150,10 @@ msgstr "Webová stránka" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "Vítejte! Zdá se, že je to poprvé, co spouštíte CommaFeed. Chcete-li začít, vytvořte si účet správce." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Žlutá" diff --git a/commafeed-client/src/locales/cy/messages.po b/commafeed-client/src/locales/cy/messages.po index fee6ddec..f59f582b 100644 --- a/commafeed-client/src/locales/cy/messages.po +++ b/commafeed-client/src/locales/cy/messages.po @@ -1150,6 +1150,10 @@ msgstr "Gwefan" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "Croeso! Ymddengys mai dyma'r tro cyntaf i chi redeg CommaFeed. Creuwch gyfrif gweinyddwr i ddechrau." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Melyn" diff --git a/commafeed-client/src/locales/da/messages.po b/commafeed-client/src/locales/da/messages.po index 0d579b8f..7dab7739 100644 --- a/commafeed-client/src/locales/da/messages.po +++ b/commafeed-client/src/locales/da/messages.po @@ -1150,6 +1150,10 @@ msgstr "Hjemmeside" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "Velkommen! Det ser ud til at være første gang, du kører CommaFeed. Opret venligst en administratorkonto for at komme i gang." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Gul" diff --git a/commafeed-client/src/locales/de/messages.po b/commafeed-client/src/locales/de/messages.po index d05bf3b5..f50cb0b0 100644 --- a/commafeed-client/src/locales/de/messages.po +++ b/commafeed-client/src/locales/de/messages.po @@ -1150,6 +1150,10 @@ msgstr "Webseite" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "Willkommen! Dies scheint das erste Mal zu sein, dass Sie CommaFeed ausführen. Bitte erstellen Sie ein Administrator-Konto, um zu beginnen." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Gelb" diff --git a/commafeed-client/src/locales/en/messages.po b/commafeed-client/src/locales/en/messages.po index 1239a514..d204db08 100644 --- a/commafeed-client/src/locales/en/messages.po +++ b/commafeed-client/src/locales/en/messages.po @@ -1150,6 +1150,10 @@ msgstr "Website" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "Wrong username or password" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Yellow" diff --git a/commafeed-client/src/locales/es/messages.po b/commafeed-client/src/locales/es/messages.po index 6a6a2ccf..6cd26df6 100644 --- a/commafeed-client/src/locales/es/messages.po +++ b/commafeed-client/src/locales/es/messages.po @@ -1151,6 +1151,10 @@ msgstr "Sitio web" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "¡Bienvenido! Parece que esta es la primera vez que ejecutas CommaFeed. Por favor, crea una cuenta de administrador para empezar." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Amarillo" diff --git a/commafeed-client/src/locales/fa/messages.po b/commafeed-client/src/locales/fa/messages.po index 786c3b44..4347713d 100644 --- a/commafeed-client/src/locales/fa/messages.po +++ b/commafeed-client/src/locales/fa/messages.po @@ -1150,6 +1150,10 @@ msgstr "وب سایت" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "خوش آمدید! به نظر می‌رسد این اولین بار است که CommaFeed را اجرا می‌کنید. لطفاً برای شروع یک حساب مدیر ایجاد کنید." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "زرد" diff --git a/commafeed-client/src/locales/fi/messages.po b/commafeed-client/src/locales/fi/messages.po index 31b1a7ba..e35e8b04 100644 --- a/commafeed-client/src/locales/fi/messages.po +++ b/commafeed-client/src/locales/fi/messages.po @@ -1150,6 +1150,10 @@ msgstr "Verkkosivusto" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "Tervetuloa! Näyttää siltä, että suoritat CommaFeediä ensimmäistä kertaa. Luo ylläpitäjän tili aloittaaksesi." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Keltainen" diff --git a/commafeed-client/src/locales/fr/messages.po b/commafeed-client/src/locales/fr/messages.po index a83518ae..151e26e2 100644 --- a/commafeed-client/src/locales/fr/messages.po +++ b/commafeed-client/src/locales/fr/messages.po @@ -1150,6 +1150,10 @@ msgstr "Site web" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "Bienvenue ! Il semble que ce soit le premier démarrage de Commafeed. Avant tout, vous devez créer un compte administrateur." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Jaune" diff --git a/commafeed-client/src/locales/gl/messages.po b/commafeed-client/src/locales/gl/messages.po index babc3f5d..285748b2 100644 --- a/commafeed-client/src/locales/gl/messages.po +++ b/commafeed-client/src/locales/gl/messages.po @@ -1151,6 +1151,10 @@ msgstr "Páxina web" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "Benvida! Semella que é a primeira vez que executas CommaFeed. Para comezar, crea unha conta de administración." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Amarelo" diff --git a/commafeed-client/src/locales/hu/messages.po b/commafeed-client/src/locales/hu/messages.po index 260a797b..5d14360e 100644 --- a/commafeed-client/src/locales/hu/messages.po +++ b/commafeed-client/src/locales/hu/messages.po @@ -1150,6 +1150,10 @@ msgstr "Webhely" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "Üdvözöljük! Úgy tűnik, ez az első alkalom, hogy a CommaFeedet futtatja. Kérjük, hozzon létre egy rendszergazdai fiókot a kezdéshez." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Sárga" diff --git a/commafeed-client/src/locales/id/messages.po b/commafeed-client/src/locales/id/messages.po index 27b6b893..b17dd049 100644 --- a/commafeed-client/src/locales/id/messages.po +++ b/commafeed-client/src/locales/id/messages.po @@ -1150,6 +1150,10 @@ msgstr "Situs Web" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "Selamat datang! Ini sepertinya pertama kalinya Anda menjalankan CommaFeed. Silakan buat akun administrator untuk memulai." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Kuning" diff --git a/commafeed-client/src/locales/it/messages.po b/commafeed-client/src/locales/it/messages.po index d3f07316..b1ba4b78 100644 --- a/commafeed-client/src/locales/it/messages.po +++ b/commafeed-client/src/locales/it/messages.po @@ -1150,6 +1150,10 @@ msgstr "Sito web" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "Benvenuto! Sembra che sia la prima volta che esegui CommaFeed. Crea un account amministratore per iniziare." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Giallo" diff --git a/commafeed-client/src/locales/ja/messages.po b/commafeed-client/src/locales/ja/messages.po index c4c79054..4867b344 100644 --- a/commafeed-client/src/locales/ja/messages.po +++ b/commafeed-client/src/locales/ja/messages.po @@ -1150,6 +1150,10 @@ msgstr "ウェブサイト" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "ようこそ!CommaFeedを初めて実行するようです。開始するには管理者アカウントを作成してください。" +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "イエロー" diff --git a/commafeed-client/src/locales/ko/messages.po b/commafeed-client/src/locales/ko/messages.po index caaeb854..1139dfc2 100644 --- a/commafeed-client/src/locales/ko/messages.po +++ b/commafeed-client/src/locales/ko/messages.po @@ -1150,6 +1150,10 @@ msgstr "웹사이트" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "환영합니다! CommaFeed를 처음 실행하는 것 같습니다. 시작하려면 관리자 계정을 만드십시오." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "옐로우" diff --git a/commafeed-client/src/locales/ms/messages.po b/commafeed-client/src/locales/ms/messages.po index 47641ce8..16479715 100644 --- a/commafeed-client/src/locales/ms/messages.po +++ b/commafeed-client/src/locales/ms/messages.po @@ -1150,6 +1150,10 @@ msgstr "Laman web" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "Selamat datang! Ini nampaknya kali pertama anda menjalankan CommaFeed. Sila cipta akaun pentadbir untuk bermula." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Kuning" diff --git a/commafeed-client/src/locales/nb/messages.po b/commafeed-client/src/locales/nb/messages.po index e282672c..2756dbb8 100644 --- a/commafeed-client/src/locales/nb/messages.po +++ b/commafeed-client/src/locales/nb/messages.po @@ -1150,6 +1150,10 @@ msgstr "Nettsted" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "Velkommen! Det ser ut til å være første gang du kjører CommaFeed. Vennligst opprett en administratorkonto for å komme i gang." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Gul" diff --git a/commafeed-client/src/locales/nl/messages.po b/commafeed-client/src/locales/nl/messages.po index c3556446..53d22f29 100644 --- a/commafeed-client/src/locales/nl/messages.po +++ b/commafeed-client/src/locales/nl/messages.po @@ -1150,6 +1150,10 @@ msgstr "Website" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "Welkom! Dit lijkt de eerste keer te zijn dat je CommaFeed draait. Maak een beheerdersaccount aan om aan de slag te gaan." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Geel" diff --git a/commafeed-client/src/locales/nn/messages.po b/commafeed-client/src/locales/nn/messages.po index 6e1b0e55..26e65f38 100644 --- a/commafeed-client/src/locales/nn/messages.po +++ b/commafeed-client/src/locales/nn/messages.po @@ -1150,6 +1150,10 @@ msgstr "Nettstad" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "Velkomen! Det ser ut til å vera fyrste gongen du køyrer CommaFeed. Ver venleg og opprett ein administratorkonto for å koma i gang." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Gul" diff --git a/commafeed-client/src/locales/pl/messages.po b/commafeed-client/src/locales/pl/messages.po index 87b99d5f..e374bbed 100644 --- a/commafeed-client/src/locales/pl/messages.po +++ b/commafeed-client/src/locales/pl/messages.po @@ -1150,6 +1150,10 @@ msgstr "Strona internetowa" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "Witaj! Wygląda na to, że uruchamiasz CommaFeed po raz pierwszy. Aby rozpocząć, utwórz konto administratora." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Żółty" diff --git a/commafeed-client/src/locales/pt/messages.po b/commafeed-client/src/locales/pt/messages.po index 29d9f98a..b243be2f 100644 --- a/commafeed-client/src/locales/pt/messages.po +++ b/commafeed-client/src/locales/pt/messages.po @@ -1150,6 +1150,10 @@ msgstr "Site" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "Bem-vindo! Esta parece ser a primeira vez que você está executando o CommaFeed. Crie uma conta de administrador para começar." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Amarelo" diff --git a/commafeed-client/src/locales/ru/messages.po b/commafeed-client/src/locales/ru/messages.po index 4f7333b1..14b31887 100644 --- a/commafeed-client/src/locales/ru/messages.po +++ b/commafeed-client/src/locales/ru/messages.po @@ -1150,6 +1150,10 @@ msgstr "Веб-сайт" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "Добро пожаловать! Похоже, вы запускаете CommaFeed в первый раз. Пожалуйста, создайте учетную запись администратора, чтобы начать работу." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Желтый" diff --git a/commafeed-client/src/locales/sk/messages.po b/commafeed-client/src/locales/sk/messages.po index c41f01cb..15d89b56 100644 --- a/commafeed-client/src/locales/sk/messages.po +++ b/commafeed-client/src/locales/sk/messages.po @@ -1150,6 +1150,10 @@ msgstr "Webová stránka" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "Vitajte! Zdá sa, že CommaFeed spúšťate prvýkrát. Ak chcete začať, vytvorte si účet správcu." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Žltá" diff --git a/commafeed-client/src/locales/sv/messages.po b/commafeed-client/src/locales/sv/messages.po index 45a55941..0fff2c70 100644 --- a/commafeed-client/src/locales/sv/messages.po +++ b/commafeed-client/src/locales/sv/messages.po @@ -1150,6 +1150,10 @@ msgstr "Webbplats" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "Välkommen! Det verkar vara första gången du kör CommaFeed. Skapa ett administratörskonto för att komma igång." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Gul" diff --git a/commafeed-client/src/locales/tr/messages.po b/commafeed-client/src/locales/tr/messages.po index 4e77c8b3..910bc7bf 100644 --- a/commafeed-client/src/locales/tr/messages.po +++ b/commafeed-client/src/locales/tr/messages.po @@ -1150,6 +1150,10 @@ msgstr "Web sitesi" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "Hoş geldiniz! Görünüşe göre CommaFeed'i ilk kez çalıştırıyorsunuz. Başlamak için lütfen bir yönetici hesabı oluşturun." +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "Sarı" diff --git a/commafeed-client/src/locales/zh/messages.po b/commafeed-client/src/locales/zh/messages.po index 8d41c712..868dfdc7 100644 --- a/commafeed-client/src/locales/zh/messages.po +++ b/commafeed-client/src/locales/zh/messages.po @@ -1150,6 +1150,10 @@ msgstr "网站" msgid "Welcome! This appears to be the first time you're running CommaFeed. Please create an administrator account to get started." msgstr "欢迎!当前页仅当您第一次使用CommaFeed时出现,请创建一个管理员帐号以开始使用。" +#: src/app/client.ts +msgid "Wrong username or password" +msgstr "用户名或密码错误" + #: src/components/settings/DisplaySettings.tsx msgid "Yellow" msgstr "黄" diff --git a/commafeed-server/src/main/java/com/commafeed/CommaFeedApplicationException.java b/commafeed-server/src/main/java/com/commafeed/CommaFeedApplicationException.java new file mode 100644 index 00000000..3264e12a --- /dev/null +++ b/commafeed-server/src/main/java/com/commafeed/CommaFeedApplicationException.java @@ -0,0 +1,20 @@ +package com.commafeed; + +import java.io.Serial; +import java.util.Objects; + +public class CommaFeedApplicationException extends RuntimeException { + + @Serial private static final long serialVersionUID = 1L; + + private final CommaFeedExceptionType type; + + public CommaFeedApplicationException(CommaFeedExceptionType type) { + super(Objects.requireNonNull(type).message()); + this.type = type; + } + + public CommaFeedExceptionType type() { + return type; + } +} diff --git a/commafeed-server/src/main/java/com/commafeed/CommaFeedExceptionType.java b/commafeed-server/src/main/java/com/commafeed/CommaFeedExceptionType.java new file mode 100644 index 00000000..c42870e9 --- /dev/null +++ b/commafeed-server/src/main/java/com/commafeed/CommaFeedExceptionType.java @@ -0,0 +1,23 @@ +package com.commafeed; + +import org.jboss.resteasy.reactive.RestResponse.Status; + +public enum CommaFeedExceptionType { + WRONG_USERNAME_OR_PASSWORD(Status.UNAUTHORIZED, "wrong username or password"); + + private final Status status; + private final String message; + + CommaFeedExceptionType(Status status, String message) { + this.status = status; + this.message = message; + } + + public Status status() { + return status; + } + + public String message() { + return message; + } +} diff --git a/commafeed-server/src/main/java/com/commafeed/ExceptionMappers.java b/commafeed-server/src/main/java/com/commafeed/ExceptionMappers.java index da6f9c4e..de8f8083 100644 --- a/commafeed-server/src/main/java/com/commafeed/ExceptionMappers.java +++ b/commafeed-server/src/main/java/com/commafeed/ExceptionMappers.java @@ -26,6 +26,18 @@ public class ExceptionMappers { private final CookieService cookieService; private final CommaFeedConfiguration config; + @ServerExceptionMapper(CommaFeedApplicationException.class) + public RestResponse applicationError( + CommaFeedApplicationException e) { + ResponseBuilder response = + ResponseBuilder.create( + e.type().status(), new CommaFeedApplicationError(e.type(), e.getMessage())); + if (e.type().status() == Status.UNAUTHORIZED) { + response.cookie(cookieService.buildLogoutCookie()); + } + return response.build(); + } + @ServerExceptionMapper(UnauthorizedException.class) public RestResponse unauthorized(UnauthorizedException e) { return RestResponse.status( @@ -55,4 +67,7 @@ public class ExceptionMappers { @RegisterForReflection public record ValidationFailed(String message) {} + + @RegisterForReflection + public record CommaFeedApplicationError(CommaFeedExceptionType type, String message) {} } diff --git a/commafeed-server/src/main/java/com/commafeed/security/identity/DatabaseUsernamePasswordIdentityProvider.java b/commafeed-server/src/main/java/com/commafeed/security/identity/DatabaseUsernamePasswordIdentityProvider.java index 93826dd4..cb342687 100644 --- a/commafeed-server/src/main/java/com/commafeed/security/identity/DatabaseUsernamePasswordIdentityProvider.java +++ b/commafeed-server/src/main/java/com/commafeed/security/identity/DatabaseUsernamePasswordIdentityProvider.java @@ -1,11 +1,12 @@ package com.commafeed.security.identity; +import com.commafeed.CommaFeedApplicationException; +import com.commafeed.CommaFeedExceptionType; import com.commafeed.backend.dao.UnitOfWork; import com.commafeed.backend.model.User; import com.commafeed.backend.model.UserRole.Role; import com.commafeed.backend.service.UserService; -import io.quarkus.security.AuthenticationFailedException; import io.quarkus.security.identity.AuthenticationRequestContext; import io.quarkus.security.identity.IdentityProvider; import io.quarkus.security.identity.SecurityIdentity; @@ -48,7 +49,8 @@ public class DatabaseUsernamePasswordIdentityProvider new String( request.getPassword().getPassword()))); if (user.isEmpty()) { - throw new AuthenticationFailedException("wrong username or password"); + throw new CommaFeedApplicationException( + CommaFeedExceptionType.WRONG_USERNAME_OR_PASSWORD); } Set roles = unitOfWork.call(() -> userService.getRoles(user.get())); diff --git a/commafeed-server/src/test/java/com/commafeed/e2e/AuthentificationIT.java b/commafeed-server/src/test/java/com/commafeed/e2e/AuthentificationIT.java index 89a8c527..87dc955c 100644 --- a/commafeed-server/src/test/java/com/commafeed/e2e/AuthentificationIT.java +++ b/commafeed-server/src/test/java/com/commafeed/e2e/AuthentificationIT.java @@ -36,7 +36,7 @@ class AuthentificationIT { page.navigate(getLoginPageUrl()); PlaywrightTestUtils.login(page, TestConstants.ADMIN_USERNAME, "wrong_password"); PlaywrightAssertions.assertThat(page.getByRole(AriaRole.ALERT)) - .containsText("wrong username or password"); + .containsText("Wrong username or password"); } @Test diff --git a/commafeed-server/src/test/java/com/commafeed/integration/SecurityIT.java b/commafeed-server/src/test/java/com/commafeed/integration/SecurityIT.java index d6e36fd6..7148a664 100644 --- a/commafeed-server/src/test/java/com/commafeed/integration/SecurityIT.java +++ b/commafeed-server/src/test/java/com/commafeed/integration/SecurityIT.java @@ -1,5 +1,7 @@ package com.commafeed.integration; +import com.commafeed.CommaFeedExceptionType; +import com.commafeed.ExceptionMappers.CommaFeedApplicationError; import com.commafeed.ExceptionMappers.UnauthorizedResponse; import com.commafeed.TestConstants; import com.commafeed.frontend.model.Entries; @@ -57,6 +59,27 @@ class SecurityIT extends BaseIT { .statusCode(HttpStatus.SC_OK); } + @Test + void formLoginWrongPassword() { + CommaFeedApplicationError error = + RestAssured.given() + .auth() + .none() + .formParams( + "j_username", + TestConstants.ADMIN_USERNAME, + "j_password", + "wrong-password") + .post("j_security_check") + .then() + .statusCode(HttpStatus.SC_UNAUTHORIZED) + .extract() + .as(CommaFeedApplicationError.class); + + Assertions.assertEquals(CommaFeedExceptionType.WRONG_USERNAME_OR_PASSWORD, error.type()); + Assertions.assertEquals("wrong username or password", error.message()); + } + @Test void basicAuthLogin() { RestAssured.given()