-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_on_error.js.html
93 lines (74 loc) · 4.61 KB
/
lib_on_error.js.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: lib/on_error.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: lib/on_error.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* @module
* @desc 统一的错误处理模块
*/
/**
* @name OnError
* @desc 根据每个出错请求生成新的处理函数
* @function
* @param {HTTPRequest} req - 导致出错的请求
* @param {HTTPResponse} res - 需要处理的响应
* @returns {Function} handler - 错误处理函数
*/
const OnError = (req, res) => (err) => {
let isMatched = false;
// error router
if (err instanceof Error) {
if (err.name === 'ValidationError') {
isMatched = true;
res.json({ code: 2, message: err.message, errors: err.errors });
} else if (err.name === 'MongoError') {
if (err.code === 11000) {
isMatched = true;
// duplicated
res.json({ code: 3, message: err.message, errors: err.errmsg });
}
} else if (err.name === 'CastError') {
isMatched = true;
res.json({ code: 2, message: err.message });
} else if (err.name === 'Error') {
if (err.message === 'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters') {
console.log('[Deprecated Error]');
res.json({ code: 2, message: 'Invalid ObjectId' });
}
}
}
// uncatched
if (isMatched === false) {
console.log('[unexpected error]', err.name, err.message);
res.json({ code: 1, message: 'BUG' });
}
};
module.exports = OnError;</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-config.html">config</a></li><li><a href="module-lib_on_error.html">lib/on_error</a></li><li><a href="module-lib_problem_create.html">lib/problem_create</a></li><li><a href="module-lib_problem_delete.html">lib/problem_delete</a></li><li><a href="module-lib_problem_detail.html">lib/problem_detail</a></li><li><a href="module-lib_problem_list_create.html">lib/problem_list_create</a></li><li><a href="module-lib_problem_list_detail.html">lib/problem_list_detail</a></li><li><a href="module-lib_problem_list_problem_create.html">lib/problem_list_problem_create</a></li><li><a href="module-lib_problem_list_retrieve.html">lib/problem_list_retrieve</a></li><li><a href="module-lib_problem_retrieve.html">lib/problem_retrieve</a></li><li><a href="module-lib_problem_update.html">lib/problem_update</a></li><li><a href="module-lib_submission_create.html">lib/submission_create</a></li><li><a href="module-lib_submission_detail.html">lib/submission_detail</a></li><li><a href="module-lib_submission_retrieve.html">lib/submission_retrieve</a></li><li><a href="module-lib_submission_sentence_update.html">lib/submission_sentence_update</a></li><li><a href="module-lib_user_create.html">lib/user_create</a></li><li><a href="module-lib_user_detail.html">lib/user_detail</a></li><li><a href="module-lib_user_login.html">lib/user_login</a></li><li><a href="module-lib_user_logout.html">lib/user_logout</a></li><li><a href="module-lib_user_profile_detail.html">lib/user_profile_detail</a></li><li><a href="module-lib_user_retrieve.html">lib/user_retrieve</a></li><li><a href="module-models_index.html">models/index</a></li><li><a href="module-routes_group.html">routes/group</a></li><li><a href="module-routes_index.html">routes/index</a></li><li><a href="module-routes_problem.html">routes/problem</a></li><li><a href="module-routes_problem_list.html">routes/problem_list</a></li><li><a href="module-routes_submission.html">routes/submission</a></li><li><a href="module-routes_user.html">routes/user</a></li><li><a href="module-server.html">server</a></li></ul><h3>Classes</h3><ul><li><a href="Problem.html">Problem</a></li><li><a href="ProblemList.html">ProblemList</a></li><li><a href="Submission.html">Submission</a></li><li><a href="User.html">User</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Thu Mar 23 2017 05:16:25 GMT+0000 (UTC)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>