{"size": 791, "ext": "js", "max_stars_repo_path": "app/middlewares/internal-error-handler.js", "max_stars_repo_name": "qqcloud/tcb", "max_stars_repo_stars_event_min_datetime": "2018-04-04T07:15:44.000Z", "max_stars_repo_stars_event_max_datetime": "2018-04-04T07:55:12.000Z", "max_issues_repo_path": "app/middlewares/internal-error-handler.js", "max_issues_repo_name": "qqcloud/tcb", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/middlewares/internal-error-handler.js", "max_forks_repo_name": "qqcloud/tcb", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 21.9722222222, "max_line_length": 55, "alphanum_fraction": 0.6245259166}
const utilLib = require('../libs/utillib');

/**
 * Error handler middleware
 * @module app/middlewares/internal-error-handler
 */

/**
 * Add error handler
 * @author jerishi
 * @DateTime 2018-05-30
 * @param    {Object}     err   app err
 * @param    {Function}   ctx   app ctx
 */
module.exports = async (err, ctx) => {
	const { request, response } = ctx;
	if (response.headersSent) {
		return;
	}

	const { $cgiType: cgiType, originalUrl } = request;
	Logger.error({
		info: '[internal error] =>' + originalUrl,
		detail: err.stack,
		reqSeqId: request.$reqSeqId,
	});
	if(cgiType === 'ajax' || cgiType === 'jsonp') {

		const error = ERROR.create('SERVER_RESPONSE_ERROR', {
			'msg': '${msg}[Server]',
		});
		utilLib.resJson(error, ctx);
	} else {
		await utilLib.render50X(ctx);
	}
};