All files / src/app/shared/components/common/error-message error-message.component.ts

95% Statements 19/20
90% Branches 9/10
100% Functions 7/7
94.73% Lines 18/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 5543x 43x   43x 43x                               43x   13x         13x 13x       11x 6x     5x           6x     6x 5x 5x   6x 7x 6x      
import { ChangeDetectionStrategy, Component, Input, OnChanges, SecurityContext } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
 
import { MessageFacade } from 'ish-core/facades/message.facade';
import { HttpErrorHelper } from 'ish-core/models/http-error/http-error.helper';
import { HttpError } from 'ish-core/models/http-error/http-error.model';
 
/**
 * The Error Message Component displays an error message for an {@link HttpError}.
 * The ICM messages are rendered as sanitized HTML with escaped parameter values, see {@link HttpErrorHelper.formatMessage}.
 *
 * @example
   <ish-error-message [error]="error" />
 */
@Component({
  selector: 'ish-error-message',
  standalone: false,
  templateUrl: './error-message.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ErrorMessageComponent implements OnChanges {
  @Input({ required: true }) error: Partial<HttpError>;
  @Input() toast = true;
 
  message: string;
 
  constructor(
    private messageFacade: MessageFacade,
    private sanitizer: DomSanitizer
  ) {}
 
  ngOnChanges() {
    if (this.toast) {
      this.displayToast(this.error);
    } else {
      // the ishServerHtml directive does not sanitize, so the message is sanitized here
      this.message = this.sanitizer.sanitize(SecurityContext.HTML, HttpErrorHelper.formatMessage(this.error));
    }
  }
 
  // toasts are sanitized by the toast service
  private displayToast(err: Partial<HttpError>) {
    Iif (!err) {
      return;
    }
    const messages = (err.errors ?? [])
      .map(feedback => HttpErrorHelper.formatFeedback(feedback))
      .filter(message => !!message);
 
    (messages.length ? messages : [err.message || err.code])
      .filter(message => !!message)
      .forEach(message => this.messageFacade.error({ message }));
  }
}