Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.

Commit 3c52672

Browse files
author
Valentin Krasontovitsch
committed
Return error when waiting for packet to be sent
The client's capture method returns an error channel that according to the docs is intended to be used for checking if a packet was sent successfully whenever that is important. The `...AndWait` methods use this channel, but only to wait. They do not capture the possible error coming from that channel. The changes in this commit suggest to use the error and return it, so that a user may check whether a packet was sent successfully using the top level methods (like `CaptureMessageAndWait`), instead of having to write their own. **Breaking changes**: The signatures of the following methods are changed: ``` - [Client.]CaptureMessageAndWait - [Client.]CaptureErrorAndWait - [Client.]CapturePanicAndWait ``` Work on #84
1 parent d3909e6 commit 3c52672

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

client.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var (
3838
ErrMissingUser = errors.New("raven: dsn missing public key and/or password")
3939
ErrMissingPrivateKey = errors.New("raven: dsn missing private key")
4040
ErrMissingProjectID = errors.New("raven: dsn missing project id")
41+
ErrClientNotConfigured = errors.New("raven: client not configured")
4142
)
4243

4344
type Severity string
@@ -318,7 +319,7 @@ func newTransport() Transport {
318319
} else {
319320
t.Client = &http.Client{
320321
Transport: &http.Transport{
321-
Proxy: http.ProxyFromEnvironment,
322+
Proxy: http.ProxyFromEnvironment,
322323
TLSClientConfig: &tls.Config{RootCAs: rootCAs},
323324
},
324325
}
@@ -608,24 +609,24 @@ func CaptureMessage(message string, tags map[string]string, interfaces ...Interf
608609
}
609610

610611
// CaptureMessageAndWait is identical to CaptureMessage except it blocks and waits for the message to be sent.
611-
func (client *Client) CaptureMessageAndWait(message string, tags map[string]string, interfaces ...Interface) string {
612+
func (client *Client) CaptureMessageAndWait(message string, tags map[string]string, interfaces ...Interface) (error, string) {
612613
if client == nil {
613-
return ""
614+
return ErrClientNotConfigured, ""
614615
}
615616

616617
if client.shouldExcludeErr(message) {
617-
return ""
618+
return nil, ""
618619
}
619620

620621
packet := NewPacket(message, append(append(interfaces, client.context.interfaces()...), &Message{message, nil})...)
621622
eventID, ch := client.Capture(packet, tags)
622-
<-ch
623+
internalError := <-ch
623624

624-
return eventID
625+
return internalError, eventID
625626
}
626627

627628
// CaptureMessageAndWait is identical to CaptureMessage except it blocks and waits for the message to be sent.
628-
func CaptureMessageAndWait(message string, tags map[string]string, interfaces ...Interface) string {
629+
func CaptureMessageAndWait(message string, tags map[string]string, interfaces ...Interface) (error, string) {
629630
return DefaultClient.CaptureMessageAndWait(message, tags, interfaces...)
630631
}
631632

@@ -655,24 +656,24 @@ func CaptureError(err error, tags map[string]string, interfaces ...Interface) st
655656
}
656657

657658
// CaptureErrorAndWait is identical to CaptureError, except it blocks and assures that the event was sent
658-
func (client *Client) CaptureErrorAndWait(err error, tags map[string]string, interfaces ...Interface) string {
659+
func (client *Client) CaptureErrorAndWait(err error, tags map[string]string, interfaces ...Interface) (error, string) {
659660
if client == nil {
660-
return ""
661+
return ErrClientNotConfigured, ""
661662
}
662663

663664
if client.shouldExcludeErr(err.Error()) {
664-
return ""
665+
return nil, ""
665666
}
666667

667668
packet := NewPacket(err.Error(), append(append(interfaces, client.context.interfaces()...), NewException(err, NewStacktrace(1, 3, client.includePaths)))...)
668669
eventID, ch := client.Capture(packet, tags)
669-
<-ch
670+
internalError := <-ch
670671

671-
return eventID
672+
return internalError, eventID
672673
}
673674

674675
// CaptureErrorAndWait is identical to CaptureError, except it blocks and assures that the event was sent
675-
func CaptureErrorAndWait(err error, tags map[string]string, interfaces ...Interface) string {
676+
func CaptureErrorAndWait(err error, tags map[string]string, interfaces ...Interface) (error, string) {
676677
return DefaultClient.CaptureErrorAndWait(err, tags, interfaces...)
677678
}
678679

@@ -716,7 +717,7 @@ func CapturePanic(f func(), tags map[string]string, interfaces ...Interface) (in
716717
}
717718

718719
// CapturePanicAndWait is identical to CaptureError, except it blocks and assures that the event was sent
719-
func (client *Client) CapturePanicAndWait(f func(), tags map[string]string, interfaces ...Interface) (err interface{}, errorID string) {
720+
func (client *Client) CapturePanicAndWait(f func(), tags map[string]string, interfaces ...Interface) (internalError error, err interface{}, errorID string) {
720721
// Note: This doesn't need to check for client, because we still want to go through the defer/recover path
721722
// Down the line, Capture will be noop'd, so while this does a _tiny_ bit of overhead constructing the
722723
// *Packet just to be thrown away, this should not be the normal case. Could be refactored to
@@ -742,15 +743,15 @@ func (client *Client) CapturePanicAndWait(f func(), tags map[string]string, inte
742743

743744
var ch chan error
744745
errorID, ch = client.Capture(packet, tags)
745-
<-ch
746+
internalError = <-ch
746747
}()
747748

748749
f()
749750
return
750751
}
751752

752753
// CapturePanicAndWait is identical to CaptureError, except it blocks and assures that the event was sent
753-
func CapturePanicAndWait(f func(), tags map[string]string, interfaces ...Interface) (interface{}, string) {
754+
func CapturePanicAndWait(f func(), tags map[string]string, interfaces ...Interface) (error, interface{}, string) {
754755
return DefaultClient.CapturePanicAndWait(f, tags, interfaces...)
755756
}
756757

0 commit comments

Comments
 (0)