Fix: Handle RPC errors gracefully

This commit is contained in:
gpt-engineer-app[bot]
2025-10-30 19:07:38 +00:00
parent 16940419e5
commit ff2215f450
4 changed files with 100 additions and 27 deletions

View File

@@ -86,11 +86,20 @@ export function UserRoleManager() {
const userIds = [...new Set((data || []).map(r => r.user_id))];
// Fetch user profiles with emails (for admins)
const {
data: allProfiles
} = await supabase.rpc('get_users_with_emails');
let profiles: Array<{ user_id: string; username: string; display_name?: string }> | null = null;
const { data: allProfiles, error: rpcError } = await supabase
.rpc('get_users_with_emails');
const profiles = allProfiles?.filter(p => userIds.includes(p.user_id));
if (rpcError) {
logger.warn('Failed to fetch users with emails, using basic profiles', { error: getErrorMessage(rpcError) });
const { data: basicProfiles } = await supabase
.from('profiles')
.select('user_id, username, display_name')
.in('user_id', userIds);
profiles = basicProfiles as typeof profiles;
} else {
profiles = allProfiles?.filter(p => userIds.includes(p.user_id)) || null;
}
const profileMap = new Map(profiles?.map(p => [p.user_id, p]) || []);
// Combine data with profiles
@@ -114,17 +123,26 @@ export function UserRoleManager() {
return;
}
try {
const {
data: allUsers,
error
} = await supabase.rpc('get_users_with_emails');
let data;
const { data: allUsers, error: rpcError } = await supabase
.rpc('get_users_with_emails');
// Filter by search term
const data = allUsers?.filter(user =>
user.username.toLowerCase().includes(search.toLowerCase()) ||
user.display_name?.toLowerCase().includes(search.toLowerCase())
).slice(0, 10);
if (error) throw error;
if (rpcError) {
logger.warn('Failed to fetch users with emails, using basic profiles', { error: getErrorMessage(rpcError) });
const { data: basicProfiles, error: profilesError } = await supabase
.from('profiles')
.select('user_id, username, display_name')
.ilike('username', `%${search}%`);
if (profilesError) throw profilesError;
data = basicProfiles?.slice(0, 10);
} else {
// Filter by search term
data = allUsers?.filter(user =>
user.username.toLowerCase().includes(search.toLowerCase()) ||
user.display_name?.toLowerCase().includes(search.toLowerCase())
).slice(0, 10);
}
// Filter out users who already have roles
const existingUserIds = userRoles.map(ur => ur.user_id);