github.com/nya3jp/tast@v0.0.0-20230601000426-85c8e4d83a9b/src/go.chromium.org/tast/core/internal/testing/util.go (about)

     1  // Copyright 2021 The ChromiumOS Authors
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package testing
     6  
     7  import (
     8  	"fmt"
     9  	"runtime"
    10  
    11  	"go.chromium.org/tast/core/errors/stack"
    12  	"go.chromium.org/tast/core/internal/protocol"
    13  )
    14  
    15  // NewError returns a new Error object containing reason rsn.
    16  // skipFrames contains the number of frames to skip to get the code that's reporting
    17  // the error: the caller should pass 0 to report its own frame, 1 to skip just its own frame,
    18  // 2 to additionally skip the frame that called it, and so on.
    19  func NewError(err error, fullMsg, lastMsg string, skipFrames int) *protocol.Error {
    20  	// Also skip the NewError frame.
    21  	skipFrames++
    22  
    23  	// runtime.Caller starts counting stack frames at the point of the code that
    24  	// invoked Caller.
    25  	_, fn, ln, _ := runtime.Caller(skipFrames)
    26  
    27  	trace := fmt.Sprintf("%s\n%s", lastMsg, stack.New(skipFrames))
    28  	if err != nil {
    29  		trace += fmt.Sprintf("\n%+v", err)
    30  	}
    31  
    32  	return &protocol.Error{
    33  		Reason: fullMsg,
    34  		Location: &protocol.ErrorLocation{
    35  			File:  fn,
    36  			Line:  int64(ln),
    37  			Stack: trace,
    38  		},
    39  	}
    40  }