cubefs
1/*
2*
3* Copyright 2020 gRPC authors.
4*
5* Licensed under the Apache License, Version 2.0 (the "License");
6* you may not use this file except in compliance with the License.
7* You may obtain a copy of the License at
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
11* Unless required by applicable law or agreed to in writing, software
12* distributed under the License is distributed on an "AS IS" BASIS,
13* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14* See the License for the specific language governing permissions and
15* limitations under the License.
16*
17*/
18
19// Package status implements errors returned by gRPC. These errors are
20// serialized and transmitted on the wire between server and client, and allow
21// for additional data to be transmitted via the Details field in the status
22// proto. gRPC service handlers should return an error created by this
23// package, and gRPC clients should expect a corresponding error to be
24// returned from the RPC call.
25//
26// This package upholds the invariants that a non-nil error may not
27// contain an OK code, and an OK code must result in a nil error.
28package status29
30import (31"errors"32"fmt"33
34"github.com/golang/protobuf/proto"35"github.com/golang/protobuf/ptypes"36spb "google.golang.org/genproto/googleapis/rpc/status"37"google.golang.org/grpc/codes"38)
39
40// Status represents an RPC status code, message, and details. It is immutable
41// and should be created with New, Newf, or FromProto.
42type Status struct {43s *spb.Status44}
45
46// New returns a Status representing c and msg.
47func New(c codes.Code, msg string) *Status {48return &Status{s: &spb.Status{Code: int32(c), Message: msg}}49}
50
51// Newf returns New(c, fmt.Sprintf(format, a...)).
52func Newf(c codes.Code, format string, a ...interface{}) *Status {53return New(c, fmt.Sprintf(format, a...))54}
55
56// FromProto returns a Status representing s.
57func FromProto(s *spb.Status) *Status {58return &Status{s: proto.Clone(s).(*spb.Status)}59}
60
61// Err returns an error representing c and msg. If c is OK, returns nil.
62func Err(c codes.Code, msg string) error {63return New(c, msg).Err()64}
65
66// Errorf returns Error(c, fmt.Sprintf(format, a...)).
67func Errorf(c codes.Code, format string, a ...interface{}) error {68return Err(c, fmt.Sprintf(format, a...))69}
70
71// Code returns the status code contained in s.
72func (s *Status) Code() codes.Code {73if s == nil || s.s == nil {74return codes.OK75}76return codes.Code(s.s.Code)77}
78
79// Message returns the message contained in s.
80func (s *Status) Message() string {81if s == nil || s.s == nil {82return ""83}84return s.s.Message85}
86
87// Proto returns s's status as an spb.Status proto message.
88func (s *Status) Proto() *spb.Status {89if s == nil {90return nil91}92return proto.Clone(s.s).(*spb.Status)93}
94
95// Err returns an immutable error representing s; returns nil if s.Code() is OK.
96func (s *Status) Err() error {97if s.Code() == codes.OK {98return nil99}100return &Error{e: s.Proto()}101}
102
103// WithDetails returns a new status with the provided details messages appended to the status.
104// If any errors are encountered, it returns nil and the first error encountered.
105func (s *Status) WithDetails(details ...proto.Message) (*Status, error) {106if s.Code() == codes.OK {107return nil, errors.New("no error details for status with code OK")108}109// s.Code() != OK implies that s.Proto() != nil.110p := s.Proto()111for _, detail := range details {112any, err := ptypes.MarshalAny(detail)113if err != nil {114return nil, err115}116p.Details = append(p.Details, any)117}118return &Status{s: p}, nil119}
120
121// Details returns a slice of details messages attached to the status.
122// If a detail cannot be decoded, the error is returned in place of the detail.
123func (s *Status) Details() []interface{} {124if s == nil || s.s == nil {125return nil126}127details := make([]interface{}, 0, len(s.s.Details))128for _, any := range s.s.Details {129detail := &ptypes.DynamicAny{}130if err := ptypes.UnmarshalAny(any, detail); err != nil {131details = append(details, err)132continue133}134details = append(details, detail.Message)135}136return details137}
138
139// Error wraps a pointer of a status proto. It implements error and Status,
140// and a nil *Error should never be returned by this package.
141type Error struct {142e *spb.Status143}
144
145func (e *Error) Error() string {146return fmt.Sprintf("rpc error: code = %s desc = %s", codes.Code(e.e.GetCode()), e.e.GetMessage())147}
148
149// GRPCStatus returns the Status represented by se.
150func (e *Error) GRPCStatus() *Status {151return FromProto(e.e)152}
153
154// Is implements future error.Is functionality.
155// A Error is equivalent if the code and message are identical.
156func (e *Error) Is(target error) bool {157tse, ok := target.(*Error)158if !ok {159return false160}161return proto.Equal(e.e, tse.e)162}
163