MCPcopy Create free account
hub / github.com/coderwhy/coderhub / errorHandler

Function errorHandler

src/app/error-handle.js:3–38  ·  view source on GitHub ↗
(error, ctx)

Source from the content-addressed store, hash-verified

1const errorTypes = require('../constants/error-types');
2
3const errorHandler = (error, ctx) => {
4 let status, message;
5
6 switch (error.message) {
7 case errorTypes.NAME_OR_PASSWORD_IS_REQUIRED:
8 status = 400; // Bad Request
9 message = "用户名或者密码不能为空~";
10 break;
11 case errorTypes.USER_ALREADY_EXISTS:
12 status = 409; // conflict
13 message = "用户名已经存在~";
14 break;
15 case errorTypes.USER_DOES_NOT_EXISTS:
16 status = 400; // 参数错误
17 message = "用户名不存在~";
18 break;
19 case errorTypes.PASSWORD_IS_INCORRENT:
20 status = 400; // 参数错误
21 message = "密码是错误的~";
22 break;
23 case errorTypes.UNAUTHORIZATION:
24 status = 401; // 参数错误
25 message = "无效的token~";
26 break;
27 case errorTypes.UNPERMISSION:
28 status = 401; // 参数错误
29 message = "您不具备操作的权限~";
30 break;
31 default:
32 status = 404;
33 message = "NOT FOUND";
34 }
35
36 ctx.status = status;
37 ctx.body = message;
38}
39
40module.exports = errorHandler;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected