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

Commit 6913381

Browse files
author
Bjørn
authored
Add context to errors (#29)
This will make understanding errors simpler as we can deduce what the origin was.
1 parent 082c302 commit 6913381

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module github.com/lunarway/snyk_exporter
22

33
require (
4+
github.com/pkg/errors v0.8.1
45
github.com/prometheus/client_golang v0.9.2
56
github.com/prometheus/common v0.0.0-20190107103113-2998b132700a
67
gopkg.in/alecthomas/kingpin.v2 v2.2.6

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5
2323
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
2424
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
2525
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
26+
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
27+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
2628
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2729
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2830
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=

main.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"syscall"
1414
"time"
1515

16+
"github.com/pkg/errors"
1617
"github.com/prometheus/client_golang/prometheus"
1718
"github.com/prometheus/client_golang/prometheus/promhttp"
1819
"github.com/prometheus/common/log"
@@ -155,7 +156,7 @@ func runAPIPolling(done chan error, url, token string, organizationIDs []string,
155156
return nil
156157
})
157158
if err != nil {
158-
done <- err
159+
done <- errors.WithMessagef(err, "organization %s (%s)", organization.Name, organization.ID)
159160
return
160161
}
161162
gaugeResults = append(gaugeResults, results...)
@@ -175,6 +176,7 @@ func runAPIPolling(done chan error, url, token string, organizationIDs []string,
175176
func poll(organization org, collector func(org) error) error {
176177
err := collector(organization)
177178
if err != nil {
179+
err = errors.Cause(err)
178180
httpErr, ok := err.(*url.Error)
179181
if ok {
180182
if httpErr.Timeout() {
@@ -252,7 +254,7 @@ type gaugeResult struct {
252254
func collect(client *client, organization org) ([]gaugeResult, error) {
253255
projects, err := client.getProjects(organization.ID)
254256
if err != nil {
255-
return nil, err
257+
return nil, errors.WithMessage(err, "get projects for organization")
256258
}
257259

258260
var gaugeResults []gaugeResult

0 commit comments

Comments
 (0)