Skip to content

Commit

Permalink
feat: 添加中间件来捕获并打印异常信息
Browse files Browse the repository at this point in the history
  • Loading branch information
yinxin630 committed Jul 12, 2018
1 parent fcaee25 commit 8f2551a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/App/middleware/catchError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* 捕获 ProxyServer 异常中间件
*/
export function catchError() {
return async function catchErrorMiddleware(ctx, next) {
try {
await next();
} catch (err) {
console.error(err);
}
}
}
1 change: 1 addition & 0 deletions src/App/middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './monitor';
export * from './rule';
export * from './user';
export * from './ignore';
export * from './catchError';
2 changes: 2 additions & 0 deletions src/App/proxy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ip,
rule,
user,
catchError,
} from '../middleware';
import PluginManager from '../plugin-manager';
import {
Expand All @@ -33,6 +34,7 @@ export class Proxy {
public async init() {
this.server = await ProxyServer.create();
this.ignorer = new Ignorer();
this.server.use(catchError());
this.server.use(this.ignorer.middleware.bind(this.ignorer));
this.server.use(ip());
this.server.use(user(Container.get(ProfileService)));
Expand Down

0 comments on commit 8f2551a

Please sign in to comment.