talos

Форк
0
45 строк · 1.1 Кб
1
// This Source Code Form is subject to the terms of the Mozilla Public
2
// License, v. 2.0. If a copy of the MPL was not distributed with this
3
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4

5
package client
6

7
import (
8
	"errors"
9

10
	"google.golang.org/grpc/codes"
11
	"google.golang.org/grpc/status"
12
)
13

14
// Status returns the status if it is a Status error, nil otherwise.
15
func Status(err error) *status.Status {
16
	type grpcStatus interface {
17
		GRPCStatus() *status.Status
18
	}
19

20
	// Don't use FromError to avoid allocation of OK status.
21
	var st grpcStatus
22

23
	if errors.As(err, &st) {
24
		return st.GRPCStatus()
25
	}
26

27
	return nil
28
}
29

30
// StatusCode returns the Code of the error if it is a Status error, codes.OK if err
31
// is nil, or codes.Unknown otherwise correctly unwrapping wrapped errors.
32
//
33
// StatusCode is mostly equivalent to grpc `status.Code` method, but it correctly unwraps wrapped errors
34
// including `multierror.Error` used when parsing multi-node responses.
35
func StatusCode(err error) codes.Code {
36
	if err == nil {
37
		return codes.OK
38
	}
39

40
	if st := Status(err); st != nil {
41
		return st.Code()
42
	}
43

44
	return codes.Unknown
45
}
46

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.