Techniques Mod 6 - TecProg-2018-2/TecProg-VoxPop-WebApp GitHub Wiki

Examples

Example 1 - Logging (T29)

LogginService

npm install @ngx-toolkit/logger --save

/**
 * Outputs an error message.
 */
error(message?: any, ...optionalParams: any[]) {}
 
/**
 * Outputs a warning message.
 */
warn(message?: any, ...optionalParams: any[]) {}
 
/**
 * Outputs an informational message.
 */
info(message?: any, ...optionalParams: any[]) {}
 
/**
 * Outputs a debug message.
 */
debug(message?: any, ...optionalParams: any[]) {}
 
/**
 * Outputs a message.
 */
log(message?: any, ...optionalParams: any[]) {}

Examples

/**
 * Method responsible for call user logout into header	 * Method responsible for call user logout into header
 */	
  callLogout() {
    this.appComponent.logout(this.cookieService);	
    this.logger.error('[ERROR] Impossible to logout user. Wrong or missing statements: cookie ', this.cookieService );
  }	  

Example 2 - Handle error (T32)

Before

if (statusRequest === 401 || statusRequest === 500 || statusRequest === 400) {
      document.getElementById('contactFail').style.display = 'block';
      return true;
    }
      return false;
  }

After

handleError(error: any) {
    const httpErrorCode = error.status;

    switch (httpErrorCode) {
      case 401:
        document.getElementById('contactFail').style.display = 'block';
        this.router.navigateByUrl('/login');
        break;

      case 400:
        document.getElementById('contactFail').style.display = 'block';
        this.showError(error.message);
        break;

      case 500:
        this.showError('Internal Server error');
        break;

      default:
        this.showError(ContactUsComponent.REFRESH_PAGE_ON_TOAST_CLICK_MESSAGE);
    }
}

  private showError(message: string) {
    this.toastManager.error(message, ContactUsComponent.DEFAULT_ERROR_TITLE, { dismiss: 'controlled'}).then((toast: Toast) => {
            const currentToastId: number = toast.id;
            this.toastManager.onClickToast().subscribe(clickedToast => {
                if (clickedToast.id === currentToastId) {
                    this.toastManager.dismissToast(toast);
                    window.location.reload();
                }
            });
        });
  }

Broken Tests

Tests