提交 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`)
})
})
})
......@@ -11,192 +11,194 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
var StackTracey__default = /*#__PURE__*/_interopDefaultLegacy(StackTracey);
const nixSlashes = (x) => x.replace(/\\/g, '/');
const sourcemapCatch = {};
function stacktracey(stacktrace, opts) {
const parseStack = [];
const stack = opts.preset.parseStacktrace(stacktrace);
stack.items.forEach((item, index) => {
const fn = () => {
const { line = 0, column = 0, file, fileName } = item;
try {
return opts.preset
.getSourceMapContent(file, fileName)
.then((content) => {
if (content)
return sourceMap.SourceMapConsumer.with(content, null, (consumer) => {
const sourceMapContent = parseSourceMapContent(consumer, {
line,
column,
});
if (sourceMapContent) {
const { source, sourcePath, sourceLine, sourceColumn, fileName = '', } = sourceMapContent;
stack.items[index] = Object.assign({}, item, {
file: source,
line: sourceLine,
column: sourceColumn,
fileShort: sourcePath,
fileRelative: sourcePath,
fileName,
});
}
});
});
}
catch (error) {
return Promise.resolve();
}
};
parseStack.push(fn());
});
return new Promise((resolve, reject) => {
Promise.all(parseStack)
.then(() => {
const parseError = opts.preset.asTableStacktrace({
maxColumnWidths: {
callee: 999,
file: 999,
sourceLine: 999,
},
stacktrace,
});
resolve(parseError);
})
.catch(() => {
resolve(stacktrace);
});
});
}
function getSourceMapContent(sourcemapUrl) {
try {
return (sourcemapCatch[sourcemapUrl] ||
(sourcemapCatch[sourcemapUrl] = new Promise((resolve, reject) => {
try {
if (/^[a-z]+:/i.test(sourcemapUrl)) {
/* uni
.request(sourcemapUrl)
.then((res) => {
console.log('sourcemapUrl :>> ', sourcemapUrl)
sourcemapCatch[sourcemapUrl] = res.data
resolve(sourcemapCatch[sourcemapUrl])
})
.catch((_) => {
resolve('')
}) */
}
else {
sourcemapCatch[sourcemapUrl] = fs__default["default"].readFileSync(sourcemapUrl, 'utf-8');
resolve(sourcemapCatch[sourcemapUrl]);
}
}
catch (error) {
resolve('');
}
})));
}
catch (error) {
return '';
}
}
function parseSourceMapContent(consumer, obj) {
// source -> 'uni-app:///node_modules/@sentry/browser/esm/helpers.js'
const { source, line: sourceLine, column: sourceColumn, } = consumer.originalPositionFor(obj);
if (source) {
const sourcePathSplit = source.split('/');
const sourcePath = sourcePathSplit.slice(3).join('/');
const fileName = sourcePathSplit.pop();
return {
source,
sourcePath,
sourceLine: sourceLine === null ? 0 : sourceLine,
sourceColumn: sourceColumn === null ? 0 : sourceColumn,
fileName,
};
}
}
function uniStracktraceyPreset(opts) {
const { base, platform, version } = opts;
let stack;
return {
parseSourceMapUrl(file, fileName) {
if (!platform || !version)
return '';
// 根据 base,platform,version,filename 组合 sourceMapUrl
return `${base}/${version}/.sourcemap/${platform}/${file.split('.')[0]}.js.map`;
},
getSourceMapContent(file, fileName) {
return Promise.resolve(getSourceMapContent(this.parseSourceMapUrl(file, fileName)));
},
parseStacktrace(stacktrace) {
return (stack = new StackTracey__default["default"](stacktrace));
},
asTableStacktrace({ maxColumnWidths, stacktrace } = { stacktrace: '' }) {
const errorName = stacktrace.split('\n')[0];
return errorName.indexOf('at') === -1
? `${errorName}\n`
: '' + (stack.asTable ? stack.asTable({ maxColumnWidths }) : '');
},
};
}
function utsStracktraceyPreset(opts) {
let stack;
return {
parseSourceMapUrl(file, fileName) {
// 根据 base,filename 组合 sourceMapUrl
return `${opts.base}/${fileName}.map`;
},
getSourceMapContent(file, fileName) {
// 根据 base,filename 组合 sourceMapUrl
return Promise.resolve(getSourceMapContent(this.parseSourceMapUrl(file, fileName)));
},
parseStacktrace(str) {
const lines = (str || '').split('\n');
const entries = lines
.map((line) => {
line = line.trim();
let callee, fileLineColumn = [], planA, planB;
if ((planA = line.match(/e: \[(.+)\](.+): (.+)/))) {
callee = planA[1];
fileLineColumn = (planA[2].match(/(.+):.*\((\d+).+?(\d+)\)/) || []).slice(1);
}
else {
return undefined;
}
const fileName = fileLineColumn[0]
? (planB = fileLineColumn[0].match(/(\/.*)*\/(.+)/) || [])[2] || ''
: '';
return {
beforeParse: line,
callee: callee || '',
index: false,
native: false,
file: nixSlashes(fileLineColumn[0] || ''),
line: parseInt(fileLineColumn[1] || '', 10) || undefined,
column: parseInt(fileLineColumn[2] || '', 10) || undefined,
fileName,
fileShort: planB ? planB[1] : '',
errMsg: planA[3] || '',
calleeShort: '',
fileRelative: '',
thirdParty: false,
};
})
.filter((x) => x !== undefined);
return (stack = {
items: entries,
});
},
asTableStacktrace({ stacktrace } = { stacktrace: '' }) {
const stacktraceSplit = stacktrace.split('\n');
const errorName = stacktraceSplit[0];
const errorMsg = stacktraceSplit.pop();
return ((errorName.indexOf('e:') === -1 ? `${errorName}\n` : '') +
(stack.items
.map((item) => `e: [${item.callee}]${item.fileShort}/${item.fileName}: (${item.line}, ${item.column}): ${item.errMsg}`)
.join('\n') +
(errorMsg ? `\n\n${errorMsg}` : '')));
},
};
const nixSlashes = (x) => x.replace(/\\/g, '/');
const sourcemapCatch = {};
function stacktracey(stacktrace, opts) {
const parseStack = [];
const stack = opts.preset.parseStacktrace(stacktrace);
stack.items.forEach((item, index) => {
const fn = () => {
const { line = 0, column = 0, file, fileName } = item;
try {
return opts.preset
.getSourceMapContent(file, fileName)
.then((content) => {
if (content)
return sourceMap.SourceMapConsumer.with(content, null, (consumer) => {
const sourceMapContent = parseSourceMapContent(consumer, {
line,
column,
});
if (sourceMapContent) {
const { source, sourcePath, sourceLine, sourceColumn, fileName = '', } = sourceMapContent;
stack.items[index] = Object.assign({}, item, {
file: source,
line: sourceLine,
column: sourceColumn,
fileShort: sourcePath,
fileRelative: sourcePath,
fileName,
});
}
});
});
}
catch (error) {
return Promise.resolve();
}
};
parseStack.push(fn());
});
return new Promise((resolve, reject) => {
Promise.all(parseStack)
.then(() => {
const parseError = opts.preset.asTableStacktrace({
maxColumnWidths: {
callee: 999,
file: 999,
sourceLine: 999,
},
stacktrace,
});
resolve(parseError);
})
.catch(() => {
resolve(stacktrace);
});
});
}
function getSourceMapContent(sourcemapUrl) {
try {
return (sourcemapCatch[sourcemapUrl] ||
(sourcemapCatch[sourcemapUrl] = new Promise((resolve, reject) => {
try {
if (/^[a-z]+:/i.test(sourcemapUrl)) {
uni.request({
url: sourcemapUrl,
success: (res) => {
console.log('sourcemapUrl :>> ', sourcemapUrl);
sourcemapCatch[sourcemapUrl] = res.data;
resolve(sourcemapCatch[sourcemapUrl]);
},
});
}
else {
sourcemapCatch[sourcemapUrl] = fs__default["default"].readFileSync(sourcemapUrl, 'utf-8');
resolve(sourcemapCatch[sourcemapUrl]);
}
}
catch (error) {
resolve('');
}
})));
}
catch (error) {
return '';
}
}
function parseSourceMapContent(consumer, obj) {
// source -> 'uni-app:///node_modules/@sentry/browser/esm/helpers.js'
const { source, line: sourceLine, column: sourceColumn, } = consumer.originalPositionFor(obj);
if (source) {
const sourcePathSplit = source.split('/');
const sourcePath = sourcePathSplit.slice(3).join('/');
const fileName = sourcePathSplit.pop();
return {
source,
sourcePath,
sourceLine: sourceLine === null ? 0 : sourceLine,
sourceColumn: sourceColumn === null ? 0 : sourceColumn,
fileName,
};
}
}
function uniStracktraceyPreset(opts) {
const { base, platform, version } = opts;
let stack;
return {
parseSourceMapUrl(file, fileName) {
if (!platform || !version)
return '';
// 根据 base,platform,version,filename 组合 sourceMapUrl
return `${base}/${version}/.sourcemap/${platform}/${file.split('.')[0]}.js.map`;
},
getSourceMapContent(file, fileName) {
return Promise.resolve(getSourceMapContent(this.parseSourceMapUrl(file, fileName)));
},
parseStacktrace(stacktrace) {
return (stack = new StackTracey__default["default"](stacktrace));
},
asTableStacktrace({ maxColumnWidths, stacktrace } = { stacktrace: '' }) {
const errorName = stacktrace.split('\n')[0];
return ((errorName.indexOf('at') === -1 ? `${errorName}\n` : '') +
(stack.asTable ? stack.asTable({ maxColumnWidths }) : ''));
},
};
}
function utsStracktraceyPreset(opts) {
let stack;
let errStack = [];
return {
parseSourceMapUrl(file, fileName) {
// 根据 base,filename 组合 sourceMapUrl
return `${opts.base}${file.replace(opts.sourceRoot, '')}.map`;
},
getSourceMapContent(file, fileName) {
// 根据 base,filename 组合 sourceMapUrl
return Promise.resolve(getSourceMapContent(this.parseSourceMapUrl(file, fileName)));
},
parseStacktrace(str) {
const lines = (str || '').split('\n');
const entries = lines
.map((line, index) => {
line = line.trim();
let callee, fileLineColumn = [], planA, planB;
if ((planA = line.match(/e: (.+\.kt)(.+\))*:\s*(.+)*/))) {
errStack.push('%StacktraceyItem%');
callee = 'e: ';
fileLineColumn = (planA[2].match(/.*:.*\((\d+).+?(\d+)\)/) || []).slice(1);
}
else {
errStack.push(line);
return undefined;
}
const fileName = planA[1]
? (planB = planA[1].match(/(.*)*\/(.+)/) || [])[2] || ''
: '';
return {
beforeParse: line,
callee: callee || '',
index: false,
native: false,
file: nixSlashes(planA[1] || ''),
line: parseInt(fileLineColumn[0] || '', 10) || undefined,
column: parseInt(fileLineColumn[1] || '', 10) || undefined,
fileName,
fileShort: planB ? planB[1] : '',
errMsg: planA[3] || '',
calleeShort: '',
fileRelative: '',
thirdParty: false,
};
})
.filter((x) => x !== undefined);
return (stack = {
items: entries,
});
},
asTableStacktrace({ stacktrace } = { stacktrace: '' }) {
return errStack
.map((item) => {
if (item === '%StacktraceyItem%') {
const _stack = stack.items.shift();
if (_stack)
return `${_stack.callee}${_stack.fileShort}/${_stack.fileName}: (${_stack.line}, ${_stack.column}): ${_stack.errMsg}`;
}
return item;
})
.join('\n');
},
};
}
exports.stacktracey = stacktracey;
......
......@@ -23,5 +23,7 @@
"stacktracey": "^2.1.8",
"source-map": "^0.7.3"
},
"devDependencies": {}
"devDependencies": {
"@dcloudio/types": "^2.6.6"
}
}
......@@ -120,16 +120,14 @@ function getSourceMapContent(sourcemapUrl: string) {
(sourcemapCatch[sourcemapUrl] = new Promise((resolve, reject) => {
try {
if (/^[a-z]+:/i.test(sourcemapUrl)) {
/* uni
.request(sourcemapUrl)
.then((res) => {
uni.request({
url: sourcemapUrl,
success: (res) => {
console.log('sourcemapUrl :>> ', sourcemapUrl)
sourcemapCatch[sourcemapUrl] = res.data
sourcemapCatch[sourcemapUrl] = res.data as string
resolve(sourcemapCatch[sourcemapUrl])
})
.catch((_) => {
resolve('')
}) */
},
})
} else {
sourcemapCatch[sourcemapUrl] = fs.readFileSync(
sourcemapUrl,
......@@ -212,9 +210,10 @@ export function uniStracktraceyPreset(
},
asTableStacktrace({ maxColumnWidths, stacktrace } = { stacktrace: '' }) {
const errorName = stacktrace.split('\n')[0]
return errorName.indexOf('at') === -1
? `${errorName}\n`
: '' + (stack.asTable ? stack.asTable({ maxColumnWidths }) : '')
return (
(errorName.indexOf('at') === -1 ? `${errorName}\n` : '') +
(stack.asTable ? stack.asTable({ maxColumnWidths }) : '')
)
},
}
}
......@@ -233,10 +232,11 @@ export function utsStracktraceyPreset(
opts: UtsStracktraceyPreset
): StacktraceyPreset {
let stack: Stacktracey
let errStack: string[] = []
return {
parseSourceMapUrl(file, fileName) {
// 根据 base,filename 组合 sourceMapUrl
return `${opts.base}/${fileName}.map`
return `${opts.base}${file.replace(opts.sourceRoot, '')}.map`
},
getSourceMapContent(file, fileName) {
// 根据 base,filename 组合 sourceMapUrl
......@@ -248,7 +248,7 @@ export function utsStracktraceyPreset(
const lines = (str || '').split('\n')
const entries = lines
.map((line) => {
.map((line, index) => {
line = line.trim()
let callee,
......@@ -256,17 +256,19 @@ export function utsStracktraceyPreset(
planA,
planB
if ((planA = line.match(/e: \[(.+)\](.+): (.+)/))) {
callee = planA[1]
if ((planA = line.match(/e: (.+\.kt)(.+\))*:\s*(.+)*/))) {
errStack.push('%StacktraceyItem%')
callee = 'e: '
fileLineColumn = (
planA[2].match(/(.+):.*\((\d+).+?(\d+)\)/) || []
planA[2].match(/.*:.*\((\d+).+?(\d+)\)/) || []
).slice(1)
} else {
errStack.push(line)
return undefined
}
const fileName = fileLineColumn[0]
? (planB = fileLineColumn[0].match(/(\/.*)*\/(.+)/) || [])[2] || ''
const fileName = planA[1]
? (planB = planA[1].match(/(.*)*\/(.+)/) || [])[2] || ''
: ''
return {
......@@ -274,9 +276,9 @@ export function utsStracktraceyPreset(
callee: callee || '',
index: false,
native: false,
file: nixSlashes(fileLineColumn[0] || ''),
line: parseInt(fileLineColumn[1] || '', 10) || undefined,
column: parseInt(fileLineColumn[2] || '', 10) || undefined,
file: nixSlashes(planA[1] || ''),
line: parseInt(fileLineColumn[0] || '', 10) || undefined,
column: parseInt(fileLineColumn[1] || '', 10) || undefined,
fileName,
fileShort: planB ? planB[1] : '',
errMsg: planA[3] || '',
......@@ -292,19 +294,16 @@ export function utsStracktraceyPreset(
})
},
asTableStacktrace({ stacktrace } = { stacktrace: '' }) {
const stacktraceSplit = stacktrace.split('\n')
const errorName = stacktraceSplit[0]
const errorMsg = stacktraceSplit.pop()
return (
(errorName.indexOf('e:') === -1 ? `${errorName}\n` : '') +
(stack.items
.map(
(item) =>
`e: [${item.callee}]${item.fileShort}/${item.fileName}: (${item.line}, ${item.column}): ${item.errMsg}`
)
.join('\n') +
(errorMsg ? `\n\n${errorMsg}` : ''))
)
return errStack
.map((item) => {
if (item === '%StacktraceyItem%') {
const _stack = stack.items.shift()
if (_stack)
return `${_stack.callee}${_stack.fileShort}/${_stack.fileName}: (${_stack.line}, ${_stack.column}): ${_stack.errMsg}`
}
return item
})
.join('\n')
},
}
}
......@@ -5,34 +5,45 @@ const {
} = require('../dist/uni-stacktracey.cjs.js')
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: [PackagePath]/wgtRoot/__UNI__E070870/nativeplugins/DCloud-UTSPlugin/android/src/io/dcloud/uniplugin/TestModule.kt: (8, 9): Expecting a top level declaration
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
e: [PackagePath]/wgtRoot/__UNI__E070870/nativeplugins/DCloud-UTSPlugin/android/src/io/dcloud/uniplugin/TestModule.kt: (8, 16): Expecting a top level declaration
FAILURE: Build failed with an exception.`
stacktracey(
`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)`,
{
preset: uniStracktraceyPreset({
base: 'https://7463-tcb-uzyfn59tqxjxtnbab2e2c-5ba40b-1303909289.tcb.qcloud.la',
platform: 'h5',
version: '1.0.0',
}),
}
).then((res) => console.log(res))
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)`
stacktracey(uniErrorMsg, {
preset: uniStracktraceyPreset({
base: 'https://7463-tcb-uzyfn59tqxjxtnbab2e2c-5ba40b-1303909289.tcb.qcloud.la',
platform: 'h5',
version: '1.0.0',
}),
}).then((res) => {
console.log('res :>> ', res)
})
/* stacktracey(utsErrorMsg, {
preset: utsStracktraceyPreset({
base: '/wgtRoot/__UNI__E070870/nativeplugins/DCloud-UTSPlugin/android/src/io/dcloud/uniplugin',
sourceRoot: '',
base: '/usr/fxy/poroject/test/.sourcemap/src/',
sourceRoot:
'/wgtRoot/__UNI__E070870/nativeplugins/DCloud-UTSPlugin/android/src/',
}),
}).then((res) => console.log(res)) */
}).then((res) => {
console.log('res :>> ', res);
}) */
......@@ -15,6 +15,10 @@
"lib": [
"ESNext",
"DOM"
],
"types": [
"node",
"@dcloudio/types"
]
},
"include": [
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册