Skip to content

Commit

Permalink
feat: 增加错误文案方便调试
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Oct 10, 2019
1 parent 86c1489 commit ff28793
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 8 deletions.
7 changes: 6 additions & 1 deletion lib/command/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import path from 'path';
import {
loadConfig
} from '../utils';
import getProvider from '../utils/getProvider';
import getProvider from '../utils/get-provider';
import { errorHandler } from '../utils/error-helper';

class CheckCommand extends Command {
private options: object;
Expand Down Expand Up @@ -44,6 +45,10 @@ class CheckCommand extends Command {
public get description(): string {
return 'Check configurations from provider';
}

public errorHandler(err): void {
errorHandler.call(this, err);
}
}

export = CheckCommand;
5 changes: 5 additions & 0 deletions lib/command/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';

import { loadConfig } from '../utils';
import generate from '../generate';
import { errorHandler } from '../utils/error-helper';

class GenerateCommand extends Command {
private options: object;
Expand Down Expand Up @@ -35,6 +36,10 @@ class GenerateCommand extends Command {
public get description(): string {
return 'Generate configurations';
}

public errorHandler(err): void {
errorHandler.call(this, err);
}
}

export = GenerateCommand;
7 changes: 6 additions & 1 deletion lib/command/speed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import Provider from '../class/Provider';

import { NodeTypeEnum, PossibleNodeConfigType, ShadowsocksNodeConfig } from '../types';
import { getClashNodes, loadConfig, toYaml } from '../utils';
import getProvider from '../utils/getProvider';
import getProvider from '../utils/get-provider';
import { errorHandler } from '../utils/error-helper';

const { combine, timestamp, printf } = format;
const speedDebug = debug('speed');
Expand Down Expand Up @@ -47,6 +48,10 @@ class SpeedCommand extends Command {
return 'Speed test Shadowsocks server.';
}

public errorHandler(err): void {
errorHandler.call(this, err);
}

public async run(ctx): Promise<void> {
assert(ctx.argv._[0], 'No provider specified.');

Expand Down
9 changes: 5 additions & 4 deletions lib/command/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ora, { Ora } from 'ora';
import path from 'path';

import { loadConfig } from '../utils';
import { errorHandler } from '../utils/error-helper';

class GenerateCommand extends Command {
private readonly spinner: Ora;
Expand Down Expand Up @@ -44,9 +45,9 @@ class GenerateCommand extends Command {
accessKeySecret: ctx.env.OSS_ACCESS_KEY_SECRET || config.upload.accessKeySecret,
};

assert(ossConfig.bucket);
assert(ossConfig.accessKeyId);
assert(ossConfig.accessKeySecret);
assert(ossConfig.bucket, '未配置 OSS Bucket');
assert(ossConfig.accessKeyId, '未配置 OSS accessKeyId');
assert(ossConfig.accessKeySecret, '未配置 OSS accessKeySecret');

const client = new OSS({
secure: true,
Expand Down Expand Up @@ -111,7 +112,7 @@ class GenerateCommand extends Command {
public errorHandler(err): void {
this.spinner.fail();

super.errorHandler(err);
errorHandler.call(this, err);
}
}

Expand Down
3 changes: 1 addition & 2 deletions lib/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
usFilter,
youtubePremiumFilter as defaultYoutubePremiumFilter,
} from './utils/filter';
import getProvider from './utils/getProvider';
import getProvider from './utils/get-provider';
import { prependFlag } from './utils/flag';

const spinner = ora();
Expand Down Expand Up @@ -197,7 +197,6 @@ export async function generate(
} : {}),
});
} catch (err) {
console.error('模板渲染错误');
throw err;
}

Expand Down
5 changes: 5 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import GenerateCommand from './command/generate';
import SpeedCommand from './command/speed';
import UploadCommand from './command/upload';
import * as filter from './utils/filter';
import { errorHandler } from './utils/error-helper';

const envPath = path.resolve(process.cwd(), './.env');

Expand All @@ -26,6 +27,10 @@ export class SurgioCommand extends Command {
this.load(path.join(__dirname, './command'));
this.yargs.alias('v', 'version');
}

public errorHandler(err): void {
errorHandler.call(this, err);
}
}

export {
Expand Down
12 changes: 12 additions & 0 deletions lib/utils/error-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Command from 'common-bin';
import chalk from 'chalk';

export const errorHandler = function(this: Command, err: Error): void {
console.error(chalk.red(`⚠️ 发生错误`));
console.error(chalk.red(`⚠️ ${err.name}: ${err.message}`));
console.error('⚠️ 命令参数: %s', process.argv.slice(3));
console.error('⚠️ 加入交流群汇报问题 %s', chalk.cyan('https://t.me/surgiotg'));
console.error();
console.error(chalk.red(err.stack));
process.exit(1);
}
File renamed without changes.

0 comments on commit ff28793

Please sign in to comment.