Skip to content

Commit 04811c0

Browse files
committed
Merge pull request #3 from gophergala2016/gore-rewrite
More useful error handling in the notebook
2 parents 44dfbf4 + 7d731d2 commit 04811c0

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

execution.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ func SetupExecutionEnvironment() {
2424
panic(err)
2525
}
2626

27-
for _, imp := range REPLSession.File.Imports {
28-
fmt.Println(imp.Name)
29-
imp.Name = nil
30-
}
31-
3227
World = eval.NewWorld()
3328
fset = token.NewFileSet()
3429
}
@@ -74,8 +69,7 @@ func HandleExecuteRequest(receipt MsgReceipt) {
7469
// with customer gore functionality
7570

7671
//testerr := REPLSession.Eval(code)
77-
val, err := REPLSession.Eval(code)
78-
fmt.Println(err)
72+
val, err, stderr := REPLSession.Eval(code)
7973
//val, err := RunCode(code)
8074

8175

@@ -99,9 +93,11 @@ func HandleExecuteRequest(receipt MsgReceipt) {
9993
content["status"] = "error"
10094
content["ename"] = "ERROR"
10195
content["evalue"] = err.Error()
102-
content["traceback"] = []string{err.Error()}
96+
//content["traceback"] = []string{err.Error()}
97+
content["traceback"] = []string{stderr.String()}
10398
errormsg := NewMsg("pyerr", receipt.Msg)
104-
errormsg.Content = ErrMsg{"Error", err.Error(), []string{err.Error()}}
99+
//errormsg.Content = ErrMsg{"Error", err.Error(), []string{err.Error()}}
100+
errormsg.Content = ErrMsg{"Error", err.Error(), []string{stderr.String()}}
105101
receipt.SendResponse(receipt.Sockets.IOPub_socket, errormsg)
106102
}
107103
reply.Content = content

replpkg/repl.go

+17-12
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,15 @@ func (s *Session) mainFunc() *ast.FuncDecl {
227227
return s.File.Scope.Lookup("main").Decl.(*ast.FuncDecl)
228228
}
229229

230-
func (s *Session) Run() ([]byte, error) {
230+
func (s *Session) Run() ([]byte, error, bytes.Buffer) {
231231
f, err := os.Create(s.FilePath)
232232
if err != nil {
233-
return []byte{}, err
233+
return []byte{}, err, bytes.Buffer{}
234234
}
235235

236236
err = printer.Fprint(f, s.Fset, s.File)
237237
if err != nil {
238-
return []byte{}, err
238+
return []byte{}, err, bytes.Buffer{}
239239
}
240240

241241
return goRun(append(s.ExtraFilePaths, s.FilePath))
@@ -255,14 +255,19 @@ func tempFile() (string, error) {
255255
return filepath.Join(dir, "gore_session.go"), nil
256256
}
257257

258-
func goRun(files []string) ([]byte, error) {
258+
func goRun(files []string) ([]byte, error, bytes.Buffer) {
259+
260+
var stderr bytes.Buffer
261+
259262
args := append([]string{"run"}, files...)
260263
debugf("go %s", strings.Join(args, " "))
261264
cmd := exec.Command("go", args...)
262265
cmd.Stdin = os.Stdin
263266
//cmd.Stdout = os.Stdout
264-
cmd.Stderr = os.Stderr
265-
return cmd.Output()
267+
//cmd.Stderr = os.Stderr
268+
cmd.Stderr = &stderr
269+
out, err := cmd.Output()
270+
return out, err, stderr
266271
//return cmd.Run()
267272
}
268273

@@ -379,7 +384,7 @@ func (s *Session) reset() error {
379384
return nil
380385
}
381386

382-
func (s *Session) Eval(in string) (string, error) {
387+
func (s *Session) Eval(in string) (string, error, bytes.Buffer) {
383388
debugf("eval >>> %q", in)
384389

385390
s.clearQuickFix()
@@ -397,7 +402,7 @@ func (s *Session) Eval(in string) (string, error) {
397402
err := command.action(s, arg)
398403
if err != nil {
399404
if err == ErrQuit {
400-
return "", err
405+
return "", err, bytes.Buffer{}
401406
}
402407
errorf("%s: %s", command.name, err)
403408
}
@@ -408,7 +413,7 @@ func (s *Session) Eval(in string) (string, error) {
408413

409414
if commandRan {
410415
s.doQuickFix()
411-
return "", nil
416+
return "", nil, bytes.Buffer{}
412417
}
413418

414419
if _, err := s.evalExpr(in); err != nil {
@@ -419,7 +424,7 @@ func (s *Session) Eval(in string) (string, error) {
419424
debugf("stmt :: err = %s", err)
420425

421426
if _, ok := err.(scanner.ErrorList); ok {
422-
return "", ErrContinue
427+
return "", ErrContinue, bytes.Buffer{}
423428
}
424429
}
425430
}
@@ -429,7 +434,7 @@ func (s *Session) Eval(in string) (string, error) {
429434
}
430435
s.doQuickFix()
431436

432-
output, err := s.Run()
437+
output, err, strerr := s.Run()
433438
if err != nil {
434439
if exitErr, ok := err.(*exec.ExitError); ok {
435440
// if failed with status 2, remove the last statement
@@ -443,7 +448,7 @@ func (s *Session) Eval(in string) (string, error) {
443448
errorf("%s", err)
444449
}
445450

446-
return string(output), err
451+
return string(output), err, strerr
447452
}
448453

449454
// storeMainBody stores current state of code so that it can be restored

0 commit comments

Comments
 (0)