@@ -8,61 +8,55 @@ function _inherits(subClass, superClass) { if (typeof superClass !== "function"
88
99/**
1010 * 模板错误处理类
11+ * @param {Object } options
1112 */
1213var TemplateError = function ( _Error ) {
1314 _inherits ( TemplateError , _Error ) ;
1415
15- function TemplateError ( error ) {
16+ function TemplateError ( options ) {
1617 _classCallCheck ( this , TemplateError ) ;
1718
18- var _this = _possibleConstructorReturn ( this , _Error . call ( this , error ) ) ;
19-
20- var message = error . message ;
21-
22- if ( TemplateError . debugTypes [ error . name ] ) {
23-
24- if ( error . source ) {
25- message = TemplateError . debug ( error ) ;
26- }
27-
28- _this . path = error . path ;
29- }
19+ var _this = _possibleConstructorReturn ( this , _Error . call ( this , options . message ) ) ;
3020
3121 _this . name = 'TemplateError' ;
32- _this . message = message ;
22+ _this . message = formatMessage ( options ) ;
23+ if ( Error . captureStackTrace ) {
24+ Error . captureStackTrace ( _this , _this . constructor ) ;
25+ }
3326 return _this ;
3427 }
3528
36- TemplateError . debug = function debug ( error ) {
37- var source = error . source ,
38- path = error . path ,
39- line = error . line ,
40- column = error . column ;
29+ return TemplateError ;
30+ } ( Error ) ;
4131
32+ ;
4233
43- var lines = source . split ( / \n / ) ;
44- var start = Math . max ( line - 3 , 0 ) ;
45- var end = Math . min ( lines . length , line + 3 ) ;
34+ function formatMessage ( _ref ) {
35+ var name = _ref . name ,
36+ source = _ref . source ,
37+ path = _ref . path ,
38+ line = _ref . line ,
39+ column = _ref . column ,
40+ message = _ref . message ;
4641
47- // Error context
48- var context = lines . slice ( start , end ) . map ( function ( code , index ) {
49- var number = index + start + 1 ;
50- var left = number === line ? ' >> ' : ' ' ;
51- return '' + left + number + '| ' + code ;
52- } ) . join ( '\n' ) ;
5342
54- // Alter exception message
55- return ( path || 'anonymous' ) + ':' + line + ':' + column + '\n' + ( context + '\n\n' ) + ( '' + error . message ) ;
56- } ;
43+ if ( ! source ) {
44+ return message ;
45+ }
5746
58- return TemplateError ;
59- } ( Error ) ;
47+ var lines = source . split ( / \n / ) ;
48+ var start = Math . max ( line - 3 , 0 ) ;
49+ var end = Math . min ( lines . length , line + 3 ) ;
6050
61- ;
51+ // Error context
52+ var context = lines . slice ( start , end ) . map ( function ( code , index ) {
53+ var number = index + start + 1 ;
54+ var left = number === line ? ' >> ' : ' ' ;
55+ return '' + left + number + '| ' + code ;
56+ } ) . join ( '\n' ) ;
6257
63- TemplateError . debugTypes = {
64- 'RuntimeError' : true ,
65- 'CompileError' : true
66- } ;
58+ // Alter exception message
59+ return ( path || 'anonymous' ) + ':' + line + ':' + column + '\n' + ( context + '\n\n' ) + ( name + ': ' + message ) ;
60+ }
6761
6862module . exports = TemplateError ;
0 commit comments