-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved errors following guidelines here https://pkg.go.dev/errors#Is * Standardized error message formatting * Type checking (`errors.Is(err, ErrFailedLogEvent)`) * Sharable metadata (e.g. `StatusCode`, `Endpoint`) This was the original ask from Monzo https://statsigconnect.slack.com/archives/C05SN05C4N8/p1714723168057799?thread_ts=1714654581.641249&cid=C05SN05C4N8 Example: ``` [2024-06-14T12:35:11-07:00][Statsig] Failed to log 1 events: Failed request to /log_event after 0 retries: 400 Bad Request ```
- Loading branch information
1 parent
f53d35e
commit 3c9f008
Showing
6 changed files
with
183 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package statsig | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// Error Variables | ||
type StatsigError error | ||
|
||
var ( | ||
ErrFailedLogEvent StatsigError = errors.New("failed to log events") | ||
) | ||
|
||
type RequestMetadata struct { | ||
StatusCode int | ||
Endpoint string | ||
Retries int | ||
} | ||
|
||
type TransportError struct { | ||
RequestMetadata *RequestMetadata | ||
Err error | ||
} | ||
|
||
func (e *TransportError) Error() string { | ||
if e.RequestMetadata != nil { | ||
return fmt.Sprintf("Failed request to %s after %d retries: %s", e.RequestMetadata.Endpoint, e.RequestMetadata.Retries, e.Err.Error()) | ||
} else { | ||
return e.Err.Error() | ||
} | ||
} | ||
|
||
func (e *TransportError) Unwrap() error { return e.Err } | ||
|
||
type LogEventError struct { | ||
Err *TransportError | ||
Events int | ||
} | ||
|
||
func (e *LogEventError) Error() string { | ||
if e.Err != nil { | ||
return fmt.Sprintf("Failed to log %d events: %s", e.Events, e.Err.Error()) | ||
} else { | ||
return fmt.Sprintf("Failed to log %d events", e.Events) | ||
} | ||
} | ||
|
||
func (e *LogEventError) Unwrap() error { return e.Err } | ||
|
||
func (e *LogEventError) Is(target error) bool { return target == ErrFailedLogEvent } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package statsig | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestLogEventErrors(t *testing.T) { | ||
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { | ||
res.WriteHeader(http.StatusBadRequest) | ||
})) | ||
defer testServer.Close() | ||
|
||
errs := make([]error, 0) | ||
opts := &Options{ | ||
API: testServer.URL, | ||
StatsigLoggerOptions: StatsigLoggerOptions{ | ||
DisableInitDiagnostics: true, | ||
DisableSyncDiagnostics: true, | ||
DisableApiDiagnostics: true, | ||
}, | ||
OutputLoggerOptions: OutputLoggerOptions{ | ||
EnableDebug: true, | ||
LogCallback: func(message string, err error) { | ||
errs = append(errs, err) | ||
}, | ||
}, | ||
} | ||
diagnostics := newDiagnostics(opts) | ||
transport := newTransport("secret", opts) | ||
errorBoundary := newErrorBoundary("secret", opts, nil) | ||
logger := newLogger(transport, opts, diagnostics, errorBoundary) | ||
|
||
user := User{ | ||
UserID: "123", | ||
} | ||
event := Event{ | ||
EventName: "test_event", | ||
User: user, | ||
Value: "3", | ||
} | ||
|
||
stderrLogs := swallow_stderr(func() { | ||
logger.logCustom(event) | ||
logger.flush(true) | ||
}) | ||
|
||
if stderrLogs == "" { | ||
t.Errorf("Expected output to stderr") | ||
} | ||
|
||
InitializeGlobalOutputLogger(opts.OutputLoggerOptions) | ||
logger.logCustom(event) | ||
logger.flush(true) | ||
|
||
if len(errs) == 0 { | ||
t.Errorf("Expected output to callback") | ||
} | ||
|
||
if !errors.Is(errs[0], ErrFailedLogEvent) { | ||
t.Errorf("Expected error to match ErrFailedLogEvent") | ||
} | ||
|
||
if errs[0].Error() != "Failed to log 1 events: Failed request to /log_event after 0 retries: 400 Bad Request" { | ||
t.Errorf("Expected error message") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters