From 6a3fd249a4909211c258f77b115193823eb77dc1 Mon Sep 17 00:00:00 2001 From: Veljko Tosic Date: Tue, 10 Mar 2026 20:35:52 +0100 Subject: [PATCH] Error handling on signup, login and home view --- .../Common/Validation/Rules/MaxLengthRule.cs | 2 +- .../AuthService/EfAuthService.cs | 8 ++--- front/src/services/api.ts | 9 +++++- front/src/views/HomeView.vue | 29 +++++++++++++++++-- front/src/views/LoginView.vue | 12 +++++--- front/src/views/SignupView.vue | 12 +++++--- 6 files changed, 54 insertions(+), 18 deletions(-) diff --git a/dotnet/AipsCore/Domain/Common/Validation/Rules/MaxLengthRule.cs b/dotnet/AipsCore/Domain/Common/Validation/Rules/MaxLengthRule.cs index 798e177..8999dc5 100644 --- a/dotnet/AipsCore/Domain/Common/Validation/Rules/MaxLengthRule.cs +++ b/dotnet/AipsCore/Domain/Common/Validation/Rules/MaxLengthRule.cs @@ -7,7 +7,7 @@ public class MaxLengthRule : AbstractRule { private readonly string _stringValue; private readonly int _maximumLentgh; - protected override string ErrorCode => "minimum_length"; + protected override string ErrorCode => "maximum_length"; protected override string ErrorMessage => $"Length of '{ValueObjectName}' must be at most {_maximumLentgh} characters"; diff --git a/dotnet/AipsCore/Infrastructure/Authentication/AuthService/EfAuthService.cs b/dotnet/AipsCore/Infrastructure/Authentication/AuthService/EfAuthService.cs index 84f6c74..ccede97 100644 --- a/dotnet/AipsCore/Infrastructure/Authentication/AuthService/EfAuthService.cs +++ b/dotnet/AipsCore/Infrastructure/Authentication/AuthService/EfAuthService.cs @@ -10,12 +10,10 @@ namespace AipsCore.Infrastructure.Authentication.AuthService; public class EfAuthService : IAuthService { - private readonly AipsDbContext _dbContext; private readonly UserManager _userManager; - public EfAuthService(AipsDbContext dbContext, UserManager userManager) + public EfAuthService(UserManager userManager) { - _dbContext = dbContext; _userManager = userManager; } @@ -27,8 +25,8 @@ public class EfAuthService : IAuthService if (!result.Succeeded) { - var errors = string.Join(", ", result.Errors.Select(e => e.Description)); - throw new Exception($"User registration failed: {errors}"); + var validationErrors = result.Errors.Select(e => new ValidationError(e.Code, e.Description)).ToList(); + throw new ValidationException(validationErrors); } await _userManager.AddToRoleAsync(entity, UserRole.User.Name); diff --git a/front/src/services/api.ts b/front/src/services/api.ts index 407ca75..8fcacc4 100644 --- a/front/src/services/api.ts +++ b/front/src/services/api.ts @@ -72,7 +72,14 @@ async function request(method: string, url: string, body?: any, attempt if (!res.ok) { const errorBody = await parseJsonOrThrow(res) - throw Object.assign(new Error(res.statusText || 'Request failed'), { status: res.status, body: errorBody }) + + if (errorBody?.errors) { + const messages = errorBody.errors.map((e: any) => e.message ?? e) + + const err = new Error(messages[0]) as any + err.messages = messages + throw err + } } return await parseJsonOrThrow(res) diff --git a/front/src/views/HomeView.vue b/front/src/views/HomeView.vue index 415c6cc..8f9d8b0 100644 --- a/front/src/views/HomeView.vue +++ b/front/src/views/HomeView.vue @@ -18,7 +18,12 @@ const selectedJoinPolicy = ref(WhiteboardJoinPolicy.FreeToJoin) const maxParticipants = ref(10) const showCreateModal = ref(false) +const newWhiteboardError = ref([]) +const joinWithCodeError = ref([]) + async function handleCreateNewWhiteboard() { + newWhiteboardError.value = [] + if (!whiteboardTitle.value.trim()) { alert('Please enter a title for the whiteboard.') return @@ -37,12 +42,16 @@ async function handleCreateNewWhiteboard() { maxParticipants.value = 10 await router.push({ name: 'whiteboard', params: { id: newWhiteboardId } }) - } catch (e) { - console.error('Failed to create new whiteboard', e) + } catch (e: any) { + newWhiteboardError.value = e.messages || ['Failed to create whiteboard'] + } finally { + whiteboards.isLoading = false } } async function joinWithCode() { + joinWithCodeError.value = [] + if (joinCode.value.length !== 8) { alert('Please enter a valid 8-digit code.') return @@ -59,7 +68,7 @@ async function joinWithCode() { await router.push({ name: 'whiteboard', params: { id: joinResult.whiteboardId } }) } catch (err: any) { - console.error(err) + joinWithCodeError.value = err.messages || ['Failed to join whiteboard with code'] } } @@ -99,6 +108,13 @@ async function joinWithCode() { style="min-height: calc(100vh - 56px);" >
+ +
+
    +
  • {{ errorMessage }}
  • +
+
+