Skip to content

Commit ee157bf

Browse files
authored
Merge pull request go-errors#40 from kishoresenji/master
optimize SourceLine to not create a new instance of Error from String method
2 parents b6240f0 + d88c094 commit ee157bf

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

stackframe.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (frame *StackFrame) Func() *runtime.Func {
5353
func (frame *StackFrame) String() string {
5454
str := fmt.Sprintf("%s:%d (0x%x)\n", frame.File, frame.LineNumber, frame.ProgramCounter)
5555

56-
source, err := frame.SourceLine()
56+
source, err := frame.sourceLine()
5757
if err != nil {
5858
return str
5959
}
@@ -63,13 +63,21 @@ func (frame *StackFrame) String() string {
6363

6464
// SourceLine gets the line of code (from File and Line) of the original source if possible.
6565
func (frame *StackFrame) SourceLine() (string, error) {
66+
source, err := frame.sourceLine()
67+
if err != nil {
68+
return source, New(err)
69+
}
70+
return source, err
71+
}
72+
73+
func (frame *StackFrame) sourceLine() (string, error) {
6674
if frame.LineNumber <= 0 {
6775
return "???", nil
6876
}
6977

7078
file, err := os.Open(frame.File)
7179
if err != nil {
72-
return "", New(err)
80+
return "", err
7381
}
7482
defer file.Close()
7583

@@ -82,7 +90,7 @@ func (frame *StackFrame) SourceLine() (string, error) {
8290
currentLine++
8391
}
8492
if err := scanner.Err(); err != nil {
85-
return "", New(err)
93+
return "", err
8694
}
8795

8896
return "???", nil

0 commit comments

Comments
 (0)