entry-server.js
1.92 KB
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
import {createApp} from './app';
import {get} from 'lodash';
import {
SET_ENV,
} from 'store/yoho/types';
const sender = global.yoho.apmSender;
const logger = global.yoho.logger;
const catchError = (err, context) => {
logger.error(`[catchError], ${err}`);
setImmediate(() => {
try {
sender.addMessage({
measurement: 'error-report',
tags: {
app: 'yoho-app', // 应用名称
hostname: context.hostname,
type: 'server',
route: context.route, // 请求路由
uid: get(context, 'user.uid', 0),
udid: context.udid,
code: err.code || 500,
path: context.path,
url: encodeURIComponent(context.url),
ip: context.env.clientIp
},
fields: {
message: err.message,
stack: err.stack,
useragent: context.ua
}
});
} catch (error) {
logger.error(error);
}
});
};
export default context => {
return new Promise((resolve, reject) => {
const {app, router, store} = createApp(context);
const {url, env} = context;
store.commit(SET_ENV, env);
router.push(url);
router.onReady(() => {
const matched = router.getMatchedComponents();
if (matched.some(m => !m)) {
catchError(new Error('导航组件为空'), context);
router.push({name: 'error.500'});
return resolve(app);
}
if (!matched.length) {
return reject({code: 404, message: ''});
}
Promise.all(matched.map(({asyncData}) =>
asyncData && asyncData({store, router: router.currentRoute})))
.then(() => {
context.state = store.state;
return resolve(app);
}).catch(e => {
catchError(e, context);
return resolve(app);
});
});
router.onError(e => {
catchError(e, context);
router.push({name: 'error.500'});
return resolve(app);
});
});
};