forked from TheThingsNetwork/lorawan-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
98 lines (79 loc) · 5.19 KB
/
errors.go
File metadata and controls
98 lines (79 loc) · 5.19 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package lorawan
import (
"go.thethings.network/lorawan-stack/v3/pkg/errors"
)
var (
errDecode = errors.Define("decode", "could not decode `{lorawan_field}`")
errEncode = errors.Define("encode", "could not encode `{lorawan_field}`")
errEncodedFieldLengthBound = errors.DefineInvalidArgument("encoded_field_length_bound", "`{lorawan_field}` encoded length `{value}` should between `{min}` and `{max}`", valueKey)
errEncodedFieldLengthEqual = errors.DefineInvalidArgument("encoded_field_length_equal", "`{lorawan_field}` encoded length `{value}` should be `{expected}`", valueKey)
errEncodedFieldLengthTwoChoices = errors.DefineInvalidArgument("encoded_field_length_multiple_choices", "`{lorawan_field}` encoded length `{value}` should be equal to `{expected_1}` or `{expected_2}`", valueKey)
errFieldBound = errors.DefineInvalidArgument("field_bound", "`{lorawan_field}` should be between `{min}` and `{max}`", valueKey)
errFieldHasMax = errors.DefineInvalidArgument("field_with_max", "`{lorawan_field}` value `{value}` should be lower or equal to `{max}`", valueKey)
errFieldLengthEqual = errors.DefineInvalidArgument("field_length_equal", "`{lorawan_field}` length `{value}` should be equal to `{expected}`", valueKey)
errFieldLengthHasMax = errors.DefineInvalidArgument("field_length_with_max", "`{lorawan_field}` length `{value}` should be lower or equal to `{max}`", valueKey)
errFieldLengthHasMin = errors.DefineInvalidArgument("field_length_with_min", "`{lorawan_field}` length `{value}` should be higher or equal to `{min}`", valueKey)
errFieldLengthTwoChoices = errors.DefineInvalidArgument("field_length_with_multiple_choices", "`{lorawan_field}` length `{value}` should be equal to `{expected_1}` or `{expected_2}`", valueKey)
errUnknownField = errors.DefineInvalidArgument("unknown_field", "unknown `{lorawan_field}`", valueKey)
)
const valueKey = "value"
type valueErr func(interface{}) errors.Error
func unexpectedValue(err interface {
WithAttributes(kv ...interface{}) errors.Error
}) valueErr {
return func(value interface{}) errors.Error {
return err.WithAttributes(valueKey, value)
}
}
func errExpectedLowerOrEqual(lorawanField string, max interface{}) valueErr {
return unexpectedValue(errFieldHasMax.WithAttributes("lorawan_field", lorawanField, "max", max))
}
func errExpectedLengthEqual(lorawanField string, expected interface{}) valueErr {
return unexpectedValue(errFieldLengthEqual.WithAttributes("lorawan_field", lorawanField, "expected", expected))
}
func errExpectedLengthLowerOrEqual(lorawanField string, max interface{}) valueErr {
return unexpectedValue(errFieldLengthHasMax.WithAttributes("lorawan_field", lorawanField, "max", max))
}
func errExpectedLengthHigherOrEqual(lorawanField string, min interface{}) valueErr {
return unexpectedValue(errFieldLengthHasMin.WithAttributes("lorawan_field", lorawanField, "min", min))
}
func errExpectedLengthTwoChoices(lorawanField string, expected1, expected2 interface{}) valueErr {
return unexpectedValue(errFieldLengthTwoChoices.WithAttributes("lorawan_field", lorawanField, "expected_1", expected1, "expected_2", expected2))
}
func errExpectedLengthEncodedBound(lorawanField string, min, max interface{}) valueErr {
return unexpectedValue(errEncodedFieldLengthBound.WithAttributes("lorawan_field", lorawanField, "min", min, "max", max))
}
func errExpectedLengthEncodedEqual(lorawanField string, expected interface{}) valueErr {
return unexpectedValue(errEncodedFieldLengthEqual.WithAttributes("lorawan_field", lorawanField, "expected", expected))
}
func errExpectedLengthEncodedTwoChoices(lorawanField string, expected1, expected2 interface{}) valueErr {
return unexpectedValue(errEncodedFieldLengthTwoChoices.WithAttributes("lorawan_field", lorawanField, "expected_1", expected1, "expected_2", expected2))
}
func errFailedEncoding(lorawanField string) errors.Error {
return errEncode.WithAttributes("lorawan_field", lorawanField)
}
func errFailedDecoding(lorawanField string) errors.Error {
return errDecode.WithAttributes("lorawan_field", lorawanField)
}
func errUnknown(lorawanField string) valueErr {
return unexpectedValue(errUnknownField.WithAttributes("lorawan_field", lorawanField))
}
func errMissing(lorawanField string) errors.Error {
return errUnknownField.WithAttributes("lorawan_field", lorawanField)
}
func errExpectedBetween(lorawanField string, min, max interface{}) valueErr {
return unexpectedValue(errFieldBound.WithAttributes("lorawan_field", lorawanField, "min", min, "max", max))
}