提交 baef92ee 编写于 作者: D DCloud_LXH

chore: uni-stacktracey

上级 c140fab2
const {
stacktracey,
uniStracktraceyPreset,
utsStracktraceyPreset,
} = require('../dist/uni-stacktracey.cjs.js')
const utsErrorMsg = `Appid: __UNI__E070870
e: DCloud-UTSPlugin/android/src/io/dcloud/uniplugin/TestComponent.kt: (68, 9): Unresolved reference: hello
e: DCloud-UTSPlugin/android/src/io/dcloud/uniplugin/TestModule.kt: (30, 9): Unresolved reference: hello
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':nativeplugins:DCloud-UTSPlugin:compileReleaseKotlin'.
> Compilation error. See log for more details
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 3s`
const uniErrorMsg = `ReferenceError: Sentry is not defined
at Proxy.throwError(/vue3/assets/pages-index-index.4077a069.js:1:295)
at e(/vue3/assets/index.2be9343a.js:1:52317)
at Ue(/vue3/assets/index.2be9343a.js:1:16271)
at He(/vue3/assets/index.2be9343a.js:1:16349)
at HTMLElement.n(/vue3/assets/index.2be9343a.js:1:51834)
at HTMLElement.o(/vue3/assets/index.2be9343a.js:21:60087)`
describe('uni-stacktracey', () => {
test('uniStracktraceyPreset', () => {
stacktracey(uniErrorMsg, {
preset: uniStracktraceyPreset({
base: 'https://7463-tcb-uzyfn59tqxjxtnbab2e2c-5ba40b-1303909289.tcb.qcloud.la',
platform: 'h5',
version: '1.0.0',
}),
}).then((res: string) => {
expect(res).toEqual(`ReferenceError: Sentry is not defined
at Proxy.throwError(/vue3/assets/pages-index-index.4077a069.js:1
at e(/vue3/assets/index.2be9343a.js:1
at Ue(/vue3/assets/index.2be9343a.js:1
at He(/vue3/assets/index.2be9343a.js:1
at HTMLElement.n(/vue3/assets/index.2be9343a.js:1
at HTMLElement.o(/vue3/assets/index.2be9343a.js:21 `)
})
})
test('uniStracktraceyPreset', () => {
stacktracey(utsErrorMsg, {
preset: utsStracktraceyPreset({
base: '/usr/fxy/poroject/test/.sourcemap/src/',
sourceRoot:
'/wgtRoot/__UNI__E070870/nativeplugins/DCloud-UTSPlugin/android/src/',
}),
}).then((res: string) => {
expect(res).toEqual(`Appid: __UNI__E070870
e: DCloud-UTSPlugin/android/src/io/dcloud/uniplugin/TestComponent.kt: (68, 9): Unresolved reference: hello
e: DCloud-UTSPlugin/android/src/io/dcloud/uniplugin/TestModule.kt: (30, 9): Unresolved reference: hello
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':nativeplugins:DCloud-UTSPlugin:compileReleaseKotlin'.
> Compilation error. See log for more details
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 3s`)
})
})
})
...@@ -73,16 +73,14 @@ function getSourceMapContent(sourcemapUrl) { ...@@ -73,16 +73,14 @@ function getSourceMapContent(sourcemapUrl) {
(sourcemapCatch[sourcemapUrl] = new Promise((resolve, reject) => { (sourcemapCatch[sourcemapUrl] = new Promise((resolve, reject) => {
try { try {
if (/^[a-z]+:/i.test(sourcemapUrl)) { if (/^[a-z]+:/i.test(sourcemapUrl)) {
/* uni uni.request({
.request(sourcemapUrl) url: sourcemapUrl,
.then((res) => { success: (res) => {
console.log('sourcemapUrl :>> ', sourcemapUrl) console.log('sourcemapUrl :>> ', sourcemapUrl);
sourcemapCatch[sourcemapUrl] = res.data sourcemapCatch[sourcemapUrl] = res.data;
resolve(sourcemapCatch[sourcemapUrl]) resolve(sourcemapCatch[sourcemapUrl]);
}) },
.catch((_) => { });
resolve('')
}) */
} }
else { else {
sourcemapCatch[sourcemapUrl] = fs__default["default"].readFileSync(sourcemapUrl, 'utf-8'); sourcemapCatch[sourcemapUrl] = fs__default["default"].readFileSync(sourcemapUrl, 'utf-8');
...@@ -132,18 +130,18 @@ function uniStracktraceyPreset(opts) { ...@@ -132,18 +130,18 @@ function uniStracktraceyPreset(opts) {
}, },
asTableStacktrace({ maxColumnWidths, stacktrace } = { stacktrace: '' }) { asTableStacktrace({ maxColumnWidths, stacktrace } = { stacktrace: '' }) {
const errorName = stacktrace.split('\n')[0]; const errorName = stacktrace.split('\n')[0];
return errorName.indexOf('at') === -1 return ((errorName.indexOf('at') === -1 ? `${errorName}\n` : '') +
? `${errorName}\n` (stack.asTable ? stack.asTable({ maxColumnWidths }) : ''));
: '' + (stack.asTable ? stack.asTable({ maxColumnWidths }) : '');
}, },
}; };
} }
function utsStracktraceyPreset(opts) { function utsStracktraceyPreset(opts) {
let stack; let stack;
let errStack = [];
return { return {
parseSourceMapUrl(file, fileName) { parseSourceMapUrl(file, fileName) {
// 根据 base,filename 组合 sourceMapUrl // 根据 base,filename 组合 sourceMapUrl
return `${opts.base}/${fileName}.map`; return `${opts.base}${file.replace(opts.sourceRoot, '')}.map`;
}, },
getSourceMapContent(file, fileName) { getSourceMapContent(file, fileName) {
// 根据 base,filename 组合 sourceMapUrl // 根据 base,filename 组合 sourceMapUrl
...@@ -152,27 +150,29 @@ function utsStracktraceyPreset(opts) { ...@@ -152,27 +150,29 @@ function utsStracktraceyPreset(opts) {
parseStacktrace(str) { parseStacktrace(str) {
const lines = (str || '').split('\n'); const lines = (str || '').split('\n');
const entries = lines const entries = lines
.map((line) => { .map((line, index) => {
line = line.trim(); line = line.trim();
let callee, fileLineColumn = [], planA, planB; let callee, fileLineColumn = [], planA, planB;
if ((planA = line.match(/e: \[(.+)\](.+): (.+)/))) { if ((planA = line.match(/e: (.+\.kt)(.+\))*:\s*(.+)*/))) {
callee = planA[1]; errStack.push('%StacktraceyItem%');
fileLineColumn = (planA[2].match(/(.+):.*\((\d+).+?(\d+)\)/) || []).slice(1); callee = 'e: ';
fileLineColumn = (planA[2].match(/.*:.*\((\d+).+?(\d+)\)/) || []).slice(1);
} }
else { else {
errStack.push(line);
return undefined; return undefined;
} }
const fileName = fileLineColumn[0] const fileName = planA[1]
? (planB = fileLineColumn[0].match(/(\/.*)*\/(.+)/) || [])[2] || '' ? (planB = planA[1].match(/(.*)*\/(.+)/) || [])[2] || ''
: ''; : '';
return { return {
beforeParse: line, beforeParse: line,
callee: callee || '', callee: callee || '',
index: false, index: false,
native: false, native: false,
file: nixSlashes(fileLineColumn[0] || ''), file: nixSlashes(planA[1] || ''),
line: parseInt(fileLineColumn[1] || '', 10) || undefined, line: parseInt(fileLineColumn[0] || '', 10) || undefined,
column: parseInt(fileLineColumn[2] || '', 10) || undefined, column: parseInt(fileLineColumn[1] || '', 10) || undefined,
fileName, fileName,
fileShort: planB ? planB[1] : '', fileShort: planB ? planB[1] : '',
errMsg: planA[3] || '', errMsg: planA[3] || '',
...@@ -187,14 +187,16 @@ function utsStracktraceyPreset(opts) { ...@@ -187,14 +187,16 @@ function utsStracktraceyPreset(opts) {
}); });
}, },
asTableStacktrace({ stacktrace } = { stacktrace: '' }) { asTableStacktrace({ stacktrace } = { stacktrace: '' }) {
const stacktraceSplit = stacktrace.split('\n'); return errStack
const errorName = stacktraceSplit[0]; .map((item) => {
const errorMsg = stacktraceSplit.pop(); if (item === '%StacktraceyItem%') {
return ((errorName.indexOf('e:') === -1 ? `${errorName}\n` : '') + const _stack = stack.items.shift();
(stack.items if (_stack)
.map((item) => `e: [${item.callee}]${item.fileShort}/${item.fileName}: (${item.line}, ${item.column}): ${item.errMsg}`) return `${_stack.callee}${_stack.fileShort}/${_stack.fileName}: (${_stack.line}, ${_stack.column}): ${_stack.errMsg}`;
.join('\n') + }
(errorMsg ? `\n\n${errorMsg}` : ''))); return item;
})
.join('\n');
}, },
}; };
} }
......
...@@ -23,5 +23,7 @@ ...@@ -23,5 +23,7 @@
"stacktracey": "^2.1.8", "stacktracey": "^2.1.8",
"source-map": "^0.7.3" "source-map": "^0.7.3"
}, },
"devDependencies": {} "devDependencies": {
"@dcloudio/types": "^2.6.6"
}
} }
...@@ -120,16 +120,14 @@ function getSourceMapContent(sourcemapUrl: string) { ...@@ -120,16 +120,14 @@ function getSourceMapContent(sourcemapUrl: string) {
(sourcemapCatch[sourcemapUrl] = new Promise((resolve, reject) => { (sourcemapCatch[sourcemapUrl] = new Promise((resolve, reject) => {
try { try {
if (/^[a-z]+:/i.test(sourcemapUrl)) { if (/^[a-z]+:/i.test(sourcemapUrl)) {
/* uni uni.request({
.request(sourcemapUrl) url: sourcemapUrl,
.then((res) => { success: (res) => {
console.log('sourcemapUrl :>> ', sourcemapUrl) console.log('sourcemapUrl :>> ', sourcemapUrl)
sourcemapCatch[sourcemapUrl] = res.data sourcemapCatch[sourcemapUrl] = res.data as string
resolve(sourcemapCatch[sourcemapUrl]) resolve(sourcemapCatch[sourcemapUrl])
},
}) })
.catch((_) => {
resolve('')
}) */
} else { } else {
sourcemapCatch[sourcemapUrl] = fs.readFileSync( sourcemapCatch[sourcemapUrl] = fs.readFileSync(
sourcemapUrl, sourcemapUrl,
...@@ -212,9 +210,10 @@ export function uniStracktraceyPreset( ...@@ -212,9 +210,10 @@ export function uniStracktraceyPreset(
}, },
asTableStacktrace({ maxColumnWidths, stacktrace } = { stacktrace: '' }) { asTableStacktrace({ maxColumnWidths, stacktrace } = { stacktrace: '' }) {
const errorName = stacktrace.split('\n')[0] const errorName = stacktrace.split('\n')[0]
return errorName.indexOf('at') === -1 return (
? `${errorName}\n` (errorName.indexOf('at') === -1 ? `${errorName}\n` : '') +
: '' + (stack.asTable ? stack.asTable({ maxColumnWidths }) : '') (stack.asTable ? stack.asTable({ maxColumnWidths }) : '')
)
}, },
} }
} }
...@@ -233,10 +232,11 @@ export function utsStracktraceyPreset( ...@@ -233,10 +232,11 @@ export function utsStracktraceyPreset(
opts: UtsStracktraceyPreset opts: UtsStracktraceyPreset
): StacktraceyPreset { ): StacktraceyPreset {
let stack: Stacktracey let stack: Stacktracey
let errStack: string[] = []
return { return {
parseSourceMapUrl(file, fileName) { parseSourceMapUrl(file, fileName) {
// 根据 base,filename 组合 sourceMapUrl // 根据 base,filename 组合 sourceMapUrl
return `${opts.base}/${fileName}.map` return `${opts.base}${file.replace(opts.sourceRoot, '')}.map`
}, },
getSourceMapContent(file, fileName) { getSourceMapContent(file, fileName) {
// 根据 base,filename 组合 sourceMapUrl // 根据 base,filename 组合 sourceMapUrl
...@@ -248,7 +248,7 @@ export function utsStracktraceyPreset( ...@@ -248,7 +248,7 @@ export function utsStracktraceyPreset(
const lines = (str || '').split('\n') const lines = (str || '').split('\n')
const entries = lines const entries = lines
.map((line) => { .map((line, index) => {
line = line.trim() line = line.trim()
let callee, let callee,
...@@ -256,17 +256,19 @@ export function utsStracktraceyPreset( ...@@ -256,17 +256,19 @@ export function utsStracktraceyPreset(
planA, planA,
planB planB
if ((planA = line.match(/e: \[(.+)\](.+): (.+)/))) { if ((planA = line.match(/e: (.+\.kt)(.+\))*:\s*(.+)*/))) {
callee = planA[1] errStack.push('%StacktraceyItem%')
callee = 'e: '
fileLineColumn = ( fileLineColumn = (
planA[2].match(/(.+):.*\((\d+).+?(\d+)\)/) || [] planA[2].match(/.*:.*\((\d+).+?(\d+)\)/) || []
).slice(1) ).slice(1)
} else { } else {
errStack.push(line)
return undefined return undefined
} }
const fileName = fileLineColumn[0] const fileName = planA[1]
? (planB = fileLineColumn[0].match(/(\/.*)*\/(.+)/) || [])[2] || '' ? (planB = planA[1].match(/(.*)*\/(.+)/) || [])[2] || ''
: '' : ''
return { return {
...@@ -274,9 +276,9 @@ export function utsStracktraceyPreset( ...@@ -274,9 +276,9 @@ export function utsStracktraceyPreset(
callee: callee || '', callee: callee || '',
index: false, index: false,
native: false, native: false,
file: nixSlashes(fileLineColumn[0] || ''), file: nixSlashes(planA[1] || ''),
line: parseInt(fileLineColumn[1] || '', 10) || undefined, line: parseInt(fileLineColumn[0] || '', 10) || undefined,
column: parseInt(fileLineColumn[2] || '', 10) || undefined, column: parseInt(fileLineColumn[1] || '', 10) || undefined,
fileName, fileName,
fileShort: planB ? planB[1] : '', fileShort: planB ? planB[1] : '',
errMsg: planA[3] || '', errMsg: planA[3] || '',
...@@ -292,19 +294,16 @@ export function utsStracktraceyPreset( ...@@ -292,19 +294,16 @@ export function utsStracktraceyPreset(
}) })
}, },
asTableStacktrace({ stacktrace } = { stacktrace: '' }) { asTableStacktrace({ stacktrace } = { stacktrace: '' }) {
const stacktraceSplit = stacktrace.split('\n') return errStack
const errorName = stacktraceSplit[0] .map((item) => {
const errorMsg = stacktraceSplit.pop() if (item === '%StacktraceyItem%') {
return ( const _stack = stack.items.shift()
(errorName.indexOf('e:') === -1 ? `${errorName}\n` : '') + if (_stack)
(stack.items return `${_stack.callee}${_stack.fileShort}/${_stack.fileName}: (${_stack.line}, ${_stack.column}): ${_stack.errMsg}`
.map( }
(item) => return item
`e: [${item.callee}]${item.fileShort}/${item.fileName}: (${item.line}, ${item.column}): ${item.errMsg}` })
) .join('\n')
.join('\n') +
(errorMsg ? `\n\n${errorMsg}` : ''))
)
}, },
} }
} }
...@@ -5,34 +5,45 @@ const { ...@@ -5,34 +5,45 @@ const {
} = require('../dist/uni-stacktracey.cjs.js') } = require('../dist/uni-stacktracey.cjs.js')
const utsErrorMsg = `Appid: __UNI__E070870 const utsErrorMsg = `Appid: __UNI__E070870
e: [PackagePath]/wgtRoot/__UNI__E070870/nativeplugins/DCloud-UTSPlugin/android/src/io/dcloud/uniplugin/TestModule.kt: (8, 1): Expecting a top level declaration e: DCloud-UTSPlugin/android/src/io/dcloud/uniplugin/TestComponent.kt: (68, 9): Unresolved reference: hello
e: [PackagePath]/wgtRoot/__UNI__E070870/nativeplugins/DCloud-UTSPlugin/android/src/io/dcloud/uniplugin/TestModule.kt: (8, 9): Expecting a top level declaration e: DCloud-UTSPlugin/android/src/io/dcloud/uniplugin/TestModule.kt: (30, 9): Unresolved reference: hello
e: [PackagePath]/wgtRoot/__UNI__E070870/nativeplugins/DCloud-UTSPlugin/android/src/io/dcloud/uniplugin/TestModule.kt: (8, 10): Expecting a top level declaration
e: [PackagePath]/wgtRoot/__UNI__E070870/nativeplugins/DCloud-UTSPlugin/android/src/io/dcloud/uniplugin/TestModule.kt: (8, 11): Expecting a top level declaration FAILURE: Build failed with an exception.
e: [PackagePath]/wgtRoot/__UNI__E070870/nativeplugins/DCloud-UTSPlugin/android/src/io/dcloud/uniplugin/TestModule.kt: (8, 16): Expecting a top level declaration
* What went wrong:
FAILURE: Build failed with an exception.` Execution failed for task ':nativeplugins:DCloud-UTSPlugin:compileReleaseKotlin'.
> Compilation error. See log for more details
stacktracey(
`ReferenceError: Sentry is not defined * Try:
at Proxy.throwError(/vue3/assets/pages-index-index.4077a069.js:1:295) Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
at e(/vue3/assets/index.2be9343a.js:1:52317)
at Ue(/vue3/assets/index.2be9343a.js:1:16271) * Get more help at https://help.gradle.org
at He(/vue3/assets/index.2be9343a.js:1:16349)
at HTMLElement.n(/vue3/assets/index.2be9343a.js:1:51834) BUILD FAILED in 3s`
at HTMLElement.o(/vue3/assets/index.2be9343a.js:21:60087)`,
{ const uniErrorMsg = `ReferenceError: Sentry is not defined
at Proxy.throwError(/vue3/assets/pages-index-index.4077a069.js:1:295)
at e(/vue3/assets/index.2be9343a.js:1:52317)
at Ue(/vue3/assets/index.2be9343a.js:1:16271)
at He(/vue3/assets/index.2be9343a.js:1:16349)
at HTMLElement.n(/vue3/assets/index.2be9343a.js:1:51834)
at HTMLElement.o(/vue3/assets/index.2be9343a.js:21:60087)`
stacktracey(uniErrorMsg, {
preset: uniStracktraceyPreset({ preset: uniStracktraceyPreset({
base: 'https://7463-tcb-uzyfn59tqxjxtnbab2e2c-5ba40b-1303909289.tcb.qcloud.la', base: 'https://7463-tcb-uzyfn59tqxjxtnbab2e2c-5ba40b-1303909289.tcb.qcloud.la',
platform: 'h5', platform: 'h5',
version: '1.0.0', version: '1.0.0',
}), }),
} }).then((res) => {
).then((res) => console.log(res)) console.log('res :>> ', res)
})
/* stacktracey(utsErrorMsg, { /* stacktracey(utsErrorMsg, {
preset: utsStracktraceyPreset({ preset: utsStracktraceyPreset({
base: '/wgtRoot/__UNI__E070870/nativeplugins/DCloud-UTSPlugin/android/src/io/dcloud/uniplugin', base: '/usr/fxy/poroject/test/.sourcemap/src/',
sourceRoot: '', sourceRoot:
'/wgtRoot/__UNI__E070870/nativeplugins/DCloud-UTSPlugin/android/src/',
}), }),
}).then((res) => console.log(res)) */ }).then((res) => {
console.log('res :>> ', res);
}) */
...@@ -15,6 +15,10 @@ ...@@ -15,6 +15,10 @@
"lib": [ "lib": [
"ESNext", "ESNext",
"DOM" "DOM"
],
"types": [
"node",
"@dcloudio/types"
] ]
}, },
"include": [ "include": [
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册