5htp-core 0.2.4 → 0.2.5-2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,250 +0,0 @@
1
- /*----------------------------------
2
- - DEPENDANCES
3
- ----------------------------------*/
4
-
5
- // Npm
6
- import { v4 as uuid } from 'uuid';
7
-
8
- // Core
9
- import { SqlError } from '@server/services/database/debug';
10
- import type Console from '.';
11
-
12
- // Types
13
- import type ServerRequest from '@server/services/router/request';
14
-
15
- /*----------------------------------
16
- - TYPES
17
- ----------------------------------*/
18
-
19
- type AppBugInfos = {
20
- user: string,
21
- ip: string,
22
- device: string,
23
-
24
- client: string,
25
- build: string,
26
-
27
- status: string,
28
- side: 'gui' | 'daemon',
29
- action: string,
30
- message: string,
31
- stacktrace: string,
32
- }
33
-
34
- export type ServerBug = {
35
-
36
- type: 'server',
37
-
38
- // Context
39
- hash: string,
40
- date: Date, // Timestamp
41
- channelType?: string,
42
- channelId?: string,
43
-
44
- user: string | null | undefined,
45
- ip: string | null | undefined,
46
-
47
- // Error
48
- error: Error,
49
- stacktrace: string,
50
- logs: string,
51
- }
52
-
53
- export type ApplicationBug = {
54
-
55
- type: 'application',
56
-
57
- // Context
58
- hash: string,
59
- date: Date,
60
- side: string,
61
- action: string,
62
- // User
63
- user: string | null | undefined,
64
- ip: string | null | undefined,
65
- device: string,
66
- // Client
67
- client: string,
68
- build: string,
69
- status: string,
70
- // Error
71
- message: string,
72
- stacktrace: string
73
- }
74
-
75
- /*----------------------------------
76
- - CONFIG
77
- ----------------------------------*/
78
-
79
- const errorMailInterval = (1 * 60 * 60 * 1000); // 1 hour
80
-
81
- const LogPrefix = '[console][bugReporter]'
82
-
83
- export type TTransport = {
84
- name: string,
85
- send: TransportSender
86
- }
87
-
88
- export type Bug = ServerBug | ApplicationBug
89
-
90
- type TransportSender = (report: Bug, error?: Error) => Promise<any>
91
-
92
- /*----------------------------------
93
- - SERVICE
94
- ----------------------------------*/
95
- export default class BugReporter {
96
-
97
- private transporters: TTransport[] = [];
98
-
99
- public addTransporter( name: string, sender: TransportSender ) {
100
- console.log(LogPrefix, `Register trabsporter ${name} ...`);
101
- this.transporters.push({
102
- name,
103
- send: sender
104
- })
105
- }
106
-
107
- // Bug ID => Timestamp latest send
108
- private sentBugs: {[bugId: string]: number} = {};
109
-
110
- public constructor(
111
- private console: Console,
112
- public app = console.app,
113
- ) {
114
-
115
- }
116
-
117
- private shouldSendReport(
118
- side: string,
119
- user: string | undefined,
120
- action: string | undefined,
121
- message: string
122
- ) {
123
- const bugId = [side, user, action, message].filter(e => !!e).join('::');
124
- const lastSending = this.sentBugs[bugId];
125
- this.sentBugs[bugId] = Date.now();
126
- return lastSending === undefined || lastSending < Date.now() - errorMailInterval;
127
- }
128
-
129
- public async client() {
130
-
131
- }
132
-
133
- public async server( error: Error, request?: ServerRequest ) {
134
-
135
- // Print the error so it's accessible via logs
136
- if (error instanceof SqlError) {
137
- let printedQuery: string;
138
- try {
139
- printedQuery = this.console.printSql( error.query );
140
- } catch (error) {
141
- printedQuery = 'Failed to print query:' + (error || 'unknown error');
142
- }
143
- console.error(`Error caused by this query:`, printedQuery);
144
- }
145
- console.error(LogPrefix, `Sending bug report for the following error:`, error);
146
-
147
- // Prevent duplicates
148
- if (!this.shouldSendReport('server', request?.user?.name, undefined, error.message))
149
- return;
150
-
151
- // Get context
152
- const now = new Date();
153
- const hash = uuid();
154
- const { channelType, channelId } = this.console.getChannel();
155
-
156
- // On envoi l'email avant l'insertion dans bla bdd
157
- // Car cette denrière a plus de chances de provoquer une erreur
158
- const logsHtml = this.console.printHtml(
159
- this.console.logs.filter(e => e.channelId === channelId),
160
- true
161
- );
162
-
163
- const bugReport: ServerBug = {
164
-
165
- type: 'server',
166
-
167
- // Context
168
- hash: hash,
169
- date: now,
170
- channelType,
171
- channelId,
172
- // User
173
- user: request?.user?.email,
174
- ip: request?.ip,
175
- // Error
176
- error,
177
- stacktrace: error.stack || error.message,
178
- logs: logsHtml
179
- }
180
-
181
- await this.sendToTransporters(bugReport);
182
-
183
- // Update error message
184
- error.message = "We encountered an internal error, and our team has just been notified. Sorry for the inconvenience.";
185
- }
186
-
187
- public async application( report: AppBugInfos ) {
188
-
189
- // Prevent duplicates
190
- if (!this.shouldSendReport(report.side, report.user, report.action, report.message))
191
- return;
192
-
193
- // Get context
194
- const now = new Date();
195
- const hash = uuid();
196
-
197
- const bugReport: ApplicationBug = {
198
-
199
- type: 'application',
200
-
201
- // Context
202
- hash: hash,
203
- date: now,
204
- side: report.side,
205
- action: report.action,
206
- // User
207
- user: report.user,
208
- ip: report.ip,
209
- device: report.device,
210
- // Client
211
- client: report.client,
212
- build: report.build,
213
- status: report.status,
214
- // Error
215
- message: report.message,
216
- stacktrace: report.stacktrace,
217
- }
218
-
219
-
220
- // TODO:
221
- // Memorize
222
- //$.sql.insert('BugApp', );
223
-
224
- await this.sendToTransporters(bugReport);
225
- }
226
-
227
- private async sendToTransporters( bugReport: Bug, error?: Error ) {
228
-
229
- // Check if a transporter if configurated
230
- if (this.transporters.length === 0) {
231
- console.warn(LogPrefix, `No transporter configurated to report this error.`);
232
- return false;
233
- }
234
-
235
- // Don't send if we're in local (avoid to use credits: ex: email, sms)
236
- if (this.app.env.name === 'local'){
237
- console.warn(LogPrefix, `Error report sending aborted since we're local.`);
238
- return false;
239
- }
240
-
241
- // Send report to trabporters
242
- await Promise.all(
243
- this.transporters.map( transport => {
244
- console.log(LogPrefix, `Report via transporter ${transport.name} ...`);
245
- return transport.send(bugReport, error);
246
- })
247
- );
248
- }
249
-
250
- }