Skip to content

Commit c9ccd14

Browse files
committed
formatting
1 parent c12a13e commit c9ccd14

File tree

1 file changed

+81
-82
lines changed

1 file changed

+81
-82
lines changed

content/guide/crash-reporting-sentry.md

Lines changed: 81 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -56,99 +56,99 @@ npm install plist @sentry/webpack-plugin dotenv -D
5656
Update your `webpack.config.js` to configure Sentry integration:
5757

5858
```js
59-
const webpack = require("@nativescript/webpack");
60-
const { resolve, join, relative } = require("path");
61-
const { readFileSync } = require("fs");
62-
const { parse } = require("plist");
59+
const webpack = require('@nativescript/webpack')
60+
const { resolve, join, relative } = require('path')
61+
const { readFileSync } = require('fs')
62+
const { parse } = require('plist')
6363
// load .env without having to specify cli env flags
64-
require("dotenv").config();
64+
require('dotenv').config()
6565

66-
const SentryCliPlugin = require("@sentry/webpack-plugin").sentryWebpackPlugin;
67-
const SourceMapDevToolPlugin = require("webpack").SourceMapDevToolPlugin;
66+
const SentryCliPlugin = require('@sentry/webpack-plugin').sentryWebpackPlugin
67+
const SourceMapDevToolPlugin = require('webpack').SourceMapDevToolPlugin
6868

69-
const SENTRY_PREFIX = process.env.SENTRY_PREFIX || "app:///";
70-
const SENTRY_SOURCE_MAP_PATH = join(__dirname, "dist", "sourcemaps");
69+
const SENTRY_PREFIX = process.env.SENTRY_PREFIX || 'app:///'
70+
const SENTRY_SOURCE_MAP_PATH = join(__dirname, 'dist', 'sourcemaps')
7171

7272
module.exports = (env) => {
73-
webpack.init(env);
73+
webpack.init(env)
7474

7575
webpack.chainWebpack((config) => {
76-
const isStoreBuild = !!env.production;
77-
const sentryDev = !isStoreBuild && !!env["sentryDev"];
76+
const isStoreBuild = !!env.production
77+
const sentryDev = !isStoreBuild && !!env['sentryDev']
7878

79-
const platform = webpack.Utils.platform.getPlatformName();
79+
const platform = webpack.Utils.platform.getPlatformName()
8080
const projectSlug =
81-
platform === "android"
81+
platform === 'android'
8282
? process.env.SENTRY_PROJECT_SLUG_ANDROID
83-
: process.env.SENTRY_PROJECT_SLUG_IOS;
83+
: process.env.SENTRY_PROJECT_SLUG_IOS
8484
const versionString =
85-
platform === "android"
85+
platform === 'android'
8686
? readFileSync(
87-
resolve(__dirname, "App_Resources/Android/app.gradle"),
88-
"utf8"
87+
resolve(__dirname, 'App_Resources/Android/app.gradle'),
88+
'utf8',
8989
).match(/versionName\s+"([^"]+)"/)[1]
9090
: parse(
9191
readFileSync(
92-
resolve(__dirname, "App_Resources/iOS/Info.plist"),
93-
"utf8"
94-
)
95-
)["CFBundleShortVersionString"];
92+
resolve(__dirname, 'App_Resources/iOS/Info.plist'),
93+
'utf8',
94+
),
95+
)['CFBundleShortVersionString']
9696

97-
const SENTRY_DIST = sentryDev ? `dev-${Date.now()}` : `${Date.now()}`;
98-
const SENTRY_RELEASE = sentryDev ? SENTRY_DIST : versionString;
97+
const SENTRY_DIST = sentryDev ? `dev-${Date.now()}` : `${Date.now()}`
98+
const SENTRY_RELEASE = sentryDev ? SENTRY_DIST : versionString
9999

100-
config.plugin("DefinePlugin").tap((args) => {
100+
config.plugin('DefinePlugin').tap((args) => {
101101
Object.assign(args[0], {
102102
__SENTRY_DIST__: `'${SENTRY_DIST}'`,
103103
__SENTRY_RELEASE__: `'${SENTRY_RELEASE}'`,
104104
__SENTRY_ENVIRONMENT__: `'${
105-
isStoreBuild ? "production" : "development"
105+
isStoreBuild ? 'production' : 'development'
106106
}'`,
107107
__ENABLE_SENTRY__: isStoreBuild || sentryDev,
108108
__SENTRY_PREFIX__: `'${SENTRY_PREFIX}'`,
109109
__SENTRY_DSN_IOS__: JSON.stringify(process.env.SENTRY_DSN_IOS),
110110
__SENTRY_DSN_ANDROID__: JSON.stringify(process.env.SENTRY_DSN_ANDROID),
111-
});
112-
return args;
113-
});
111+
})
112+
return args
113+
})
114114

