Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to catch malformed JSON lang file when loading. #751

Closed
Harlok opened this issue Dec 8, 2017 · 2 comments
Closed

How to catch malformed JSON lang file when loading. #751

Harlok opened this issue Dec 8, 2017 · 2 comments

Comments

@Harlok
Copy link

Harlok commented Dec 8, 2017

If I write a malformed en.json file by adding a comma at the wrong place, when I run my app, I get a very generic error log: [09:54:31] console.error: ERROR [object Object] from somewhere within the library.

I have tried the following:

this.translate.use('en')
            .subscribe((response)=>{
              console.log("response: " + response);
            },(error)=>{
              console.log("error: " + error);
            })

The Error section never triggers when attempting to load the malformed JSON. Where can I put a catcher to detect that mistake?

I'm using:
"@ngx-translate/core": "^9.0.1",
"@ngx-translate/http-loader": "^2.0.0",

@stevengunneweg
Copy link

stevengunneweg commented Jan 24, 2018

I would also expect a descriptive error in my console when the JSON is malformed. Currently we get no indication of what goes wrong. A missing comma in our translation file cost us time to debug than we'd like to admit.
Could be a dev-environment-only log in my opinion

@CodeAndWeb
Copy link
Member

The best place to catch an invalid json file is creating your own TranslateLoader and handle the error there.

The default implementation is quite simple: https://github.com/ngx-translate/http-loader/blob/master/src/http-loader.ts

public getTranslation(lang: string): any {
    return this.http.get(`${this.prefix}${lang}${this.suffix}`);
}

... or make sure to only work with valid json files... BE might help you. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants