Skip to content

Commit

Permalink
fix(backend/antenna): キーワードが与えられなかった場合のエラーをApiErrorとして投げる (#14491)
Browse files Browse the repository at this point in the history
* fix(backend/antenna): report validation failure as ApiError on update

* test(backend/antenna): reflect change in previous commit

* fix(backend/antenna): report validation failure as ApiError on create

* test(backend/antenna): reflect change in previous commit

* test(backend/antenna): semi

* test(backend/antenna): bring being spread parameters first in object literal

* chore: add CHANGELOG entry

---------

Co-authored-by: syuilo <[email protected]>
  • Loading branch information
KisaragiEffective and syuilo authored Sep 15, 2024
1 parent 1544ba9 commit 6b2072f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
- Fix: 月の違う同じ日はセパレータが表示されないのを修正

### Server
- ファイルがサイズの制限を超えてアップロードされた際にエラーを返さなかった問題を修正
- Fix: アンテナの書き込み時にキーワードが与えられなかった場合のエラーをApiErrorとして投げるように
- この変更により、公式フロントエンドでは入力の不備が内部エラーとして報告される代わりに一般的なエラーダイアログで報告されます
- Fix: ファイルがサイズの制限を超えてアップロードされた際にエラーを返さなかった問題を修正
- Fix: 外部ページを解析する際に、ページに紐づけられた関連リソースも読み込まれてしまう問題を修正
(Cherry-picked from https://activitypub.software/TransFem-org/Sharkey/-/commit/26e0412fbb91447c37e8fb06ffb0487346063bb8)


## 2024.8.0

### General
Expand Down
8 changes: 7 additions & 1 deletion packages/backend/src/server/api/endpoints/antennas/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export const meta = {
code: 'TOO_MANY_ANTENNAS',
id: 'faf47050-e8b5-438c-913c-db2b1576fde4',
},

emptyKeyword: {
message: 'Either keywords or excludeKeywords is required.',
code: 'EMPTY_KEYWORD',
id: '53ee222e-1ddd-4f9a-92e5-9fb82ddb463a',
},
},

res: {
Expand Down Expand Up @@ -87,7 +93,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
) {
super(meta, paramDef, async (ps, me) => {
if (ps.keywords.flat().every(x => x === '') && ps.excludeKeywords.flat().every(x => x === '')) {
throw new Error('either keywords or excludeKeywords is required.');
throw new ApiError(meta.errors.emptyKeyword);
}

const currentAntennasCount = await this.antennasRepository.countBy({
Expand Down
8 changes: 7 additions & 1 deletion packages/backend/src/server/api/endpoints/antennas/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ export const meta = {
code: 'NO_SUCH_USER_LIST',
id: '1c6b35c9-943e-48c2-81e4-2844989407f7',
},

emptyKeyword: {
message: 'Either keywords or excludeKeywords is required.',
code: 'EMPTY_KEYWORD',
id: '721aaff6-4e1b-4d88-8de6-877fae9f68c4',
},
},

res: {
Expand Down Expand Up @@ -85,7 +91,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
super(meta, paramDef, async (ps, me) => {
if (ps.keywords && ps.excludeKeywords) {
if (ps.keywords.flat().every(x => x === '') && ps.excludeKeywords.flat().every(x => x === '')) {
throw new Error('either keywords or excludeKeywords is required.');
throw new ApiError(meta.errors.emptyKeyword);
}
}
// Fetch the antenna
Expand Down
23 changes: 23 additions & 0 deletions packages/backend/test/e2e/antennas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,17 @@ describe('アンテナ', () => {
assert.deepStrictEqual(response, expected);
});

test('を作成する時キーワードが指定されていないとエラーになる', async () => {
await failedApiCall({
endpoint: 'antennas/create',
parameters: { ...defaultParam, keywords: [[]], excludeKeywords: [[]] },
user: alice
}, {
status: 400,
code: 'EMPTY_KEYWORD',
id: '53ee222e-1ddd-4f9a-92e5-9fb82ddb463a'
})
});
//#endregion
//#region 更新(antennas/update)

Expand Down Expand Up @@ -255,6 +266,18 @@ describe('アンテナ', () => {
id: '1c6b35c9-943e-48c2-81e4-2844989407f7',
});
});
test('を変更する時キーワードが指定されていないとエラーになる', async () => {
const antenna = await successfulApiCall({ endpoint: 'antennas/create', parameters: defaultParam, user: alice });
await failedApiCall({
endpoint: 'antennas/update',
parameters: { ...defaultParam, antennaId: antenna.id, keywords: [[]], excludeKeywords: [[]] },
user: alice
}, {
status: 400,
code: 'EMPTY_KEYWORD',
id: '721aaff6-4e1b-4d88-8de6-877fae9f68c4'
})
});

//#endregion
//#region 表示(antennas/show)
Expand Down

0 comments on commit 6b2072f

Please sign in to comment.