Browse Source

Add Error() and String() to InterruptedError (#52)

* Add Error() and String() to InterruptedError

* Make sure that Error() and String() have the right level of stack
Slavik Markovich 7 years ago
parent
commit
f24d0f0d86
1 changed files with 43 additions and 14 deletions
  1. 43 14
      runtime.go

+ 43 - 14
runtime.go

@@ -160,36 +160,65 @@ func (e *InterruptedError) Value() interface{} {
 	return e.iface
 }
 
-func (e *Exception) String() string {
+func (e *InterruptedError) String() string {
 	if e == nil {
 		return "<nil>"
 	}
 	var b bytes.Buffer
-	if e.val != nil {
-		b.WriteString(e.val.String())
+	if e.iface != nil {
+		b.WriteString(fmt.Sprint(e.iface))
+		b.WriteByte('\n')
+	}
+	e.writeFullStack(&b)
+	return b.String()
+}
+
+func (e *InterruptedError) Error() string {
+	if e == nil || e.iface == nil {
+		return "<nil>"
 	}
-	b.WriteByte('\n')
+	var b bytes.Buffer
+	b.WriteString(fmt.Sprint(e.iface))
+	e.writeShortStack(&b)
+	return b.String()
+}
+
+func (e *Exception) writeFullStack(b *bytes.Buffer) {
 	for _, frame := range e.stack {
 		b.WriteString("\tat ")
-		frame.write(&b)
+		frame.write(b)
 		b.WriteByte('\n')
 	}
-	return b.String()
 }
 
-func (e *Exception) Error() string {
-	if e == nil || e.val == nil {
+func (e *Exception) writeShortStack(b *bytes.Buffer) {
+	if len(e.stack) > 0 && (e.stack[0].prg != nil || e.stack[0].funcName != "") {
+		b.WriteString(" at ")
+		e.stack[0].write(b)
+	}
+}
+
+func (e *Exception) String() string {
+	if e == nil {
 		return "<nil>"
 	}
-	if len(e.stack) > 0 && (e.stack[0].prg != nil || e.stack[0].funcName != "") {
-		var b bytes.Buffer
+	var b bytes.Buffer
+	if e.val != nil {
 		b.WriteString(e.val.String())
-		b.WriteString(" at ")
-		e.stack[0].write(&b)
-		return b.String()
+		b.WriteByte('\n')
 	}
+	e.writeFullStack(&b)
+	return b.String()
+}
 
-	return e.val.String()
+func (e *Exception) Error() string {
+	if e == nil || e.val == nil {
+		return "<nil>"
+	}
+	var b bytes.Buffer
+	b.WriteString(e.val.String())
+	e.writeShortStack(&b)
+	return b.String()
 }
 
 func (e *Exception) Value() Value {