{"version":3,"file":"toolbox-error.mjs","sources":["../../../projects/toolbox/error/error.service.ts","../../../projects/toolbox/error/global-error.service.ts","../../../projects/toolbox/error/toolbox-error.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\r\nimport { inject, Injectable } from \"@angular/core\";\r\nimport { Location } from \"@angular/common\";\r\nimport { HttpClient, HttpHeaders } from \"@angular/common/http\";\r\nimport { firstValueFrom, of } from \"rxjs\";\r\nimport { catchError } from \"rxjs/operators\";\r\n\r\nimport { TbxError, parseApiError } from \"@lacera/ngx-toolbox/http\";\r\n\r\n/**\r\n * A service to submit application errors to the administrators.\r\n */\r\n@Injectable( { providedIn: \"root\" } )\r\nexport class TbxErrorService {\r\n  /** Gets or sets the error last logged by this service. */\r\n  public lastError: TbxError | null = null;\r\n\r\n  /** Gets or sets the URL where the message can be submitted. */\r\n  public submitErrorUrl = \"api/toolbox/error\";\r\n\r\n  private readonly http = inject( HttpClient );\r\n  private readonly location = inject( Location );\r\n\r\n  /**\r\n   * Gets an alert observable to which a caller can subscribe to be notified when\r\n   * a new alert is added.\r\n   * @param error The error to log.\r\n   * @returns An observable of to subscribe to for new alerts.\r\n   */\r\n  public log( error: any ): TbxError {\r\n    // Format the error into a toolbox error\r\n    this.lastError = parseApiError( error );\r\n\r\n    // Log the error with active log publishers\r\n    console.error( this.lastError.message );\r\n\r\n    console.group( \"TbxErrorService\" );\r\n    console.error( error );\r\n    console.groupEnd();\r\n\r\n    return this.lastError;\r\n  }\r\n\r\n  /**\r\n   * Submits an error to the server for the administrators.\r\n   * @param error The error to submit.\r\n   */\r\n  public async submitError( error: TbxError ): Promise<void> {\r\n    const url = this.location.prepareExternalUrl( this.submitErrorUrl );\r\n\r\n    const http = this.http.post(\r\n      url,\r\n      error,\r\n      {\r\n        withCredentials: true,\r\n        headers: new HttpHeaders( {\r\n          \"content-Type\": \"application/json; charset=utf-8\",\r\n          \"x-Requested-With\": \"XMLHttpRequest\"\r\n        } )\r\n      }\r\n    );\r\n\r\n    await firstValueFrom(\r\n      http.pipe(\r\n        catchError( ( err: any ) => {\r\n          console.error( \"Failed to submit the error to the programmers:\", err );\r\n\r\n          return of( false );\r\n        } ) )\r\n    );\r\n  }\r\n}\r\n","/* eslint-disable @typescript-eslint/no-explicit-any */\r\nimport { ErrorHandler, Injectable, Injector } from \"@angular/core\";\r\nimport { TbxErrorService } from \"./error.service\";\r\n\r\n/**\r\n * Global error handling appropriate to LACERA Angular applications.\r\n */\r\n@Injectable( { providedIn: \"root\" } )\r\nexport class TbxGlobalErrorHandler implements ErrorHandler {\r\n  public constructor( private readonly injector: Injector ) {\r\n  }\r\n\r\n  /**\r\n   * Logs the error to the console and sends it to the ToolBox to store it on\r\n   * the server logs, which may also notify the administrators.\r\n   * @param error - The error that occurred in the application.\r\n   */\r\n  public handleError( error: any ): void {\r\n    // We have to use the injector since the global error handler gets registered\r\n    // before the services, so they would not be available for ctor injection\r\n    const errorService = this.getErrorService();\r\n\r\n    // Log the error with the registered publishers, if any\r\n    if( errorService !== null ) {\r\n      errorService.log( error );\r\n    }\r\n\r\n    console.group( \"GlobalError\" );\r\n    console.error( error );\r\n    console.groupEnd();\r\n\r\n    // IMPORTANT: Don't Rethrow the error otherwise it will not emit errors after once\r\n    // eslint-disable-next-line max-len\r\n    // https://stackoverflow.com/questions/44356040/angular-global-error-handler-working-only-once\r\n    // throw errorResponse;\r\n  }\r\n\r\n  private getErrorService(): TbxErrorService | null {\r\n    try {\r\n      return this.injector.get( TbxErrorService );\r\n    }\r\n    catch( e ) {\r\n      console.error( \"Error injecting the error service\", e );\r\n\r\n      return null;\r\n    }\r\n  }\r\n}\r\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;;;;;AAAA;AASA;;AAEG;MAEU,eAAe,CAAA;AAD5B,IAAA,WAAA,GAAA;;QAGS,IAAA,CAAA,SAAS,GAAoB,IAAI;;QAGjC,IAAA,CAAA,cAAc,GAAG,mBAAmB;AAE1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