File: err.go

package info (click to toggle)
tiup 1.16.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,384 kB
  • sloc: sh: 1,988; makefile: 138; sql: 16
file content (71 lines) | stat: -rw-r--r-- 2,041 bytes parent folder | download
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package meta

import (
	"fmt"
	"reflect"
)

var (
	// ErrValidate is an empty ValidateErr object, useful for type checking
	ErrValidate = &ValidateErr{}
)

// error types
const (
	TypeConflict = "conflict"
	TypeMismatch = "mismatch"
)

// ValidateErr is the error when meta validation fails with conflicts
type ValidateErr struct {
	Type   string // conflict type
	Target string // conflict Target
	Value  any    // conflict Value
	LHS    string // object 1
	RHS    string // object 2
}

// Error implements the error interface
func (e *ValidateErr) Error() string {
	return fmt.Sprintf("%s %s for '%v' between '%s' and '%s'", e.Target, e.Type, e.Value, e.LHS, e.RHS)
}

// Unwrap implements the error interface
func (e *ValidateErr) Unwrap() error { return nil }

// Is implements the error interface
func (e *ValidateErr) Is(target error) bool {
	t, ok := target.(*ValidateErr)
	if !ok {
		return false
	}

	// check for interface Value separately
	if e.Value != nil && t.Value != nil &&
		(!reflect.ValueOf(e.Value).IsValid() && !reflect.ValueOf(t.Value).IsValid()) {
		return false
	}
	// not supporting non-comparable values for now
	if e.Value != nil && t.Value != nil &&
		!(reflect.TypeOf(e.Value).Comparable() && reflect.TypeOf(t.Value).Comparable()) {
		return false
	}
	return (e.Type == t.Type || t.Type == "") &&
		(e.Target == t.Target || t.Target == "") &&
		(e.Value == t.Value || t.Value == nil || reflect.ValueOf(t.Value).IsZero()) &&
		(e.LHS == t.LHS || t.LHS == "") &&
		(e.RHS == t.RHS || t.RHS == "")
}