Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
14ae5708
Commit
14ae5708
authored
9 years ago
by
Péter Szilágyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
console, internal/jsre: colorize JavaScript exceptions too
parent
ffaf58f0
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
37 additions
and
12 deletions
+37
-12
console.go
console/console.go
+5
-10
console_test.go
console/console_test.go
+12
-0
jsre.go
internal/jsre/jsre.go
+2
-2
pretty.go
internal/jsre/pretty.go
+18
-0
No files found.
console/console.go
View file @
14ae5708
...
...
@@ -182,7 +182,11 @@ func (c *Console) init(preload []string) error {
// Preload any JavaScript files before starting the console
for
_
,
path
:=
range
preload
{
if
err
:=
c
.
jsre
.
Exec
(
path
);
err
!=
nil
{
return
fmt
.
Errorf
(
"%s: %v"
,
path
,
jsErrorString
(
err
))
failure
:=
err
.
Error
()
if
ottoErr
,
ok
:=
err
.
(
*
otto
.
Error
);
ok
{
failure
=
ottoErr
.
String
()
}
return
fmt
.
Errorf
(
"%s: %v"
,
path
,
failure
)
}
}
// Configure the console's input prompter for scrollback and tab completion
...
...
@@ -269,7 +273,6 @@ func (c *Console) Evaluate(statement string) error {
}
}()
if
err
:=
c
.
jsre
.
Evaluate
(
statement
,
c
.
printer
);
err
!=
nil
{
fmt
.
Fprintf
(
c
.
printer
,
"%v
\n
"
,
jsErrorString
(
err
))
return
err
}
return
nil
...
...
@@ -359,11 +362,3 @@ func (c *Console) Stop(graceful bool) error {
c
.
jsre
.
Stop
(
graceful
)
return
nil
}
// jsErrorString adds a backtrace to errors generated by otto.
func
jsErrorString
(
err
error
)
string
{
if
ottoErr
,
ok
:=
err
.
(
*
otto
.
Error
);
ok
{
return
ottoErr
.
String
()
}
return
err
.
Error
()
}
This diff is collapsed.
Click to expand it.
console/console_test.go
View file @
14ae5708
...
...
@@ -281,3 +281,15 @@ func TestPrettyPrint(t *testing.T) {
t
.
Fatalf
(
"pretty print mismatch: have %s, want %s"
,
output
,
want
)
}
}
// Tests that the JavaScript exceptions are properly formatted and colored.
func
TestPrettyError
(
t
*
testing
.
T
)
{
tester
:=
newTester
(
t
,
nil
)
defer
tester
.
Close
(
t
)
tester
.
console
.
Evaluate
(
"throw 'hello'"
)
want
:=
jsre
.
ErrorColor
(
"hello"
)
+
"
\n
"
if
output
:=
string
(
tester
.
output
.
Bytes
());
output
!=
want
{
t
.
Fatalf
(
"pretty error mismatch: have %s, want %s"
,
output
,
want
)
}
}
This diff is collapsed.
Click to expand it.
internal/jsre/jsre.go
View file @
14ae5708
...
...
@@ -303,11 +303,11 @@ func (self *JSRE) Evaluate(code string, w io.Writer) error {
self
.
Do
(
func
(
vm
*
otto
.
Otto
)
{
val
,
err
:=
vm
.
Run
(
code
)
if
err
!=
nil
{
fail
=
err
prettyError
(
vm
,
err
,
w
)
}
else
{
prettyPrint
(
vm
,
val
,
w
)
fmt
.
Fprintln
(
w
)
}
fmt
.
Fprintln
(
w
)
})
return
fail
}
...
...
This diff is collapsed.
Click to expand it.
internal/jsre/pretty.go
View file @
14ae5708
...
...
@@ -37,6 +37,7 @@ var (
SpecialColor
=
color
.
New
(
color
.
Bold
)
.
SprintfFunc
()
NumberColor
=
color
.
New
(
color
.
FgRed
)
.
SprintfFunc
()
StringColor
=
color
.
New
(
color
.
FgGreen
)
.
SprintfFunc
()
ErrorColor
=
color
.
New
(
color
.
FgHiRed
)
.
SprintfFunc
()
)
// these fields are hidden when printing objects.
...
...
@@ -55,6 +56,23 @@ func prettyPrint(vm *otto.Otto, value otto.Value, w io.Writer) {
ppctx
{
vm
:
vm
,
w
:
w
}
.
printValue
(
value
,
0
,
false
)
}
// prettyError writes err to standard output.
func
prettyError
(
vm
*
otto
.
Otto
,
err
error
,
w
io
.
Writer
)
{
failure
:=
err
.
Error
()
if
ottoErr
,
ok
:=
err
.
(
*
otto
.
Error
);
ok
{
failure
=
ottoErr
.
String
()
}
fmt
.
Fprint
(
w
,
ErrorColor
(
"%s"
,
failure
))
}
// jsErrorString adds a backtrace to errors generated by otto.
func
jsErrorString
(
err
error
)
string
{
if
ottoErr
,
ok
:=
err
.
(
*
otto
.
Error
);
ok
{
return
ottoErr
.
String
()
}
return
err
.
Error
()
}
func
prettyPrintJS
(
call
otto
.
FunctionCall
,
w
io
.
Writer
)
otto
.
Value
{
for
_
,
v
:=
range
call
.
ArgumentList
{
prettyPrint
(
call
.
Otto
,
v
,
w
)
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment