Package errors is a Google Stackdriver Error Reporting library.

This package is still experimental and subject to change.

See for more information.

To initialize a client, use the NewClient function. Generally you will want to do this on program initialization. The NewClient function takes as arguments a context, the project name, a service name, and a version string. The service name and version string identify the running program, and are included in error reports. The version string can be left empty. NewClient also takes a bool that indicates whether to report errors using Stackdriver Logging, which will result in errors appearing in both the logs and the error dashboard. This is useful if you are already a user of Stackdriver Logging.

import ""
errorsClient, err = errors.NewClient(ctx, projectID, "myservice", "v1.0", true)

The client can recover panics in your program and report them as errors. To use this functionality, defer its Catch method, as you would any other function for recovering panics.

func foo(ctx context.Context, ...) {
  defer errorsClient.Catch(ctx)

Catch writes an error report containing the recovered value and a stack trace to Stackdriver Error Reporting.

There are various options you can add to the call to Catch that modify how panics are handled.

WithMessage and WithMessagef add a custom message after the recovered value, using fmt.Sprint and fmt.Sprintf respectively.

defer errorsClient.Catch(ctx, errors.WithMessagef("x=%d", x))

WithRequest fills in various fields in the error report with information about an http.Request that's being handled.

defer errorsClient.Catch(ctx, errors.WithRequest(httpReq))

By default, after recovering a panic, Catch will panic again with the recovered value. You can turn off this behavior with the Repanic option.

defer errorsClient.Catch(ctx, errors.Repanic(false))

You can also change the default behavior for the client by changing the RepanicDefault field.

errorsClient.RepanicDefault = false

It is also possible to write an error report directly without recovering a panic, using Report or Reportf.

if err != nil {
  errorsClient.Reportf(ctx, r, "unexpected error %v", err)

If you try to write an error report with a nil client, or if the client fails to write the report to the server, the error report is logged using log.Println.

Imported only in test by 4 package(s)


Imports 6 package(s)