File: grpcstatus_test.go

package info (click to toggle)
golang-github-grpc-ecosystem-go-grpc-prometheus 1.2.0%2Bgit20191002.6af20e3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 264 kB
  • sloc: makefile: 34; sh: 11
file content (34 lines) | stat: -rw-r--r-- 878 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package grpcstatus

import (
	"github.com/stretchr/testify/require"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"testing"
)

// Own implementation of pkg/errors withStack to avoid additional dependency
type wrappedError struct {
	cause error
	msg   string
}

func (w *wrappedError) Error() string { return w.msg + ": " + w.cause.Error() }

func (w *wrappedError) Cause() error { return w.cause }

func TestErrorUnwrapping(t *testing.T) {
	gRPCCode := codes.FailedPrecondition
	gRPCError := status.Errorf(gRPCCode, "Userspace error.")
	expectedGRPCStatus, _ := status.FromError(gRPCError)
	testedErrors := []error{
		gRPCError,
		&wrappedError{cause: gRPCError, msg: "pkg/errors wrapped error: "},
	}

	for _, e := range testedErrors {
		resultingStatus, ok := FromError(e)
		require.True(t, ok)
		require.Equal(t, expectedGRPCStatus, resultingStatus)
	}
}