]> juplo.de Git - demos/kafka/chat/commitdiff
feat: `user.component` handels errors frontend
authorKai Moritz <kai@juplo.de>
Mon, 13 Oct 2025 20:19:41 +0000 (22:19 +0200)
committerKai Moritz <kai@juplo.de>
Mon, 13 Oct 2025 20:20:01 +0000 (22:20 +0200)
src/app/user/user.component.html
src/app/user/user.component.ts

index 721d7023553e8361deb6a32fe520c55a6e1f2e56..91e91234b28a57d14065681aa86b48b19623bb0d 100644 (file)
@@ -3,16 +3,21 @@
   <div class="container card-body form-inline">
     <div class="row form-group">
       <div class="col input-group input-group-primary">
-        <button type="submit" class="btn btn-primary" (click)="createUserId()">Create a new user-ID</button>
+        <button type="submit" class="btn btn-primary" (click)="createUserId()" [disabled]="loading">Create a new user-ID</button>
       </div>
       <div class="col input-group input-group-primary">
         <label for="id" class="input-group-text">User-ID</label>
         <input id="id" type="text" class="form-control" placeholder="Enter your user-ID" [formControl]="useridForm">
-        <button type="submit" class="btn btn-primary" (click)="updateUserId()" [disabled]="useridForm.invalid">Send</button>
+        <button type="submit" class="btn btn-primary" (click)="updateUserId()" [disabled]="useridForm.invalid || loading">Send</button>
       </div>
     </div>
   </div>
   <div class="container card-footer">
+    @if (error) {
+    <div class="row">
+      <div class="col alert alert-primary" role="alert">{{ error }}</div>
+    </div>
+    }
     <div class="row">
       <div class="col">Value: {{ useridForm.value }}</div>
       <div class="col">Form Status: {{ useridForm.status }}</div>
index 027261c2c1e9dfde2c57fb63b093cb13b8a5d1d6..ffec960e721dd2931c071b4c4a9a072444dbf369 100644 (file)
@@ -1,13 +1,7 @@
-import { Component } from '@angular/core';
-import {
-  FormControl,
-  ReactiveFormsModule,
-  Validators,
-  ValidationErrors,
-} from '@angular/forms';
+import { Component, inject } from '@angular/core';
+import { FormControl, ReactiveFormsModule, ValidationErrors, Validators, } from '@angular/forms';
 import { Router } from "@angular/router";
 import { UserService } from "./index";
-import { UUID_V4_FORMAT } from '@pact-foundation/pact/src/dsl/matchers';
 
 
 const UUID_FORMAT = "^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$";
@@ -20,6 +14,8 @@ const UUID_FORMAT = "^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$";
 })
 export class UserComponent {
 
+  private userService = inject(UserService);
+  private router = inject(Router);
 
   useridForm = new FormControl(
     '',
@@ -29,25 +25,50 @@ export class UserComponent {
       Validators.pattern(UUID_FORMAT)
     ]);
 
-  updateUserId(): void {
+  error?: string;
+  loading = false;
+
+
+  async updateUserId() {
+    this.error = undefined;
+    this.loading = true;
+
     let input = this.useridForm.getRawValue();
+
     if (input !== null) {
-      this.userService.setUser(input.trim());
-      this.router.navigate(['chatrooms'])
+      try {
+        await this.userService.setUser(input.trim());
+        this.router.navigate(['chatrooms'])
+      }
+      catch (error) {
+        const message = (error as any)?.message ?? (error as any)?.error?.message ?? String(error);
+        this.error = 'Fehler beim laden des Benutzers: ' + message;
+      }
+      finally {
+        this.loading = false;
+      }
     }
   }
 
-  createUserId(): void {
-    this.userService.createUser();
+  async createUserId() {
+    this.error = undefined;
+    this.loading = true;
+
+    try {
+      await this.userService.createUser();
+    }
+    catch (error) {
+      const message = (error as any)?.message ?? (error as any)?.error?.message ?? String(error);
+      this.error = 'Fehler beim erzeugen des Benutzers: ' + message;
+    }
+    finally {
+      this.loading = false;
+    }
   }
 
-  noWhitespaceValidator(control: FormControl) : ValidationErrors {
+  private noWhitespaceValidator(control: FormControl) : ValidationErrors {
     const isWhitespace = (control.value || '').trim().length === 0;
     const isValid = !isWhitespace;
     return isValid ? {} : { 'whitespace': true };
   }
-
-  constructor(
-    private userService: UserService,
-    private router: Router) {}
 }