Angular catching a 404 file not found error while using ngx-doc-viewer

701 views Asked by At

I have implemented ngx-doc-viewer to be able to render docx files in my webapp. Since the webapp is multilanguage, I do have different docx for every implemented language and I do load the correct docx, depending on the current language selected. Now I would like to handle the scenario where the docx file of a certain language is, for some reason, missing which will lead me to load a default docx file (let's say the english one). I think I need to somehow catch the response error I get in the console, when the component cannot find the target file, but I don't know how. Can you help me please? Thanks!!

Here's some of my code:

HTML:

<mat-dialog-content class="text">
    <ngx-doc-viewer [url]="docUrl" [viewer]="viewer"></ngx-doc-viewer>
</mat-dialog-content>

TS:

import { Component, Inject, OnInit } from '@angular/core';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
import { viewerType } from 'ngx-doc-viewer';

@Component({
  selector: 'app-privacy-text',
  templateUrl: './privacy-text.component.html',
  styleUrls: ['./privacy-text.component.scss']
})
export class PrivacyTextComponent implements OnInit {

  docLanguage = navigator.language.split('-')[1].toString().toLocaleLowerCase();

  viewer: viewerType; // google, office, mammoth, pdf, url
  docUrl = '';

  constructor(
    @Inject(MAT_DIALOG_DATA) public data: {width: string, height: string}) { }

  ngOnInit(): void {
    this.viewer = 'mammoth';
    this.docUrl = 'assets/docs/terms_and_conditions-' + this.docLanguage + ".docx";
  }

}

CONSOLE error msg:

GET http://localhost:4200/assets/docs/terms_and_conditions-it.docx [HTTP/1.1 404 Not Found 5ms]

Response:

Cannot GET /assets/docs/terms_and_conditions-it.docx

0

There are 0 answers