Skip to content

Commit

Permalink
feat: add koa-error & koa-api-error-response middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
SimpleCodeCX committed Nov 11, 2019
1 parent 0868fa6 commit 69d6c67
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/app/middlewares/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import koaApiErrorResponse from './koa-api-error-response';
import koaError from './koa-error';
import koaCors from './koa-cors';
import koaNotFound from './koa-not-found';
import koaResponseTime from './koa-response-time';

export {
koaApiErrorResponse,
koaError,
koaCors,
koaNotFound,
koaResponseTime
Expand Down
21 changes: 21 additions & 0 deletions src/app/middlewares/koa-api-error-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Context } from 'koa';
import { apiServerInstance } from '../services';

/**
* 处理自定义接口错误,并响应错误信息
*/
const koaError = async function (ctx: Context, next) {
try {
await next();
} catch (_err) {
const err = _err || new Error('Null or undefined error');
// 判断是否为通用 api error
if (apiServerInstance.isApiErrorResponse(err.message)) {
ctx.body = JSON.parse(err.message);
return;
}
throw (err);
}
};

export default koaError;
28 changes: 28 additions & 0 deletions src/app/middlewares/koa-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Context } from 'koa';
import GLOBAL_CONFIG from '../config/system.config';
/**
* 异常处理
*/
const koaError = async function (ctx: Context, next) {
try {
await next();
} catch (_err) {
const err = _err || new Error('Null or undefined error');
ctx.app.emit('error', err, ctx);
ctx.set('Cache-Control', 'no-cache, max-age=0');
ctx.status = err.status || 500;
ctx.type = 'application/json';
const resp = err.response || {};
ctx.body = {
code: err.code,
error: resp.body || err.error,
message: err.message,
};
// 在开发环境下,显示错误堆栈信息
if (GLOBAL_CONFIG.isDev) {
ctx.body.stack = err.stack;
}
}
};

export default koaError;
9 changes: 8 additions & 1 deletion src/app/preset.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ import koaJson from 'koa-json';
import koaFavicon from 'koa-favicon';
import koaStaticServer from 'koa-static-server';
import {
koaCors, koaNotFound
koaCors,
koaError,
koaNotFound,
koaApiErrorResponse
} from './middlewares';
import { join as pathJoin } from 'path';

export class PresetMiddleware {
app: Koa;
constructor(app: Koa) {
this.app = app;
}

use(): Koa {
// 统一处理错误响应信息
this.app.use(koaError);
// 处理跨域
this.app.use(koaCors);
// 打印每一次接口请求响应时间
Expand All @@ -42,6 +48,7 @@ export class PresetMiddleware {
this.app.use(views(pathJoin(__dirname, '../assets/views'), {
extension: 'pug'
}));
this.app.use(koaApiErrorResponse);
this.app.use(index.routes());
this.app.use(api.routes());
/**
Expand Down

0 comments on commit 69d6c67

Please sign in to comment.