-
Notifications
You must be signed in to change notification settings - Fork 79
/
errors.go
43 lines (33 loc) · 870 Bytes
/
errors.go
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
35
36
37
38
39
40
41
42
43
package teresa_errors
import (
"fmt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var ErrInternalServerError = status.Errorf(codes.Unknown, "Internal Server Error")
type GrpcError interface {
Grpc() error
}
type Error interface {
GrpcError
Error() string
}
type serverError struct {
err error
grpc error
}
func (s *serverError) Error() string { return fmt.Sprintf("%s: %s", s.grpc.Error(), s.err.Error()) }
func (s *serverError) Grpc() error { return s.grpc }
// Get returns a gRPC error if err is a GrpcError, otherwise, returns the low level error
func Get(err error) error {
if grpc, ok := err.(GrpcError); ok {
return grpc.Grpc()
}
return err
}
func New(grpcErr, err error) Error {
return &serverError{grpc: grpcErr, err: err}
}
func NewInternalServerError(err error) Error {
return New(ErrInternalServerError, err)
}