Skip to content

Commit

Permalink
fix: refactor error reporting code sample
Browse files Browse the repository at this point in the history
align code sample for error reporting with code in other languages.
use an explicit error with the "fixed" message
  • Loading branch information
minherz committed Sep 13, 2023
1 parent 993a616 commit cf5ca01
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions errorreporting/errorreporting_quickstart/main.go
Expand Up @@ -21,8 +21,9 @@ package main

import (
"context"
"errors"
"log"
"net/http"
"os"

"cloud.google.com/go/errorreporting"
)
Expand All @@ -32,30 +33,34 @@ var errorClient *errorreporting.Client
func main() {
ctx := context.Background()

// Sets your Google Cloud Platform project ID.
projectID := "YOUR_PROJECT_ID"
// TODO: Sets your Google Cloud Platform project ID via environment or explicitly
projectID := os.Getenv("GOOGLE_PROJECT_ID")
if projectID == "" {
projectID = "YOUR_PROJECT_ID"
}

var err error
errorClient, err = errorreporting.NewClient(ctx, projectID, errorreporting.Config{
ServiceName: "myservice",
ServiceName: "errorreporting_quickstart",
ServiceVersion: "0.0.0",
OnError: func(err error) {
log.Printf("Could not log error: %v", err)
log.Printf("Could not repport the error: %v", err)
},
})
if err != nil {
log.Fatal(err)
}
defer errorClient.Close()

resp, err := http.Get("not-a-valid-url")
err = errors.New("Something went wrong")
if err != nil {
logAndPrintError(err)
return
}
log.Print(resp.Status)
}

func logAndPrintError(err error) {
// error context is autopopulated
errorClient.Report(errorreporting.Entry{
Error: err,
})
Expand Down

0 comments on commit cf5ca01

Please sign in to comment.