@lvce-editor/pty-host 0.17.3 → 0.17.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lvce-editor/pty-host",
|
|
3
|
-
"version": "0.17.
|
|
3
|
+
"version": "0.17.5",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"@babel/code-frame": "^7.22.10",
|
|
20
|
+
"@lvce-editor/pretty-error": "^1.0.0",
|
|
20
21
|
"debug": "^4.3.4",
|
|
21
22
|
"lines-and-columns": "^2.0.3",
|
|
22
23
|
"verror": "^1.10.1",
|
|
@@ -1,139 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { LinesAndColumns } from 'lines-and-columns'
|
|
3
|
-
import { readFileSync } from 'node:fs'
|
|
4
|
-
import { fileURLToPath } from 'node:url'
|
|
5
|
-
import { AssertionError } from '../AssertionError/AssertionError.js'
|
|
6
|
-
import * as CleanStack from '../CleanStack/CleanStack.js'
|
|
7
|
-
import * as EncodingType from '../EncodingType/EncodingType.js'
|
|
8
|
-
import * as ErrorCodes from '../ErrorCodes/ErrorCodes.js'
|
|
9
|
-
import * as JoinLines from '../JoinLines/JoinLines.js'
|
|
10
|
-
import * as Json from '../Json/Json.js'
|
|
11
|
-
import * as SplitLines from '../SplitLines/SplitLines.js'
|
|
12
|
-
|
|
13
|
-
const getActualPath = (fileUri) => {
|
|
14
|
-
if (fileUri.startsWith('file://')) {
|
|
15
|
-
return fileURLToPath(fileUri)
|
|
16
|
-
}
|
|
17
|
-
return fileUri
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const RE_MODULE_NOT_FOUND_STACK = /Cannot find package '([^']+)' imported from (.+)$/
|
|
21
|
-
|
|
22
|
-
const prepareModuleNotFoundError = (error) => {
|
|
23
|
-
const { message } = error
|
|
24
|
-
const match = message.match(RE_MODULE_NOT_FOUND_STACK)
|
|
25
|
-
if (!match) {
|
|
26
|
-
return {
|
|
27
|
-
message,
|
|
28
|
-
stack: error.stack,
|
|
29
|
-
codeFrame: '',
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
const notFoundModule = match[1]
|
|
33
|
-
const importedFrom = match[2]
|
|
34
|
-
const rawLines = readFileSync(importedFrom, EncodingType.Utf8)
|
|
35
|
-
let line = 0
|
|
36
|
-
let column = 0
|
|
37
|
-
const splittedLines = SplitLines.splitLines(rawLines)
|
|
38
|
-
for (let i = 0; i < splittedLines.length; i++) {
|
|
39
|
-
const splittedLine = splittedLines[i]
|
|
40
|
-
const index = splittedLine.indexOf(notFoundModule)
|
|
41
|
-
if (index !== -1) {
|
|
42
|
-
line = i + 1
|
|
43
|
-
column = index
|
|
44
|
-
break
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
const location = {
|
|
48
|
-
start: {
|
|
49
|
-
line,
|
|
50
|
-
column,
|
|
51
|
-
},
|
|
52
|
-
}
|
|
53
|
-
const codeFrame = codeFrameColumns(rawLines, location)
|
|
54
|
-
const stackLines = SplitLines.splitLines(error.stack)
|
|
55
|
-
const newStackLines = [stackLines[0], ` at ${importedFrom}:${line}:${column}`, ...stackLines.slice(1)]
|
|
56
|
-
const newStack = JoinLines.joinLines(newStackLines)
|
|
57
|
-
return {
|
|
58
|
-
message,
|
|
59
|
-
stack: newStack,
|
|
60
|
-
codeFrame,
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const getStackLinesToCut = (error) => {
|
|
65
|
-
if (error instanceof AssertionError) {
|
|
66
|
-
return 1
|
|
67
|
-
}
|
|
68
|
-
return 0
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export const prepare = (error) => {
|
|
72
|
-
if (error && error.code === ErrorCodes.ERR_MODULE_NOT_FOUND) {
|
|
73
|
-
return prepareModuleNotFoundError(error)
|
|
74
|
-
}
|
|
75
|
-
const { message } = error
|
|
76
|
-
if (error && error.cause) {
|
|
77
|
-
const cause = error.cause()
|
|
78
|
-
if (cause) {
|
|
79
|
-
error = cause
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
const linesToCut = getStackLinesToCut(error)
|
|
83
|
-
const lines = CleanStack.cleanStack(error.stack).slice(linesToCut)
|
|
84
|
-
const file = lines[0]
|
|
85
|
-
let codeFrame = ''
|
|
86
|
-
if (error.codeFrame) {
|
|
87
|
-
codeFrame = error.codeFrame
|
|
88
|
-
} else if (file) {
|
|
89
|
-
let match = file.match(/\((.*):(\d+):(\d+)\)$/)
|
|
90
|
-
if (!match) {
|
|
91
|
-
match = file.match(/at (.*):(\d+):(\d+)$/)
|
|
92
|
-
}
|
|
93
|
-
if (match) {
|
|
94
|
-
const [_, path, line, column] = match
|
|
95
|
-
const actualPath = getActualPath(path)
|
|
96
|
-
const rawLines = readFileSync(actualPath, EncodingType.Utf8)
|
|
97
|
-
const location = {
|
|
98
|
-
start: {
|
|
99
|
-
line: Number.parseInt(line),
|
|
100
|
-
column: Number.parseInt(column),
|
|
101
|
-
},
|
|
102
|
-
}
|
|
103
|
-
codeFrame = codeFrameColumns(rawLines, location)
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
const relevantStack = JoinLines.joinLines(lines)
|
|
107
|
-
return {
|
|
108
|
-
message,
|
|
109
|
-
stack: relevantStack,
|
|
110
|
-
codeFrame,
|
|
111
|
-
type: error.constructor.name,
|
|
112
|
-
code: error.code,
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const fixBackslashes = (string) => {
|
|
117
|
-
return string.replaceAll('\\\\', '\\')
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export const prepareJsonError = (json, property, message) => {
|
|
121
|
-
const string = fixBackslashes(Json.stringify(json))
|
|
122
|
-
const stringifiedPropertyName = `"${property}"`
|
|
123
|
-
const index = string.indexOf(stringifiedPropertyName) // TODO this could be wrong in some cases, find a better way
|
|
124
|
-
console.log({ string, index })
|
|
125
|
-
const jsonError = {
|
|
126
|
-
stack: '',
|
|
127
|
-
}
|
|
128
|
-
if (index !== -1) {
|
|
129
|
-
const lines = new LinesAndColumns(string)
|
|
130
|
-
const location = lines.locationForIndex(index + stringifiedPropertyName.length + 1)
|
|
131
|
-
const codeFrame = codeFrameColumns(string, {
|
|
132
|
-
// @ts-ignore
|
|
133
|
-
start: { line: location.line + 1, column: location.column + 1 },
|
|
134
|
-
})
|
|
135
|
-
jsonError.codeFrame = codeFrame
|
|
136
|
-
}
|
|
137
|
-
// jsonError.stack = `${bottomMessage}\n at ${filePath}`
|
|
138
|
-
return jsonError
|
|
139
|
-
}
|
|
1
|
+
export { prepare, prepareJsonError } from '@lvce-editor/pretty-error'
|