|
|
import {createApp} from './app';
|
|
|
import {get} from 'lodash';
|
|
|
import createReport from 'report-error';
|
|
|
|
|
|
import {
|
|
|
SET_ENV,
|
|
|
} from 'store/yoho/types';
|
|
|
const sender = global.yoho.apmSender;
|
|
|
const logger = global.yoho.logger;
|
|
|
|
|
|
const catchError = (err, message, context) => {
|
|
|
logger.error(message, err);
|
|
|
setImmediate(() => {
|
|
|
try {
|
|
|
sender.addMessage({
|
|
|
measurement: 'error-report',
|
|
|
tags: {
|
|
|
app: 'yoho-app-web', // 应用名称
|
|
|
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 => {
|
|
|
const reportError = createReport(context);
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
const {app, router, store} = createApp(context);
|
|
|
const {url, env} = context;
|
|
|
const {url} = context;
|
|
|
|
|
|
store.commit(SET_ENV, env);
|
|
|
store.commit(SET_ENV, {context});
|
|
|
router.push(url);
|
|
|
router.onReady(() => {
|
|
|
const matched = router.getMatchedComponents();
|
|
|
|
|
|
if (matched.some(m => !m)) {
|
|
|
catchError(new Error('导航组件为空'), '[router.onReady]', context);
|
|
|
reportError(new Error('导航组件为空'));
|
|
|
router.push({name: 'error.500'});
|
|
|
return resolve(app);
|
|
|
}
|
|
|
if (!matched.length) {
|
|
|
return reject({code: 404, message: ''});
|
|
|
}
|
|
|
const asyncDataPromises = matched.map(({asyncData}) => {
|
|
|
try {
|
|
|
return asyncData && asyncData({store, router: router.currentRoute});
|
|
|
} catch (error) {
|
|
|
return Promise.reject(error);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
Promise.all(matched.map(({asyncData}) =>
|
|
|
asyncData && asyncData({store, router: router.currentRoute})))
|
|
|
Promise.all(asyncDataPromises)
|
|
|
.then(() => {
|
|
|
context.state = store.state;
|
|
|
return resolve(app);
|
|
|
}).catch(e => {
|
|
|
catchError(e, '[asyncData]', context);
|
|
|
reportError(e);
|
|
|
return resolve(app);
|
|
|
});
|
|
|
});
|
|
|
|
|
|
router.onError(e => {
|
|
|
catchError(e, 'router.onError', context);
|
|
|
reportError(e);
|
|
|
router.push({name: 'error.500'});
|
|
|
return resolve(app);
|
|
|
});
|
...
|
...
|
|