Ionic2 / Angular2 integration

Hi everybody,

I’m currently building an Android application using Ionic2 (which is based on Angular2) and I want to integrate Sentry into this application.

To accomplish this I’ve built a simple Environment Provider (source included below) which returns some basic environmental settings, for example, the host for the API endpoints, if we are running in debug mode.

So in my app.modules.ts I initialise Sentry with the following code;

Raven
    .config('https://xxx@sentry.io/xxx')
    .install();

export class RavenErrorHandler implements ErrorHandler {
    private env: Environment;

    constructor(http: Http) {
        this.env = new Environment(http);
    }

    handleError(err:any) : void {
        // If we are running in debug mode, don't report anything to Sentry
        if(!this.env.get('app_debug')) {
             Raven.captureException(err.originalError || err);
        }
    }
}

Basicly what I want to achieve is that I only report errors to Sentry when I’m not in debug mode. But even when I comment out Raven.captureException(err.originalError || err); error are still being reported to Sentry. So any tips on how can I fix this ?

Source ‘EnvironmentStore’

import { Injectable } from '@angular/core';
import {Http, Response} from "@angular/http";
import {Observable} from "rxjs";

@Injectable()
export class Environment {

    private rawData: Object;

    constructor(private http: Http) {
        this.intialise();
        this.rawData = {};
    }

    public get(name: string) {
        if(this.rawData[name]) {
            return this.rawData[name];
        } else {
            return null;
        }
    }

    private intialise() {
        this.http.get('./assets/env.json')
            .map((res: Response) => res.json())
            .catch((error:any) => Observable.throw("No env.json found"))
            .subscribe(data => {
                this.populate(data);
            });
    }

    private populate(data: Object) {
        this.rawData = data;
    }
}

assets/env.json file

{
    "base_url" : "https://api.xyz.dev",
    "app_debug" : true,
}
1 Like

How did you manage to make sentry work with ionic 2/3? Can you post your full app.modules.ts file please?