alert-error.component.ts 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { Component, OnDestroy } from '@angular/core';
  2. import { TranslateService } from '@ngx-translate/core';
  3. import { JhiEventManager, JhiAlertService } from 'ng-jhipster';
  4. import { Subscription } from 'rxjs';
  5. @Component({
  6. selector: 'jhi-alert-error',
  7. template: `
  8. <div class="alerts" role="alert">
  9. <div *ngFor="let alert of alerts" [ngClass]="setClasses(alert)">
  10. <ngb-alert *ngIf="alert && alert.type && alert.msg" [type]="alert.type" (close)="alert.close(alerts)">
  11. <pre [innerHTML]="alert.msg"></pre>
  12. </ngb-alert>
  13. </div>
  14. </div>`
  15. })
  16. export class JhiAlertErrorComponent implements OnDestroy {
  17. alerts: any[];
  18. cleanHttpErrorListener: Subscription;
  19. /* tslint:disable */
  20. constructor(private alertService: JhiAlertService, private eventManager: JhiEventManager, private translateService: TranslateService) {
  21. /* tslint:enable */
  22. this.alerts = [];
  23. this.cleanHttpErrorListener = eventManager.subscribe('zipConnectApp.httpError', response => {
  24. let i;
  25. const httpErrorResponse = response.content;
  26. switch (httpErrorResponse.status) {
  27. // connection refused, server not reachable
  28. case 0:
  29. this.addErrorAlert('Server not reachable', 'error.server.not.reachable');
  30. break;
  31. case 400:
  32. const arr = httpErrorResponse.headers.keys();
  33. let errorHeader = null;
  34. let entityKey = null;
  35. arr.forEach(entry => {
  36. if (entry.toLowerCase().endsWith('app-error')) {
  37. errorHeader = httpErrorResponse.headers.get(entry);
  38. } else if (entry.toLowerCase().endsWith('app-params')) {
  39. entityKey = httpErrorResponse.headers.get(entry);
  40. }
  41. });
  42. if (errorHeader) {
  43. const entityName = translateService.instant('global.menu.entities.' + entityKey);
  44. this.addErrorAlert(errorHeader, errorHeader, { entityName });
  45. } else if (httpErrorResponse.error !== '' && httpErrorResponse.error.fieldErrors) {
  46. const fieldErrors = httpErrorResponse.error.fieldErrors;
  47. for (i = 0; i < fieldErrors.length; i++) {
  48. const fieldError = fieldErrors[i];
  49. if (['Min', 'Max', 'DecimalMin', 'DecimalMax'].includes(fieldError.message)) {
  50. fieldError.message = 'Size';
  51. }
  52. // convert 'something[14].other[4].id' to 'something[].other[].id' so translations can be written to it
  53. const convertedField = fieldError.field.replace(/\[\d*\]/g, '[]');
  54. const fieldName = translateService.instant('zipConnectApp.' + fieldError.objectName + '.' + convertedField);
  55. this.addErrorAlert('Error on field "' + fieldName + '"', 'error.' + fieldError.message, { fieldName });
  56. }
  57. } else if (httpErrorResponse.error !== '' && httpErrorResponse.error.message) {
  58. this.addErrorAlert(
  59. httpErrorResponse.error.message,
  60. httpErrorResponse.error.message,
  61. httpErrorResponse.error.params
  62. );
  63. } else {
  64. this.addErrorAlert(httpErrorResponse.error);
  65. }
  66. break;
  67. case 404:
  68. this.addErrorAlert('Not found', 'error.url.not.found');
  69. break;
  70. default:
  71. if (httpErrorResponse.error !== '' && httpErrorResponse.error.message) {
  72. this.addErrorAlert(httpErrorResponse.error.message);
  73. } else {
  74. this.addErrorAlert(httpErrorResponse.error);
  75. }
  76. }
  77. });
  78. }
  79. setClasses(alert) {
  80. return {
  81. toast: !!alert.toast,
  82. [alert.position]: true
  83. };
  84. }
  85. ngOnDestroy() {
  86. if (this.cleanHttpErrorListener !== undefined && this.cleanHttpErrorListener !== null) {
  87. this.eventManager.destroy(this.cleanHttpErrorListener);
  88. this.alerts = [];
  89. }
  90. }
  91. addErrorAlert(message, key?, data?) {
  92. key = key && key !== null ? key : message;
  93. this.alerts.push(
  94. this.alertService.addAlert(
  95. {
  96. type: 'danger',
  97. msg: key,
  98. params: data,
  99. timeout: 5000,
  100. toast: this.alertService.isToast(),
  101. scoped: true
  102. },
  103. this.alerts
  104. )
  105. );
  106. }
  107. }