jj.js 0.8.5 → 0.8.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/db.js CHANGED
@@ -66,10 +66,10 @@ class Db extends Context
66
66
  if(pool.has(this._config)) {
67
67
  pool.get(this._config).end(err => {
68
68
  if(err) {
69
- const message = '连接池销毁失败:' + err['message'];
69
+ const message = '连接池销毁失败:' + err.message;
70
70
  this.$logger.sql(message);
71
71
  this.$logger.error(message);
72
- throw new Error(message);
72
+ throw new Error('DbError: ' + message);
73
73
  } else {
74
74
  pool.delete(this._config);
75
75
  this.$logger.sql(`连接池销毁成功:{poolTotal: ${pool.size}}`);
@@ -95,10 +95,10 @@ class Db extends Context
95
95
  return new Promise((resolve, reject) => {
96
96
  p.getConnection((err, connection) => {
97
97
  if(err) {
98
- const message = '获取数据库连接失败:' + err['message'];
98
+ const message = '获取数据库连接失败:' + err.message;
99
99
  this.$logger.sql(message);
100
100
  this.$logger.error(message);
101
- reject(new Error(message));
101
+ reject(new Error('DbError: ' + message));
102
102
  } else {
103
103
  this.$logger.sql(`获取数据库连接:{limit: ${p.config.connectionLimit}, all: ${p._allConnections.length}, acquiring: ${p._acquiringConnections.length}, free: ${p._freeConnections.length}, queue: ${p._connectionQueue.length}}`);
104
104
  resolve(connection);
@@ -134,10 +134,10 @@ class Db extends Context
134
134
  return new Promise((resolve, reject) => {
135
135
  conn.beginTransaction(async (err) => {
136
136
  if(err) {
137
- const message = '开启事务失败:' + err['message'];
137
+ const message = '开启事务失败:' + err.message;
138
138
  this.$logger.sql(message);
139
139
  this.$logger.error(message);
140
- reject(new Error(message));
140
+ reject(new Error('DbError: ' + message));
141
141
  } else {
142
142
  const message = '开启事务成功!';
143
143
  this.$logger.sql(message);
@@ -195,11 +195,11 @@ class Db extends Context
195
195
  return new Promise((resolve, reject) => {
196
196
  conn.commit((err) => {
197
197
  if(err) {
198
- const message = '事务提交失败:' + err['message'];
198
+ const message = '事务提交失败:' + err.message;
199
199
  this.$logger.sql(message);
200
200
  this.$logger.error(message);
201
201
  this.rollback();
202
- reject(new Error(message));
202
+ reject(new Error('DbError: ' + message));
203
203
  } else {
204
204
  trans.delete(this.ctx);
205
205
  const message = '事务提交成功!';
@@ -220,10 +220,10 @@ class Db extends Context
220
220
  return new Promise((resolve, reject) => {
221
221
  conn.query(sql, params, (err, data) => {
222
222
  if(err) {
223
- const message = '数据操作失败,SQL:' + this.format(sql, params) + '(' + err['message'] + ')';
223
+ const message = '数据操作失败,' + err.message + "\r\nSQL:" + this.format(sql, params);
224
224
  this.$logger.sql(message);
225
225
  this.$logger.error(message);
226
- reject(new Error(message));
226
+ reject(new Error('DbError: ' + message));
227
227
  } else {
228
228
  this.$logger.sql('数据操作成功,SQL:' + this.format(sql, params));
229
229
  resolve(data);
@@ -506,7 +506,7 @@ class Db extends Context
506
506
  if(!where[0]) {
507
507
  const message = 'update方法必须传入where参数';
508
508
  this.$logger.sql(message);
509
- throw new Error(message);
509
+ throw new Error('DbError: ' + message);
510
510
  }
511
511
  params = params.concat(where[1]);
512
512
 
@@ -539,7 +539,7 @@ class Db extends Context
539
539
  if(!where[0]) {
540
540
  const message = 'delete方法必须传入where参数';
541
541
  this.$logger.sql(message);
542
- throw new Error(message);
542
+ throw new Error('DbError: ' + message);
543
543
  }
544
544
  params = params.concat(where[1]);
545
545
 
package/lib/router.js CHANGED
@@ -9,7 +9,7 @@ if (cfg_routes) {
9
9
  for(let item of cfg_routes){
10
10
  item.method || (item.method = 'all');
11
11
  if(!~methods.indexOf(item.method)) {
12
- throw new Error(`未知路由方法:${item.method}`);
12
+ throw new Error(`RouteError: 未知路由方法:${item.method}`);
13
13
  }
14
14
  if(typeof item.path === 'function'){
15
15
  router[item.method](item.url, item.path);
package/lib/run.js CHANGED
@@ -30,12 +30,12 @@ async function run(ctx, next, control_type=cfg_app.controller_folder) {
30
30
 
31
31
  // 应用、控制器、方法名字验证
32
32
  if (~ctx.APP.indexOf('.') || ~ctx.CONTROLLER.indexOf('.') || ~ctx.ACTION.indexOf('.')) {
33
- throw new Error(`应用、控制器或方法名字不合法!`);
33
+ throw new Error(`RunError: 应用、控制器或方法名字不合法!`);
34
34
  }
35
35
 
36
36
  if (ctx.APP[0] == '_' || ctx.APP[0] == '$' || !ctx._[ctx.APP]) {
37
37
  if(cfg_app.app_debug) {
38
- throw new Error(`应用:${ctx.APP}不存在!`);
38
+ throw new Error(`RunError: 应用:${ctx.APP}不存在!`);
39
39
  } else {
40
40
  return false;
41
41
  }
@@ -43,7 +43,7 @@ async function run(ctx, next, control_type=cfg_app.controller_folder) {
43
43
 
44
44
  if (!ctx._[ctx.APP][control_type]) {
45
45
  if(cfg_app.app_debug) {
46
- throw new Error(`目录:${ctx.APP}/${control_type}不存在!`);
46
+ throw new Error(`RunError: 目录:${ctx.APP}/${control_type}不存在!`);
47
47
  } else {
48
48
  return false;
49
49
  }
@@ -53,7 +53,7 @@ async function run(ctx, next, control_type=cfg_app.controller_folder) {
53
53
  if(control_type == cfg_view.view_folder) {
54
54
  const content = await readFile(path.join(ctx._[ctx.APP][control_type].__node.path, ctx.CONTROLLER + cfg_view.view_depr + ctx.ACTION + cfg_view.view_ext));
55
55
  if(!content) {
56
- throw new Error(`模板文件:${ctx.APP}/${control_type}/${ctx.CONTROLLER + cfg_view.view_depr + ctx.ACTION + cfg_view.view_ext}不存在!`);
56
+ throw new Error(`RunError: 模板文件:${ctx.APP}/${control_type}/${ctx.CONTROLLER + cfg_view.view_depr + ctx.ACTION + cfg_view.view_ext}不存在!`);
57
57
  }
58
58
  ctx.body = content;
59
59
  return;
@@ -64,7 +64,7 @@ async function run(ctx, next, control_type=cfg_app.controller_folder) {
64
64
 
65
65
  if (ctx.CONTROLLER[0] == '_' || ctx.CONTROLLER[0] == '$' || typeof Controller != 'function') {
66
66
  if(cfg_app.app_debug) {
67
- throw new Error(`class文件:${ctx.APP}/${control_type}/${ctx.CONTROLLER}不存在!`);
67
+ throw new Error(`RunError: class文件:${ctx.APP}/${control_type}/${ctx.CONTROLLER}不存在!`);
68
68
  } else {
69
69
  return false;
70
70
  }
@@ -77,7 +77,7 @@ async function run(ctx, next, control_type=cfg_app.controller_folder) {
77
77
 
78
78
  if (ctx.ACTION[0] == '_' || ctx.ACTION[0] == '$' || typeof $controller[action] != 'function') {
79
79
  if(cfg_app.app_debug) {
80
- throw new Error(`class方法:${ctx.APP}/${control_type}/${ctx.CONTROLLER}/${humpAction}不存在!`);
80
+ throw new Error(`RunError: class方法:${ctx.APP}/${control_type}/${ctx.CONTROLLER}/${humpAction}不存在!`);
81
81
  } else {
82
82
  return false;
83
83
  }
@@ -97,18 +97,18 @@ async function run(ctx, next, control_type=cfg_app.controller_folder) {
97
97
  return stack.concat(async (ctx, next) => {
98
98
  const [METHOD = action, MIDDLEWARE = ctx.CONTROLLER, APP = ctx.APP] = middle.middleware.split('/').reverse();
99
99
  if(!ctx._[APP]) {
100
- throw new Error(`中间件应用:${APP}不存在!`);
100
+ throw new Error(`RunError: 中间件应用:${APP}不存在!`);
101
101
  }
102
102
  if(!ctx._[APP]['middleware']) {
103
- throw new Error(`中间件目录:${APP}/middleware不存在!`);
103
+ throw new Error(`RunError: 中间件目录:${APP}/middleware不存在!`);
104
104
  }
105
105
  const Middleware = ctx._[APP]['middleware'][MIDDLEWARE];
106
106
  if(!Middleware || typeof Middleware != 'function') {
107
- throw new Error(`中间件文件:${APP}/middleware/${MIDDLEWARE}不存在!`);
107
+ throw new Error(`RunError: 中间件文件:${APP}/middleware/${MIDDLEWARE}不存在!`);
108
108
  }
109
109
  const $middleware = new Middleware(ctx, next);
110
110
  if(!$middleware[METHOD] || typeof $middleware[METHOD] != 'function') {
111
- throw new Error(`中间件方法:${APP}/middleware/${MIDDLEWARE}/${METHOD}不存在!`);
111
+ throw new Error(`RunError: 中间件方法:${APP}/middleware/${MIDDLEWARE}/${METHOD}不存在!`);
112
112
  }
113
113
 
114
114
  // 执行中间件方法
@@ -1,29 +1,33 @@
1
+ const fs = require('./fs.js');
2
+
1
3
  function parseError(err) {
2
- let stack = err.stack;
3
- stack = stack.replace(/(\r\n)|(\n)/g, '#split#');
4
- stack = stack.split('#split#');
5
- let msg = err.name;
4
+ let msg = err.message;
5
+ let stack = err.stack.replace(/(\r\n)/g, "\n").split("\n");
6
+
6
7
  if(err.name == 'TemplateError') {
7
- msg = stack.pop();
8
+ msg = 'TemplateError: ' + stack.pop();
9
+ stack = [stack.shift().replace('TemplateError:', "at")];
8
10
  } else {
9
- msg = stack.shift();
11
+ stack = stack.filter(text => {return /^ at /.test(text);});
10
12
  }
11
- let file_info = stack[0].split(' ').slice(-1)[0].replace(/(\()|(\))/g, '').split(':');
13
+
14
+ const file_info = stack[0].split(' ').slice(-1)[0].replace(/(\()|(\))/g, '').split(':');
12
15
  const column = parseInt(file_info.pop());
13
16
  const row = parseInt(file_info.pop());
14
17
  const file_path = file_info.join(':');
15
18
  let begin = row - 10;
16
19
  let end = row + 10;
20
+ let nth = 0;
17
21
  let code = '';
18
22
  if(begin < 0) {
19
23
  end -= begin;
20
24
  begin = 0;
21
25
  }
22
- if(file_path == 'anonymous') {
26
+ if(file_path == 'anonymous' || !file_path || !fs.isFileSync(file_path)) {
23
27
  code = stack;
24
28
  } else {
25
29
  code = require('fs').readFileSync(file_path);
26
- code = code.toString().split("\n");
30
+ code = code.toString().replace(/(\r\n)/g, "\n").split("\n");
27
31
  if(begin < 0) {
28
32
  end -= begin;
29
33
  begin = 0;
@@ -36,9 +40,6 @@ function parseError(err) {
36
40
  begin = 0;
37
41
  }
38
42
  code = code.slice(begin, end);
39
- if(err.name == 'TemplateError') {
40
- stack = [stack[0]];
41
- }
42
43
  nth = row - begin;
43
44
  }
44
45
 
package/lib/view.js CHANGED
@@ -71,7 +71,7 @@ class View extends Context
71
71
  }
72
72
  if(!is_file) {
73
73
  file_name = this.parsePath(template, this._folder, this._depr);
74
- throw new Error(`文件不存在(${file_name})`);
74
+ throw new Error(`ViewError: 文件不存在(${file_name})`);
75
75
  }
76
76
  return file_name;
77
77
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jj.js",
3
- "version": "0.8.5",
3
+ "version": "0.8.6",
4
4
  "description": "A simple and lightweight MVC framework built on nodejs+koa2",
5
5
  "main": "jj.js",
6
6
  "scripts": {