import { BreadCrumb , ExceptionOptions , MessageOptions , SentryUser } from './' ; export class Sentry { public static init ( dsn ) { SentryClient . sharedClient = SentryClient . alloc ( ) . initWithDsnDidFailWithError ( dsn ) ; SentryClient . sharedClient . startCrashHandlerWithError ( ) ; SentryClient . sharedClient . enableAutomaticBreadcrumbTracking ( ) ; } public static captureMessage ( message , options ? ) { const level = options && options . level ? options . level : null ; const event = SentryEvent . alloc ( ) . initWithLevel ( this . _convertSentryLevel ( level ) ) ; event . message = message ; if ( options && options . extra ) { event . extra = NSDictionary . dictionaryWithDictionary ( options . extra as NSDictionary < string , any > ) ; } if ( options && options . tags ) { event . tags = NSDictionary . dictionaryWithDictionary ( options . tags as NSDictionary < string , string > ) ; } SentryClient . sharedClient . sendEventWithCompletionHandler ( event , ( ) => { } ) ; } public static captureException ( exception , options ? ) { const event = SentryEvent . alloc ( ) . initWithLevel ( SentrySeverity . kSentrySeverityError ) ; event . message = JSON . stringify ( { message : exception . message , stacktrace : exception . stack , name : exception . name } ) ; if ( options && options . extra ) { event . extra = NSDictionary . dictionaryWithDictionary ( options . extra as NSDictionary < string , any > ) ; } if ( options && options . tags ) { event . tags = NSDictionary . dictionaryWithDictionary ( options . tags as NSDictionary < string , string > ) ; } SentryClient . sharedClient . sendEventWithCompletionHandler ( event , ( ) => { } ) ; } public static captureBreadcrumb ( breadcrumb ) { const sentryBC = SentryBreadcrumb . alloc ( ) . initWithLevelCategory ( this . _convertSentryLevel ( breadcrumb . level ) , breadcrumb . category ) ; sentryBC . message = breadcrumb . message ; SentryClient . sharedClient . breadcrumbs . addBreadcrumb ( sentryBC ) ; } public static setContextUser ( user ) { const userNative = SentryUser . alloc ( ) . initWithUserId ( user . id ) ; userNative . email = user . email ? user . email : '' ; userNative . username = user . username ? user . username : '' ; if ( user . data ) { const dict = NSDictionary . dictionaryWithDictionary ( user . data as NSDictionary < string , any > ) ; userNative . extra = dict ? dict : null ; } SentryClient . sharedClient . user = userNative ; } public static setContextTags ( tags ) { SentryClient . sharedClient . tags = NSDictionary . dictionaryWithDictionary ( tags as NSDictionary < string , string > ) ; } public static setContextExtra ( extra ) { SentryClient . sharedClient . extra = NSDictionary . dictionaryWithDictionary ( extra as NSDictionary < string , any > ) ; } public static clearContext ( ) { SentryClient . sharedClient . clearContext ( ) ; } default private static _convertSentryLevel ( level ) { if ( ! level ) { return SentrySeverity . kSentrySeverityInfo ; } switch ( level ) { case Level . Info : return SentrySeverity . kSentrySeverityInfo ; case Level . Warning : return SentrySeverity . kSentrySeverityWarning ; case Level . Fatal : return SentrySeverity . kSentrySeverityFatal ; case Level . Error : return SentrySeverity . kSentrySeverityError ; case Level . Debug : return SentrySeverity . kSentrySeverityDebug ; default : return SentrySeverity . kSentrySeverityInfo ; } } } export enum Level { Fatal = 'fatal' , Error = 'error' , Warning = 'warning' , Info = 'info' , Debug = 'debug' }