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

Commit c227fda

Browse files
author
Valentin Krasontovitsch
committed
Add methods that return error when capturing
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 new top level methods, instead of having to write their own. The newly introduced methods follow the naming scheme `Capture$SOMETHINGAndConfirm`, where the returned error is considered confirmation. Work on #84
1 parent 563b81f commit c227fda

File tree

1 file changed

+91
-2
lines changed

1 file changed

+91
-2
lines changed

client.go

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,30 @@ func CaptureMessageAndWait(message string, tags map[string]string, interfaces ..
664664
return DefaultClient.CaptureMessageAndWait(message, tags, interfaces...)
665665
}
666666

667+
// CaptureMessageAndConfirm is identical to CaptureMessage except it blocks and
668+
// waits for the message to be sent and returns an error.
669+
func (client *Client) CaptureMessageAndConfirm(message string, tags map[string]string, interfaces ...Interface) (error, string) {
670+
if client == nil {
671+
return nil, ""
672+
}
673+
674+
if client.shouldExcludeErr(message) {
675+
return nil, ""
676+
}
677+
678+
packet := NewPacket(message, append(append(interfaces, client.context.interfaces()...), &Message{message, nil})...)
679+
eventID, ch := client.Capture(packet, tags)
680+
internalError := <-ch
681+
682+
return internalError, eventID
683+
}
684+
685+
// CaptureMessageAndConfirm is identical to CaptureMessage except it blocks and
686+
// waits for the message to be sent and returns an error.
687+
func CaptureMessageAndConfirm(message string, tags map[string]string, interfaces ...Interface) (error, string) {
688+
return DefaultClient.CaptureMessageAndConfirm(message, tags, interfaces...)
689+
}
690+
667691
// CaptureErrors formats and delivers an error to the Sentry server.
668692
// Adds a stacktrace to the packet, excluding the call to this method.
669693
func (client *Client) CaptureError(err error, tags map[string]string, interfaces ...Interface) string {
@@ -719,6 +743,30 @@ func CaptureErrorAndWait(err error, tags map[string]string, interfaces ...Interf
719743
return DefaultClient.CaptureErrorAndWait(err, tags, interfaces...)
720744
}
721745

746+
// CaptureErrorAndConfirm is identical to CaptureError, except it blocks and
747+
// assures that the event was sent and returns an error
748+
func (client *Client) CaptureErrorAndConfirm(err error, tags map[string]string, interfaces ...Interface) (error, string) {
749+
if client == nil {
750+
return nil, ""
751+
}
752+
753+
if client.shouldExcludeErr(err.Error()) {
754+
return nil, ""
755+
}
756+
757+
packet := NewPacket(err.Error(), append(append(interfaces, client.context.interfaces()...), NewException(err, NewStacktrace(1, 3, client.includePaths)))...)
758+
eventID, ch := client.Capture(packet, tags)
759+
internalError := <-ch
760+
761+
return internalError, eventID
762+
}
763+
764+
// CaptureErrorAndConfirm is identical to CaptureError, except it blocks and
765+
// assures that the event was sent and returns an error
766+
func CaptureErrorAndConfirm(err error, tags map[string]string, interfaces ...Interface) (error, string) {
767+
return DefaultClient.CaptureErrorAndConfirm(err, tags, interfaces...)
768+
}
769+
722770
// CapturePanic calls f and then recovers and reports a panic to the Sentry server if it occurs.
723771
// If an error is captured, both the error and the reported Sentry error ID are returned.
724772
func (client *Client) CapturePanic(f func(), tags map[string]string, interfaces ...Interface) (err interface{}, errorID string) {
@@ -758,7 +806,7 @@ func CapturePanic(f func(), tags map[string]string, interfaces ...Interface) (in
758806
return DefaultClient.CapturePanic(f, tags, interfaces...)
759807
}
760808

761-
// CapturePanicAndWait is identical to CaptureError, except it blocks and assures that the event was sent
809+
// CapturePanicAndWait is identical to CapturePanic, except it blocks and assures that the event was sent
762810
func (client *Client) CapturePanicAndWait(f func(), tags map[string]string, interfaces ...Interface) (err interface{}, errorID string) {
763811
// Note: This doesn't need to check for client, because we still want to go through the defer/recover path
764812
// Down the line, Capture will be noop'd, so while this does a _tiny_ bit of overhead constructing the
@@ -794,11 +842,52 @@ func (client *Client) CapturePanicAndWait(f func(), tags map[string]string, inte
794842
return
795843
}
796844

797-
// CapturePanicAndWait is identical to CaptureError, except it blocks and assures that the event was sent
845+
// CapturePanicAndWait is identical to CapturePanic, except it blocks and assures that the event was sent
798846
func CapturePanicAndWait(f func(), tags map[string]string, interfaces ...Interface) (interface{}, string) {
799847
return DefaultClient.CapturePanicAndWait(f, tags, interfaces...)
800848
}
801849

850+
// CapturePanicAndConfirm is identical to CapturePanic, except it blocks and
851+
// assures that the event was sent and returns an error
852+
func (client *Client) CapturePanicAndConfirm(f func(), tags map[string]string, interfaces ...Interface) (internalError error, err interface{}, errorID string) {
853+
// Note: This doesn't need to check for client, because we still want to go through the defer/recover path
854+
// Down the line, Capture will be noop'd, so while this does a _tiny_ bit of overhead constructing the
855+
// *Packet just to be thrown away, this should not be the normal case. Could be refactored to
856+
// be completely noop though if we cared.
857+
defer func() {
858+
var packet *Packet
859+
err = recover()
860+
switch rval := err.(type) {
861+
case nil:
862+
return
863+
case error:
864+
if client.shouldExcludeErr(rval.Error()) {
865+
return
866+
}
867+
packet = NewPacket(rval.Error(), append(append(interfaces, client.context.interfaces()...), NewException(rval, NewStacktrace(2, 3, client.includePaths)))...)
868+
default:
869+
rvalStr := fmt.Sprint(rval)
870+
if client.shouldExcludeErr(rvalStr) {
871+
return
872+
}
873+
packet = NewPacket(rvalStr, append(append(interfaces, client.context.interfaces()...), NewException(errors.New(rvalStr), NewStacktrace(2, 3, client.includePaths)))...)
874+
}
875+
876+
var ch chan error
877+
errorID, ch = client.Capture(packet, tags)
878+
internalError = <-ch
879+
}()
880+
881+
f()
882+
return
883+
}
884+
885+
// CapturePanicAndConfirm is identical to CapturePanic, except it blocks and
886+
// assures that the event was sent and returns an error
887+
func CapturePanicAndConfirm(f func(), tags map[string]string, interfaces ...Interface) (error, interface{}, string) {
888+
return DefaultClient.CapturePanicAndConfirm(f, tags, interfaces...)
889+
}
890+
802891
func (client *Client) Close() {
803892
close(client.queue)
804893
}

0 commit comments

Comments
 (0)