File tree 4 files changed +143
-0
lines changed
4 files changed +143
-0
lines changed Original file line number Diff line number Diff line change
1
+ package codes
2
+
3
+ // Code for common errors
4
+ type Code uint32
5
+
6
+ // Transport codes used for common errors.
7
+ // For more info, check google.golang.org/grpc/codes
8
+ const (
9
+ // OK ...
10
+ OK Code = iota
11
+ // Canceled ...
12
+ Canceled
13
+ // Unknown ...
14
+ Unknown
15
+ // InvalidArgument ...
16
+ InvalidArgument
17
+ // DeadlineExceeded ...
18
+ DeadlineExceeded
19
+ // NotFound ...
20
+ NotFound
21
+ // AlreadyExists ...
22
+ AlreadyExists
23
+ // PermissionDenied ...
24
+ PermissionDenied
25
+ // ResourceExhausted ...
26
+ ResourceExhausted
27
+ // FailedPrecondition ...
28
+ FailedPrecondition
29
+ // Aborted ...
30
+ Aborted
31
+ // OutOfRange ...
32
+ OutOfRange
33
+ // Unimplemented ...
34
+ Unimplemented
35
+ // Internal ...
36
+ Internal
37
+ // Unavailable ...
38
+ Unavailable
39
+ // DataLoss ...
40
+ DataLoss
41
+ // Unauthenticated ...
42
+ Unauthenticated
43
+ )
Original file line number Diff line number Diff line change
1
+ package sys
2
+
3
+ import (
4
+ "github.com/pkg/errors"
5
+
6
+ "github.com/olezhek28/platform_common/pkg/sys/codes"
7
+ )
8
+
9
+ type commonError struct {
10
+ msg string
11
+ code codes.Code
12
+ }
13
+
14
+ func NewCommonError (msg string , code codes.Code ) * commonError {
15
+ return & commonError {msg , code }
16
+ }
17
+
18
+ func (r * commonError ) Error () string {
19
+ return r .msg
20
+ }
21
+
22
+ func (r * commonError ) Code () codes.Code {
23
+ return r .code
24
+ }
25
+
26
+ func IsCommonError (err error ) bool {
27
+ var ce * commonError
28
+ return errors .As (err , & ce )
29
+ }
30
+
31
+ func GetCommonError (err error ) * commonError {
32
+ var ce * commonError
33
+ if ! errors .As (err , & ce ) {
34
+ return nil
35
+ }
36
+
37
+ return ce
38
+ }
Original file line number Diff line number Diff line change
1
+ package validate
2
+
3
+ import (
4
+ "encoding/json"
5
+
6
+ "github.com/pkg/errors"
7
+ )
8
+
9
+ type ValidationErrors struct {
10
+ Messages []string `json:"error_messages"`
11
+ }
12
+
13
+ func (v * ValidationErrors ) addError (message string ) {
14
+ v .Messages = append (v .Messages , message )
15
+ }
16
+
17
+ func NewValidationErrors (messages ... string ) * ValidationErrors {
18
+ return & ValidationErrors {
19
+ Messages : messages ,
20
+ }
21
+ }
22
+
23
+ func (v * ValidationErrors ) Error () string {
24
+ data , err := json .Marshal (v .Messages )
25
+ if err != nil {
26
+ return err .Error ()
27
+ }
28
+
29
+ return string (data )
30
+ }
31
+
32
+ func IsValidationError (err error ) bool {
33
+ var ve * ValidationErrors
34
+ return errors .As (err , & ve )
35
+ }
Original file line number Diff line number Diff line change
1
+ package validate
2
+
3
+ import "context"
4
+
5
+ type Condition func (ctx context.Context ) error
6
+
7
+ func Validate (ctx context.Context , conds ... Condition ) error {
8
+ ve := NewValidationErrors ()
9
+
10
+ for _ , c := range conds {
11
+ err := c (ctx )
12
+ if err != nil {
13
+ if IsValidationError (err ) {
14
+ ve .addError (err .Error ())
15
+ continue
16
+ }
17
+
18
+ return err
19
+ }
20
+ }
21
+
22
+ if ve .Messages == nil {
23
+ return nil
24
+ }
25
+
26
+ return ve
27
+ }
You can’t perform that action at this time.
0 commit comments