-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupSentry.ts
70 lines (56 loc) · 1.98 KB
/
setupSentry.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { exec } from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import * as dotenv from 'dotenv';
dotenv.config();
// Step 1: Install Appwrite and dotenv packages
exec('npm install appwrite dotenv', (err, stdout, stderr) => {
if (err) {
console.error(`Error installing packages: ${stderr}`);
return;
}
console.log(`Packages installed: ${stdout}`);
// Step 2: Add Appwrite initialization and error tracking code
const initCode = `
import { Client, Databases } from 'node-appwrite';
import * as dotenv from 'dotenv';
dotenv.config();
const client = new Client();
client
.setEndpoint(process.env.APPWRITE_ENDPOINT || 'YOUR_APPWRITE_ENDPOINT')
.setProject(process.env.APPWRITE_PROJECT_ID || 'YOUR_PROJECT_ID')
.setKey(process.env.APPWRITE_API_KEY || 'YOUR_API_KEY');
const databases = new Databases(client);
async function logErrorToAppwrite(error: any) {
// Log the error to the terminal
console.error("Logged Error:", error);
try {
const data = {
error: error.message,
stack: error.stack,
timestamp: new Date().toISOString(),
};
const permissions: string[] = []; // Add appropriate permissions if necessary
await databases.createDocument(
process.env.APPWRITE_DATABASE_ID || 'YOUR_DATABASE_ID',
process.env.APPWRITE_COLLECTION_ID || 'YOUR_COLLECTION_ID',
'unique()', // unique ID or provide a specific ID
data,
permissions
);
console.log('Error logged successfully to Appwrite');
} catch (err) {
console.error('Failed to log error to Appwrite:', err);
}
}
process.on('uncaughtException', (error) => {
logErrorToAppwrite(error);
});
process.on('unhandledRejection', (reason, promise) => {
logErrorToAppwrite(reason);
});
`;
const filePath = path.join('src', 'app.ts');
fs.appendFileSync(filePath, initCode, { encoding: 'utf8' });
console.log('Appwrite error tracking code added to app.ts');
});