pokemon-showdown/lib/crashlogger.ts
Guangcong Luo 87ef0cd5c4 TypeScript: export default
I'm currently pretty annoyed at TypeScript and TC39 for default exports
being a mess.

The goal here is to be able to type

    import Dex from './dex';

instead of any of

    import Dex = require('./dex');
    import {Dex} from './dex';
    import * as Dex from './dex';

This part involves a significant amount of struggle.

First, you can't automatically package up all your exports as your
default export. This leads to extremely un-DRY code, like in sim/index:

    export {
        Pokemon,
        Side,
        Battle,
        PRNG,
        Dex,
        TeamValidator,

        BattleStream,
    };

    export const Sim = {
        Pokemon,
        Side,
        Battle,
        PRNG,
        Dex,
        TeamValidator,

        BattleStream,
    };

(Both of these exports would be entirely unnecessary if you could just
automatically declare the file's exports as a default namespace.)

Second, a default export can't easily be a namespace. And TypeScript
doesn't allow types to exist in objects. Take the example from earlier:

    export const Sim = {
        Pokemon,
    };

If we were to try to use it:

    import Sim from './sim';
    let pokemon: Sim.Pokemon;

you'll get this error:

    Cannot find namespace 'Sim'. ts(2503)

You can, of course, fix this by making Sim a namespace:

    const PokemonT = Pokemon;
    type PokemonT = Pokemon;
    export namespace Sim {
        export const Pokemon = PokemonT;
        type Pokemon = PokemonT;
    }

But this quickly gets ridiculous the more classes you try to export.

You'd think there'd be a better way to do this. But I, at least,
haven't found one.
2019-05-14 20:33:33 +10:00

94 lines
2.4 KiB
TypeScript

/**
* Crash logger
* Pokemon Showdown - http://pokemonshowdown.com/
*
* Logs crashes, sends an e-mail notification if you've set up
* config.js to do that.
*
* @license MIT
*/
import * as fs from 'fs';
import * as path from 'path';
const CRASH_EMAIL_THROTTLE = 5 * 60 * 1000; // 5 minutes
const LOCKDOWN_PERIOD = 30 * 60 * 1000; // 30 minutes
const logPath = path.resolve(__dirname, '../logs/errors.txt');
let lastCrashLog = 0;
let transport: any;
/**
* Logs when a crash happens to console, then e-mails those who are configured
* to receive them.
*/
export function crashlogger(
error: Error | string, description: string, data: AnyObject | null = null
): string | null {
const datenow = Date.now();
let stack = typeof error === 'string' ? error : error.stack;
if (data) {
stack += `\n\nAdditional information:\n`;
for (const k in data) {
stack += ` ${k} = ${data[k]}\n`;
}
}
console.error(`\nCRASH: ${stack}\n`);
const out = fs.createWriteStream(logPath, {flags: 'a'});
out.on('open', () => {
out.write(`\n${stack}\n`);
out.end();
}).on('error', (err: Error) => {
console.error(`\nSUBCRASH: ${err.stack}\n`);
});
if (Config.crashguardemail && ((datenow - lastCrashLog) > CRASH_EMAIL_THROTTLE)) {
lastCrashLog = datenow;
if (!transport) {
try {
require.resolve('nodemailer');
} catch (e) {
throw new Error(
'nodemailer is not installed, but it is required if Config.crashguardemail is configured! ' +
'Run npm install --no-save nodemailer and restart the server.'
);
}
}
let text = `${description} crashed `;
if (transport) {
text += `again with this stack trace:\n${stack}`;
} else {
try {
// tslint:disable-next-line:no-implicit-dependencies
transport = require('nodemailer').createTransport(Config.crashguardemail.options);
} catch (e) {
throw new Error("Failed to start nodemailer; are you sure you've configured Config.crashguardemail correctly?");
}
text += `with this stack trace:\n${stack}`;
}
transport.sendMail({
from: Config.crashguardemail.from,
to: Config.crashguardemail.to,
subject: Config.crashguardemail.subject,
text,
}, (err: Error | null) => {
if (err) console.error(`Error sending email: ${err}`);
});
}
if (process.uptime() * 1000 < LOCKDOWN_PERIOD) {
// lock down the server
return 'lockdown';
}
return null;
}
export default crashlogger;