Skip to content

Commit

Permalink
Merge pull request #217 from bugsnag/daria-PLAT-11584-alt
Browse files Browse the repository at this point in the history
Stop trimming everything before "main.go" on main packages
  • Loading branch information
DariaKunoichi committed Mar 1, 2024
2 parents d00bba9 + 0da6720 commit 0cd4418
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
[#215](https://github.com/bugsnag/bugsnag-go/pull/215)
[Chris Duncan](https://github.com/veqryn)

* Stop trimming everything before "main.go" on main packages
[#217](https://github.com/bugsnag/bugsnag-go/pull/217)
[Chris Duncan](https://github.com/veqryn)

## 2.2.0 (2022-10-12)

### Enhancements
Expand Down
15 changes: 8 additions & 7 deletions features/fixtures/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func multipleUnhandled() {
defer notifier.AutoNotify(ctx)
panic("oops")
}

//go:noinline
func unhandledCrash() {
// Invalid type assertion, will panic
Expand Down Expand Up @@ -259,17 +260,17 @@ func handledToUnhandled() {
}

type customErr struct {
msg string
cause error
msg string
cause error
callers []uintptr
}

func newCustomErr(msg string, cause error) error {
callers := make([]uintptr, 8)
runtime.Callers(2, callers)
return customErr {
msg: msg,
cause: cause,
return customErr{
msg: msg,
cause: cause,
callers: callers,
}
}
Expand Down Expand Up @@ -297,7 +298,7 @@ func nestedHandledError() {
if val, err := checkValue(i); err != nil {
fmt.Printf("err: %v, val: %d", err, val)
}
if val, err := checkValue(i-46); err != nil {
if val, err := checkValue(i - 46); err != nil {
fmt.Printf("err: %v, val: %d", err, val)
}

Expand All @@ -317,7 +318,7 @@ func login(token string) error {
func checkValue(i int) (int, error) {
if i < 0 {
return 0, newCustomErr("invalid token", nil)
} else if i % 2 == 0 {
} else if i%2 == 0 {
return i / 2, nil
} else if i < 9 {
return i * 3, nil
Expand Down
12 changes: 12 additions & 0 deletions features/fixtures/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ services:
- PARAMS_FILTERS
- SYNCHRONOUS
- SERVER_PORT
- BUGSNAG_SOURCE_ROOT
- BUGSNAG_PROJECT_PACKAGES
restart: "no"

autoconfigure:
Expand Down Expand Up @@ -72,6 +74,8 @@ services:
- AUTO_CAPTURE_SESSIONS
- SYNCHRONOUS
- SERVER_PORT
- BUGSNAG_SOURCE_ROOT
- BUGSNAG_PROJECT_PACKAGES
restart: "no"
command: go run main.go

Expand All @@ -97,6 +101,8 @@ services:
- AUTO_CAPTURE_SESSIONS
- SYNCHRONOUS
- SERVER_PORT
- BUGSNAG_SOURCE_ROOT
- BUGSNAG_PROJECT_PACKAGES
restart: "no"
command: go run main.go

Expand All @@ -121,6 +127,8 @@ services:
- AUTO_CAPTURE_SESSIONS
- SYNCHRONOUS
- SERVER_PORT
- BUGSNAG_SOURCE_ROOT
- BUGSNAG_PROJECT_PACKAGES
restart: "no"
command: go run main.go

Expand All @@ -146,6 +154,8 @@ services:
- AUTO_CAPTURE_SESSIONS
- SYNCHRONOUS
- SERVER_PORT
- BUGSNAG_SOURCE_ROOT
- BUGSNAG_PROJECT_PACKAGES
restart: "no"
command: go run main.go

Expand Down Expand Up @@ -173,5 +183,7 @@ services:
- SYNCHRONOUS
- SERVER_PORT
- USE_PROPERTIES_FILE_CONFIG
- BUGSNAG_SOURCE_ROOT
- BUGSNAG_PROJECT_PACKAGES
restart: "no"
command: ./test/run.sh
11 changes: 6 additions & 5 deletions features/handled.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Feature: Plain handled errors

Background:
Given I set environment variable "API_KEY" to "a35a2a72bd230ac0aa0f52715bbdc6aa"
Given I set environment variable "BUGSNAG_SOURCE_ROOT" to the app directory
And I configure the bugsnag endpoint
And I have built the service "app"
And I set environment variable "AUTO_CAPTURE_SESSIONS" to "false"
Expand Down Expand Up @@ -36,7 +37,7 @@ Scenario: Sending an event using a callback to modify report contents
And the event "severityReason.type" equals "userCallbackSetSeverity"
And the event "context" equals "nonfatal.go:14"
And the "file" of stack frame 0 equals "main.go"
And stack frame 0 contains a local function spanning 241 to 247
And stack frame 0 contains a local function spanning 242 to 248
And the "file" of stack frame 1 equals ">insertion<"
And the "lineNumber" of stack frame 1 equals 0

Expand All @@ -49,7 +50,7 @@ Scenario: Marking an error as unhandled in a callback
And the event "severityReason.type" equals "userCallbackSetSeverity"
And the event "severityReason.unhandledOverridden" is true
And the "file" of stack frame 0 equals "main.go"
And stack frame 0 contains a local function spanning 253 to 256
And stack frame 0 contains a local function spanning 254 to 257

Scenario: Unwrapping the causes of a handled error
When I run the go service "app" with the test case "nested-error"
Expand All @@ -58,12 +59,12 @@ Scenario: Unwrapping the causes of a handled error
And the event "unhandled" is false
And the event "severity" equals "warning"
And the event "exceptions.0.message" equals "terminate process"
And the "lineNumber" of stack frame 0 equals 291
And the "lineNumber" of stack frame 0 equals 292
And the "file" of stack frame 0 equals "main.go"
And the "method" of stack frame 0 equals "nestedHandledError"
And the event "exceptions.1.message" equals "login failed"
And the event "exceptions.1.stacktrace.0.file" equals "main.go"
And the event "exceptions.1.stacktrace.0.lineNumber" equals 311
And the event "exceptions.1.stacktrace.0.lineNumber" equals 312
And the event "exceptions.2.message" equals "invalid token"
And the event "exceptions.2.stacktrace.0.file" equals "main.go"
And the event "exceptions.2.stacktrace.0.lineNumber" equals 319
And the event "exceptions.2.stacktrace.0.lineNumber" equals 320
1 change: 1 addition & 0 deletions features/net-http/handled.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Feature: Handled errors

Background:
Given I set environment variable "API_KEY" to "a35a2a72bd230ac0aa0f52715bbdc6aa"
Given I set environment variable "BUGSNAG_SOURCE_ROOT" to the app directory
And I configure the bugsnag endpoint
And I set environment variable "SERVER_PORT" to "4512"
And I set environment variable "AUTO_CAPTURE_SESSIONS" to "false"
Expand Down
1 change: 1 addition & 0 deletions features/plain_features/panics.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Feature: Panic handling

Background:
Given I set environment variable "API_KEY" to "a35a2a72bd230ac0aa0f52715bbdc6aa"
Given I set environment variable "BUGSNAG_SOURCE_ROOT" to the app directory
And I configure the bugsnag endpoint
And I have built the service "app"
And I set environment variable "AUTO_CAPTURE_SESSIONS" to "false"
Expand Down
5 changes: 1 addition & 4 deletions v2/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,7 @@ func (config *Configuration) isProjectPackage(_pkg string) bool {
}

func (config *Configuration) stripProjectPackages(file string) string {
trimmedFile := file
if strings.HasPrefix(trimmedFile, config.SourceRoot) {
trimmedFile = strings.TrimPrefix(trimmedFile, config.SourceRoot)
}
trimmedFile := strings.TrimPrefix(file, config.SourceRoot)
for _, p := range config.ProjectPackages {
if len(p) > 2 && p[len(p)-2] == '/' && p[len(p)-1] == '*' {
p = p[:len(p)-1]
Expand Down
13 changes: 11 additions & 2 deletions v2/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,19 @@ func generateStacktrace(err *errors.Error, config *Configuration) []StackFrame {
file := frame.File
inProject := config.isProjectPackage(frame.Package)

// remove $GOROOT and $GOHOME from other frames
if idx := strings.Index(file, frame.Package); idx > -1 {
// This will trim path before package name for external packages and golang default packages
// Excluding main package as it's special case
// This will NOT trim paths for packages in current module because path won't contain the package name
// Example: path is "/user/name/work/internal/internal.go" and module package name is "example.com/mymodule/internal"
if idx := strings.Index(file, frame.Package); idx > -1 && frame.Package != "main" {
file = file[idx:]
}

// This should trim path for main and other current module packages with correct config
// If input path is "/user/name/work/internal/internal.go"
// SourceRoot is "/user/name/work" and ProjectPackages are []string{"main*", "example.com/mymodule/**"}
// Then when package name is "example.com/mymodule/internal"
// The path will be trimmed to "/internal/internal.go"
if inProject {
file = config.stripProjectPackages(file)
}
Expand Down

0 comments on commit 0cd4418

Please sign in to comment.