Using Raven to handle errors

After upgrading to Ionic-V4 i get this error when using Raven

Module ‘"/node_modules/raven-js/typescript/raven"’ has no default export.

Any way to bypass this error?

 import * as Raven from 'raven-js';

Getting the same error

Currently I have just declare

var Raven

and at least i compiles, but I haven’t tested if it works

and that work before? could you show your all code?

Code in Ionic V3, that still works when compiling V3

import { IonicErrorHandler } from ‘ionic-angular’;
import { ErrorHandler } from ‘@angular/core’;
import Raven from ‘raven-js’;

Raven
.config(’’, {
release: ‘0.4.40’,
dataCallback: data => {

        if (data.culprit) {
            data.culprit = data.culprit.substring(data.culprit.lastIndexOf('/'));
        }

        var stacktrace = data.stacktrace || 
                         data.exception && 
                         data.exception.values[0].stacktrace;

        if (stacktrace) {
            stacktrace.frames.forEach(function (frame) {
                frame.filename = frame.filename.substring(frame.filename.lastIndexOf('/'));
            });
        }
    } 
})
.install();

export class SentryErrorHandler extends IonicErrorHandler implements ErrorHandler {

handleError(error) {
    super.handleError(error);
    console.log("Sentry");
    console.error(error);

    try {
        //alert(error);
        Raven.captureException(error.originalError || error);
    }
    catch (e) {
        console.log("Raven error");
        console.error(e);
    }
}

}

and after migration it fails with the above mentioned error

which raven version are you using? I’ve got

 "raven-js": "^3.26.3"

and my provider looks like

import {ErrorHandler} from '@angular/core';

import * as Raven from 'raven-js';

Raven
.config('MY_SENTRY_DNS', {
    release: 'vY.X.Z',
    environment: 'production'
})
.install();

export class SentryErrorHandler extends ErrorHandler {

  handleError(err: any): void {
    super.handleError(err);
    
   Raven.captureException(err);
  }
}

and it just works fine with Ionic v4, like I said the only change regarding v3 was the import

Just notice now, your code can’t work with v4, there isn’t any IonicErrorHandler anymore

In V4 it is replace with

import { ErrorHandler } from ‘@angular/core’;

as usggested in the migration guides.

and same raven version as you. It is probably TypeScript thats is newer

So basically I have the same as you

Inseting your code i get:
image

Did you just not install it with npm when you moved to Ionic 4? Is it in your package.json?

it is in my package.json as

"raven-js": "^3.26.4",

and yes i installed it with

npm install --save raven-js

Something is wrong with your installation. If the same version is being recognized as a module in one setup and not another, you’re missing a piece.

I have 2 installations, one using V3 and one using V4 - when compiling with CLI Ionic 3 it works, when compiling with CLI Ionic V4 it fails

The error you posted has nothing to do with Ionic. Have you compared the d.ts files in the two installations? Here’s a sample of such files from an npm package I published.

It is probably an error in the Raven-file, and I found also a report of the problem on Sentrys website

The Code in Raven looks like this

declare var Raven: Raven.RavenStatic;

export = Raven;

declare module Raven {

and the typescript compiler used in V4 don’t see this as a proper module (And my limited knowledge of typescript prevents me from making a local change)

How can i see the version of the typescript compiler used?

Please post your package.json.

In the V4-project I have

"ts-node": "~7.0.0",
"tslint": "~5.11.0",
"typescript": "~2.7.2"

in the V3-project I have

"@ionic/app-scripts": "3.1.11",
"typescript": "2.4.2",
"ws": "^3.3.2"

I’m running TS 2.9.2 in my v4 projects. You could run

npm install -g typescript@next

see if that makes a difference, and revert to any version you want, if you need to. If it’s really a TS issue, that might solve it.

That installed in fact the new 3.1 Typescript (typescript@3.1.0-dev.20180824), but did not change anything.

Trying to fall back to the 2.4.2 failed as the angular compiler requires at least 2.7.2

But as I said it is probably a Raven bug looking at https://forum.sentry.io/t/how-to-import-raven-js-3-26-with-angular-v6/4139

Could you use the @sentry modules instead of the raven module? That’s the new stuff.

This one https://docs.sentry.io/clients/cordova/ionic/ ?

By the way changing

export = Raven;

to

export default Raven;

in the raven.d.ts file fixed the compiler error