115115
if (isStoreBuild || sentryDev) {
116-
config.devtool(false);
116+
config.devtool(false)
117117

118118
config
119-
.plugin("SourceMapDevToolPlugin|sentry")
119+
.plugin('SourceMapDevToolPlugin|sentry')
120120
.use(SourceMapDevToolPlugin, [
121121
{
122122
append: `\n//# sourceMappingURL=${SENTRY_PREFIX}[name].js.map`,
123123
filename: relative(
124124
webpack.Utils.platform.getAbsoluteDistPath(),
125-
join(SENTRY_SOURCE_MAP_PATH, "[name].js.map")
125+
join(SENTRY_SOURCE_MAP_PATH, '[name].js.map'),
126126
),
127127
},
128-
]);
128+
])
129129

130130
config
131-
.plugin("SentryCliPlugin")
131+
.plugin('SentryCliPlugin')
132132
.init(() =>
133133
SentryCliPlugin({
134134
org: process.env.SENTRY_ORG_SLUG,
135135
project: projectSlug,
136136
// force ignore non-legacy sourcemaps
137137
sourcemaps: {
138-
assets: "/dev/null",
138+
assets: '/dev/null',
139139
},
140140
release: {
141141
uploadLegacySourcemaps: {
142142
paths: [
143-
join(__dirname, "dist", "sourcemaps"),
143+
join(__dirname, 'dist', 'sourcemaps'),
144144
webpack.Utils.platform.getAbsoluteDistPath(),
145145
],
146146
urlPrefix: SENTRY_PREFIX,
147147
},
148148
dist: SENTRY_DIST,
149149
cleanArtifacts: true,
150150
deploy: {
151-
env: sentryDev ? "development" : "production",
151+
env: sentryDev ? 'development' : 'production',
152152
},
153153
setCommits: {
154154
auto: true,
@@ -157,46 +157,46 @@ module.exports = (env) => {
157157
...(SENTRY_RELEASE ? { name: SENTRY_RELEASE } : {}),
158158
},
159159
authToken: process.env.SENTRY_AUTH_TOKEN,
160-
})
160+
}),
161161
)
162-
.use(SentryCliPlugin);
162+
.use(SentryCliPlugin)
163163

164-
config.optimization.minimizer("TerserPlugin").tap((args) => {
164+
config.optimization.minimizer('TerserPlugin').tap((args) => {
165165
// we format here otherwise the sourcemaps will be broken
166166
args[0].terserOptions.format = {
167167
...args[0].terserOptions.format,
168168
max_line_len: 1000,
169169
indent_level: 1,
170-
};
171-
return args;
172-
});
170+
}
171+
return args
172+
})
173173
}
174-
});
174+
})
175175

