replace exception-swallowing with findOptional overloads

This commit is contained in:
Hazelnoot 2025-11-14 19:25:51 -05:00
parent 792d54b0ca
commit 5365db6b20
5 changed files with 8 additions and 10 deletions

View file

@ -8,7 +8,7 @@ import chalk from 'chalk';
import { IsNull } from 'typeorm';
import { DI } from '@/di-symbols.js';
import type { UsersRepository } from '@/models/_.js';
import type { MiLocalUser, MiRemoteUser } from '@/models/User.js';
import type { MiUser, MiLocalUser, MiRemoteUser } from '@/models/User.js';
import type { Config } from '@/config.js';
import type Logger from '@/logger.js';
import { UtilityService } from '@/core/UtilityService.js';
@ -59,7 +59,7 @@ export class RemoteUserResolveService {
const acct = Acct.toString({ username, host }); // username+host -> acct (handle)
// Try fetch from DB
let user = await this.cacheService.findUserByAcct(acct).catch(() => null); // Error is expected if the user doesn't exist yet
let user: MiUser | null | undefined = await this.cacheService.findOptionalUserByAcct(acct);
// Opportunistically update remote users
if (user != null && isRemoteUser(user)) {

View file

@ -92,10 +92,9 @@ export class ApDbResolverService implements OnApplicationShutdown {
key: MiUserPublickey;
} | null> {
const key = await this.apPersonService.findPublicKeyByKeyId(keyId);
if (key == null) return null;
const user = await this.cacheService.findUserById(key.userId).catch(() => null) as MiRemoteUser | null;
const user = await this.cacheService.findOptionalRemoteUserById(key.userId);
if (user == null) return null;
if (user.isDeleted) return null;

View file

@ -304,14 +304,14 @@ export class ApPersonService implements OnModuleInit {
withSuspended: opts?.withSuspended ?? true,
};
let userId;
let userId: string | null | undefined;
// Resolve URI -> User ID
const parsed = this.utilityService.parseUri(uri);
if (parsed.local) {
userId = parsed.type === 'users' ? parsed.id : null;
} else {
userId = await this.uriPersonCache.fetch(uri).catch(() => null);
userId = await this.uriPersonCache.fetchMaybe(uri);
}
// No match
@ -319,8 +319,7 @@ export class ApPersonService implements OnModuleInit {
return null;
}
const user = await this.cacheService.findUserById(userId)
.catch(() => null) as MiLocalUser | MiRemoteUser | null;
const user = await this.cacheService.findOptionalUserById(userId) as MiLocalUser | MiRemoteUser | null;
if (user?.isDeleted && !_opts.withDeleted) {
return null;

View file

@ -48,7 +48,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
private readonly cacheService: CacheService,
) {
super(meta, paramDef, async (ps) => {
const user = await this.cacheService.findRemoteUserById(ps.userId).catch(() => null);
const user = await this.cacheService.findOptionalRemoteUserById(ps.userId);
if (!user) {
throw new ApiError(meta.errors.noSuchUser);

View file

@ -138,7 +138,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
if (ps.username) {
user = await this.remoteUserResolveService.resolveUser(ps.username, ps.host ?? null).catch(() => null);
} else if (ps.userId != null) {
user = await this.cacheService.findUserById(ps.userId).catch(() => null);
user = await this.cacheService.findOptionalUserById(ps.userId);
}
if (user == null && ps.host != null) {