UNPKG

4.38 kBJavaScriptView Raw
1"use strict";
2var __importStar = (this && this.__importStar) || function (mod) {
3 if (mod && mod.__esModule) return mod;
4 var result = {};
5 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6 result["default"] = mod;
7 return result;
8};
9Object.defineProperty(exports, "__esModule", { value: true });
10const ConnectionsManager_1 = require("./ConnectionsManager");
11const is_npm_1 = require("is-npm");
12const Sentry = __importStar(require("@sentry/node"));
13const os_1 = require("os");
14class APIError extends Error {
15}
16const log = logger.withScope('errorHandler');
17const bannedErrors = [
18 'Token/username/password not found.',
19 'Missing Permissions',
20 'Missing Access',
21 'Incorrect login details were provided.',
22 'Invalid username or email address',
23 'Invalid username or password',
24 'User must verify their account',
25 'Service temporarily unavailable',
26 'ECONNRESET',
27 'MQTT connection failed' // Facebook MQTT connection fail
28];
29const isErrorBanned = (error) => bannedErrors.some(banned => error.toString().includes(banned)) || error instanceof ConnectionsManager_1.CMError;
30const errorDescriptions = {
31 'Invalid username or email address': `
32Couldn't login to Facebook.
33Check your username/email address, it may be incorrect.
34`,
35 'Invalid username or password': `
36Couldn't login to Facebook.
37Check your password or preferrably, use an app password:
38http://facebook.com/settings?tab=security&section=per_app_passwords&view
39`,
40 'Incorrect login details were provided.': `
41Couldn't login to Discord.
42Check your token.
43(it shouldn't be client ID nor anything else that doesn't have "token" in its name)
44`
45};
46const getErrorDescription = (error) => Object.keys(errorDescriptions).find(desc => error.toString().includes(desc));
47const dataPath = process.env.DATA_PATH !== 'undefined' ? process.env.DATA_PATH : undefined;
48exports.default = async (error) => {
49 if (!(error instanceof Error)) {
50 if (typeof error === 'string')
51 error = new Error(error);
52 else if (error.err && error.err instanceof Error)
53 error = error.err;
54 else if (error.error && error.error instanceof Error)
55 error = error.error;
56 else
57 error = new Error(error.toString());
58 }
59 const exitCode = ((error instanceof APIError && error.requestArgs) || error instanceof ConnectionsManager_1.CMError) ? 'close 1' : 'close 2';
60 log.error('error', error);
61 if (!isErrorBanned(error))
62 Sentry.captureException(error);
63 const desc = getErrorDescription(error);
64 if (desc)
65 log.error(desc[1]);
66 if (is_npm_1.isNpm) {
67 log.warn(`Logs from NPM are unnecessary and don't give much information.
68Miscord logs folder:
69${dataPath || require('.//config/FileConfig').getConfigDir()}/logs`);
70 }
71 if (global.discord && discord.channels && discord.channels.error) {
72 try {
73 let errorMessage = `${error.message}\n${error.stack}`;
74 if (errorMessage.length >= 1900) {
75 for (let i = 0; i < errorMessage.length; i += 1900) {
76 await discord.channels.error.send(errorMessage.substring(i, i + 1900), { code: true });
77 }
78 }
79 else {
80 await discord.channels.error.send(errorMessage, { code: true });
81 }
82 }
83 catch (err) {
84 log.fatal(err);
85 Sentry.captureException(err);
86 }
87 finally {
88 await cleanup(exitCode);
89 }
90 }
91 else {
92 await cleanup(exitCode);
93 }
94 async function cleanup(exitCode) {
95 await Sentry.getCurrentHub().getClient().close(2000);
96 if (global.discord)
97 await discord.client.destroy();
98 console.error(exitCode);
99 }
100};
101function initSentry() {
102 const pkg = require('../package.json');
103 Sentry.init({
104 dsn: 'https://a24e24e8c74f496db5fce2d611e085ee@sentry.miscord.net/2',
105 maxBreadcrumbs: 0,
106 release: `miscord@${pkg.version}`
107 });
108 Sentry.configureScope(scope => {
109 scope.setTag('is_packaged', Boolean(process.pkg).toString().toLowerCase());
110 scope.setTag('platform', os_1.platform());
111 scope.setTag('version', pkg.version);
112 scope.setTag('node_version', process.version);
113 });
114}
115exports.initSentry = initSentry;