176-
return webpack.resolveConfig();
177-
};
176+
return webpack.resolveConfig()
177+
}
178178
```
179179

180180
## Step 5: Initialize Sentry in Your App
181181

182182
Create `sentry.ts` to initialize Sentry:
183183

184184
```ts
185-
import { Application, Trace, TraceErrorHandler } from "@nativescript/core";
186-
import * as Sentry from "@nativescript-community/sentry";
187-
188-
declare const __SENTRY_DIST__: string;
189-
declare const __SENTRY_RELEASE__: string;
190-
declare const __SENTRY_ENVIRONMENT__: string;
191-
declare const __ENABLE_SENTRY__: boolean;
192-
declare const __SENTRY_PREFIX__: string;
193-
declare const __SENTRY_DSN_IOS__: string;
194-
declare const __SENTRY_DSN_ANDROID__: string;
195-
196-
let initialized = false;
185+
import { Application, Trace, TraceErrorHandler } from '@nativescript/core'
186+
import * as Sentry from '@nativescript-community/sentry'
187+
188+
declare const __SENTRY_DIST__: string
189+
declare const __SENTRY_RELEASE__: string
190+
declare const __SENTRY_ENVIRONMENT__: string
191+
declare const __ENABLE_SENTRY__: boolean
192+
declare const __SENTRY_PREFIX__: string
193+
declare const __SENTRY_DSN_IOS__: string
194+
declare const __SENTRY_DSN_ANDROID__: string
195+
196+
let initialized = false
197197
export function initSentry() {
198-
if (initialized || !__ENABLE_SENTRY__) return;
199-
initialized = true;
198+
if (initialized || !__ENABLE_SENTRY__) return
199+
initialized = true
200200

201201
Sentry.init({
202202
dsn: __APPLE__ ? __SENTRY_DSN_IOS__ : __SENTRY_DSN_ANDROID__,
@@ -211,64 +211,64 @@ export function initSentry() {
211211
environment: __SENTRY_ENVIRONMENT__,
212212
appPrefix: __SENTRY_PREFIX__,
213213
appHangsTimeoutInterval: 5,
214-
});
215-
216-
Application.on("uncaughtError", (event) =>
217-
Sentry.captureException(event.error)
218-
);
219-
Application.on("discardedError", (event) =>
220-
Sentry.captureException(event.error)
221-
);
222-
Trace.setErrorHandler(errorHandler);
214+
})
215+
216+
Application.on('uncaughtError', (event) =>
217+
Sentry.captureException(event.error),
218+
)
219+
Application.on('discardedError', (event) =>
220+
Sentry.captureException(event.error),
221+
)
222+
Trace.setErrorHandler(errorHandler)
223223
}
224224

225225
const errorHandler: TraceErrorHandler = {
226226
handlerError(error: Error) {
227227
if (__DEV__) {
228228
// (development) - log it
229-
console.error(error);
229+
console.error(error)
230230
// (development) - or use Trace writing (categorical logging)
231-
Trace.write(error, Trace.categories.Error);
231+
Trace.write(error, Trace.categories.Error)
232232
// (development) - throw it
233-
throw error;
233+
throw error
234234
}
235235

236236
// (production) - send it to sentry
237-
Sentry.captureException(error);
237+
Sentry.captureException(error)
238238
},
239-
};
239+
}
240240
```
241241

242242
In your main bootstrap file (`app.ts` or `main.ts`), initialize on launch:
243243

244244
```ts
245-
import { initSentry } from "./sentry";
245+
import { initSentry } from './sentry'
246246

247-
Application.on("launch", () => {
248-
initSentry();
249-
});
247+
Application.on('launch', () => {
248+
initSentry()
249+
})
250250
```
251251

252252
## Step 6: Test Your Setup
253253

254254
Trigger a test crash to verify setup:
255255

256256
```ts
257-
throw new Error("Sentry test crash");
257+
throw new Error('Sentry test crash')
258258
```
259259

260260
For native crashes:
261261

262262
- iOS:
263263

264264
```ts
265-
NSString.stringWithString(null);
265+
NSString.stringWithString(null)
266266
```
267267

268268
- Android:
269269

270270
```ts
271-
new java.lang.String(null);
271+
new java.lang.String(null)
272272
```
273273

274274
Your crashes should appear in your Sentry dashboard shortly after triggering.
@@ -282,4 +282,3 @@ Your crashes should appear in your Sentry dashboard shortly after triggering.
282282
---
283283

284284
You're now successfully integrated with Sentry, gaining powerful insights into your app's performance and stability.
285-

0 commit comments

Comments
 (0)