From 925a1aca0020dc46db958f62b6baad677e02de50 Mon Sep 17 00:00:00 2001 From: shubhamsharma9199 Date: Wed, 5 Nov 2025 18:45:06 +0530 Subject: [PATCH] fix: add null safety and type guard for ErrorDialogComponent data fix:logic fix: Handles all data types gracefully improve the typeScript Component --- .../error-dialog/error-dialog.component.html | 4 +-- .../error-dialog/error-dialog.component.ts | 32 ++++++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/app/shared/error-dialog/error-dialog.component.html b/src/app/shared/error-dialog/error-dialog.component.html index 299b066e8..057663538 100644 --- a/src/app/shared/error-dialog/error-dialog.component.html +++ b/src/app/shared/error-dialog/error-dialog.component.html @@ -1,8 +1,8 @@

{{ 'Error Log' | translate }}

-

{{ data }}

- +

{{ displayData }}

+
diff --git a/src/app/shared/error-dialog/error-dialog.component.ts b/src/app/shared/error-dialog/error-dialog.component.ts index 4b41417f0..8462c32fe 100644 --- a/src/app/shared/error-dialog/error-dialog.component.ts +++ b/src/app/shared/error-dialog/error-dialog.component.ts @@ -1,5 +1,5 @@ /** Angular Imports. */ -import { Component, OnInit, Inject } from '@angular/core'; +import { Component, OnInit, Inject, SecurityContext } from '@angular/core'; import { MatDialogRef, MAT_DIALOG_DATA, @@ -9,6 +9,7 @@ import { MatDialogClose } from '@angular/material/dialog'; import { CdkScrollable } from '@angular/cdk/scrolling'; +import { DomSanitizer, SafeHtml } from '@angular/platform-browser'; import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module'; @Component({ @@ -28,12 +29,35 @@ export class ErrorDialogComponent { showAsCode = false; /** * @param {MatDialogRef} dialogRef Component reference to dialog. - * @param {any} data Provides any data. + * @param {unknown} data Provides any data. + * @param {DomSanitizer} sanitizer Service to sanitize HTML content. */ constructor( public dialogRef: MatDialogRef, - @Inject(MAT_DIALOG_DATA) public data: string + @Inject(MAT_DIALOG_DATA) public data: unknown, + private sanitizer: DomSanitizer ) { - this.showAsCode = data.startsWith('
');
+    // Guard for non-string data to avoid runtime error
+    this.showAsCode = typeof data === 'string' && data.startsWith('
');
+  }
+
+  /**
+   * Get display data with proper type safety for template usage.
+   * @returns {string} Safe string representation of the data.
+   */
+  get displayData(): string {
+    if (typeof this.data === 'string') {
+      return this.data;
+    }
+    // Convert non-string data to string representation for display
+    return this.data != null ? JSON.stringify(this.data) : '';
+  }
+
+  /**
+   * Get sanitized HTML content for safe rendering.
+   * @returns {SafeHtml} Sanitized HTML content.
+   */
+  get sanitizedData(): SafeHtml {
+    return this.sanitizer.sanitize(SecurityContext.HTML, this.displayData) || '';
   }
 }