Skip to content

Commit aa878bf

Browse files
committed
add define user error
1 parent cd6cd13 commit aa878bf

21 files changed

Lines changed: 486 additions & 326 deletions

app.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var sessions = require('./routes/sessions');
1616
var account = require('./routes/account');
1717
var users = require('./routes/users');
1818
var apps = require('./routes/apps');
19-
var AppError = require('./core/services/app-error');
19+
var AppError = require('./core/app-error');
2020
var app = express();
2121
app.use(helmet());
2222
app.disable('x-powered-by');
@@ -70,29 +70,42 @@ app.use('/apps', apps);
7070
// will print stacktrace
7171
if (app.get('env') === 'development') {
7272
app.use(function(req, res, next) {
73-
next(new AppError.NotFound());
74-
});
75-
app.use(function(err, req, res, next) {
76-
res.status(err.status || 500);
73+
var err = new AppError.NotFound();
74+
res.status(err.status || 404);
7775
res.render('error', {
7876
message: err.message,
7977
error: err
8078
});
8179
console.error(err.stack);
8280
});
81+
app.use(function(err, req, res, next) {
82+
if (err instanceof AppError.AppError) {
83+
res.send(err);
84+
} else {
85+
res.status(err.status || 500);
86+
res.render('error', {
87+
message: err.message,
88+
error: err
89+
});
90+
console.error(err.stack);
91+
}
92+
});
8393
} else {
8494
app.use(function(req, res, next) {
8595
res.status(404).send(new AppError.NotFound());
8696
});
8797
// production error handler
8898
// no stacktraces leaked to user
8999
app.use(function(err, req, res, next) {
90-
var status = err.status || 500;
91-
res.status(status);
92-
var error = new AppError.AppError(`服务器繁忙,请稍后再试!`);
93-
error.status = status;
94-
res.status(status).send(error);
95-
console.error(err.stack);
100+
if (err instanceof AppError.AppError) {
101+
res.send(err);
102+
} else {
103+
var status = err.status || 500;
104+
var error = new AppError.AppError(`服务器繁忙,请稍后再试!`);
105+
error.status = status;
106+
res.status(status).send(error);
107+
console.error(err.stack);
108+
}
96109
});
97110
}
98111

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var AppError = function (msg, constr) {
77
Error.captureStackTrace(this, constr || this)
88
this.message = msg || 'Error'
99
this.name = 'AppError'
10+
this.status = 200
1011
}
1112
util.inherits(AppError, Error)
1213

@@ -18,8 +19,17 @@ var NotFoundError = function(msg) {
1819
}
1920
util.inherits(NotFoundError, AppError)
2021

22+
var UnauthorizedError = function(msg) {
23+
NotFoundError.super_.call(this, msg, this.constructor)
24+
this.message = msg || `401 Unauthorized`;
25+
this.name = 'UnauthorizedError'
26+
this.status = 401
27+
}
28+
util.inherits(UnauthorizedError, AppError)
29+
2130
module.exports = {
2231
AppError: AppError,
23-
NotFound: NotFoundError
32+
NotFound: NotFoundError,
33+
Unauthorized: UnauthorizedError
2434
}
2535

core/middleware.js

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,37 @@ var Promise = require('bluebird');
44
var security = require('../core/utils/security');
55
var models = require('../models');
66
var moment = require('moment');
7+
var AppError = require('./app-error')
78

89
var middleware = module.exports
910

10-
const UNAUTHORIZED_TEXT = `401 Unauthorized`;
11-
1211
var checkAuthToken = function (authToken) {
1312
var objToken = security.parseToken(authToken);
1413
return models.Users.findOne({
1514
where: {identical: objToken.identical}
1615
})
17-
.then(function(users) {
16+
.then((users) => {
1817
if (_.isEmpty(users)) {
19-
throw new Error(UNAUTHORIZED_TEXT);
18+
throw new AppError.Unauthorized();
2019
}
2120
return models.UserTokens.findOne({
2221
where: {tokens: authToken, uid: users.id, expires_at: { gt: moment().format('YYYY-MM-DD HH:mm:ss') }}
2322
})
24-
.then(function(tokenInfo){
23+
.then((tokenInfo) => {
2524
if (_.isEmpty(tokenInfo)){
26-
throw new Error(UNAUTHORIZED_TEXT)
25+
throw new AppError.Unauthorized()
2726
}
2827
return users;
2928
})
30-
}).then(function (users) {
29+
}).then((users) => {
3130
return users;
3231
})
3332
}
3433

3534
var checkAccessToken = function (accessToken) {
36-
return new Promise(function (resolve, reject) {
35+
return new Promise((resolve, reject) => {
3736
if (_.isEmpty(accessToken)) {
38-
throw new Error(UNAUTHORIZED_TEXT);
37+
throw new AppError.Unauthorized();
3938
}
4039
var config = require('../core/config');
4140
var tokenSecret = _.get(config, 'jwt.tokenSecret');
@@ -47,20 +46,20 @@ var checkAccessToken = function (accessToken) {
4746
return models.Users.findOne({
4847
where: {id: uid}
4948
})
50-
.then(function(users) {
49+
.then((users) => {
5150
if (_.isEmpty(users)) {
52-
throw new Error(UNAUTHORIZED_TEXT);
51+
throw new AppError.Unauthorized();
5352
}
5453
if (!_.eq(hash, security.md5(users.get('ack_code')))){
55-
throw new Error(UNAUTHORIZED_TEXT);
54+
throw new AppError.Unauthorized();
5655
}
5756
resolve(users);
5857
})
59-
.catch(function (e) {
58+
.catch((e) => {
6059
reject(e);
6160
});
6261
} else {
63-
throw new Error(UNAUTHORIZED_TEXT);
62+
reject(new AppError.Unauthorized());
6463
}
6564
});
6665
}
@@ -83,25 +82,33 @@ middleware.checkToken = function(req, res, next) {
8382
}
8483
if (authType == 1) {
8584
checkAuthToken(authToken)
86-
.then(function(users) {
85+
.then((users) => {
8786
req.users = users;
8887
next();
8988
return users;
9089
})
91-
.catch(function (e) {
92-
res.status(401).send(e.message);
90+
.catch((e) => {
91+
if (e instanceof AppError.AppError) {
92+
res.status(e.status || 404).send(e.message);
93+
} else {
94+
next(e);
95+
}
9396
});
9497
} else if (authType == 2) {
9598
checkAccessToken(authToken)
96-
.then(function(users) {
99+
.then((users) => {
97100
req.users = users;
98101
next();
99102
return users;
100103
})
101-
.catch(function (e) {
102-
res.status(401).send(e.message);
104+
.catch((e) => {
105+
if (e instanceof AppError.AppError) {
106+
res.status(e.status || 404).send(e.message);
107+
} else {
108+
next(e);
109+
}
103110
});
104111
} else {
105-
res.status(401).send(UNAUTHORIZED_TEXT);
112+
res.send(new AppError.Unauthorized(`Auth type not supported.`));
106113
}
107114
};

0 commit comments

Comments
 (0)