improve error handling

This commit is contained in:
syuilo 2023-02-12 10:36:43 +09:00
parent 9ddf62d8b7
commit b55d26387b
2 changed files with 18 additions and 3 deletions

View File

@ -14,6 +14,8 @@ import { RoleService } from '@/core/RoleService.js';
@Injectable() @Injectable()
export class UserListService { export class UserListService {
public static TooManyUsersError = class extends Error {};
constructor( constructor(
@Inject(DI.usersRepository) @Inject(DI.usersRepository)
private usersRepository: UsersRepository, private usersRepository: UsersRepository,
@ -36,7 +38,7 @@ export class UserListService {
userListId: list.id, userListId: list.id,
}); });
if (currentCount > (await this.roleService.getUserPolicies(me.id)).userEachUserListsLimit) { if (currentCount > (await this.roleService.getUserPolicies(me.id)).userEachUserListsLimit) {
throw new Error('Too many users'); throw new UserListService.TooManyUsersError();
} }
await this.userListJoiningsRepository.insert({ await this.userListJoiningsRepository.insert({

View File

@ -45,6 +45,12 @@ export const meta = {
code: 'YOU_HAVE_BEEN_BLOCKED', code: 'YOU_HAVE_BEEN_BLOCKED',
id: '990232c5-3f9d-4d83-9f3f-ef27b6332a4b', id: '990232c5-3f9d-4d83-9f3f-ef27b6332a4b',
}, },
tooManyUsers: {
message: 'You can not push users any more.',
code: 'TOO_MANY_USERS',
id: '2dd9752e-a338-413d-8eec-41814430989b',
},
}, },
} as const; } as const;
@ -110,8 +116,15 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
throw new ApiError(meta.errors.alreadyAdded); throw new ApiError(meta.errors.alreadyAdded);
} }
// Push the user try {
await this.userListService.push(user, userList, me); await this.userListService.push(user, userList, me);
} catch (err) {
if (err instanceof UserListService.TooManyUsersError) {
throw new ApiError(meta.errors.tooManyUsers);
}
throw err;
}
}); });
} }
} }