Skip to content

Commit 55e7885

Browse files
committed
Refactor error handling in marker scope validation by introducing custom error types for invalid schema types, type constraints, and element constraints. Update validation functions to return these new error types for improved clarity and maintainability.
Signed-off-by: nayuta-ai <nayuta723@gmail.com>
1 parent d5096f1 commit 55e7885

File tree

3 files changed

+69
-12
lines changed

3 files changed

+69
-12
lines changed

pkg/analysis/markerscope/analyzer.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ const (
3636
name = "markerscope"
3737
)
3838

39-
// TODO: SuggestFix.
4039
func init() {
4140
// Register all markers we want to validate scope for
4241
defaults := DefaultMarkerRules()
@@ -285,7 +284,7 @@ func (a *analyzer) validateFieldTypeConstraint(pass *analysis.Pass, field *ast.F
285284
if rule.StrictTypeConstraint && rule.Scope == AnyScope {
286285
namedType, ok := tv.Type.(*types.Named)
287286
if ok {
288-
return fmt.Errorf("%w of %s instead of the field", errMarkerShouldBeOnTypeDefinition, namedType.Obj().Name())
287+
return &MarkerShouldBeOnTypeDefinitionError{TypeName: namedType.Obj().Name()}
289288
}
290289
}
291290

@@ -317,7 +316,7 @@ func validateTypeAgainstConstraint(t types.Type, tc *TypeConstraint, allowDanger
317316
if !allowDangerousTypes && schemaType == SchemaTypeNumber {
318317
// Get the underlying type for better error messages
319318
underlyingType := getUnderlyingType(t)
320-
return fmt.Errorf("type %s is dangerous and not allowed (set allowDangerousTypes to true to permit)", underlyingType.String())
319+
return &DengerousTypeError{Type: underlyingType.String()}
321320
}
322321

323322
if tc == nil {
@@ -327,7 +326,7 @@ func validateTypeAgainstConstraint(t types.Type, tc *TypeConstraint, allowDanger
327326
// Check if the schema type is allowed
328327
if len(tc.AllowedSchemaTypes) > 0 {
329328
if !slices.Contains(tc.AllowedSchemaTypes, schemaType) {
330-
return fmt.Errorf("type %s is not allowed (expected one of: %v)", schemaType, tc.AllowedSchemaTypes)
329+
return &TypeNotAllowedError{Type: schemaType, AllowedTypes: tc.AllowedSchemaTypes}
331330
}
332331
}
333332

@@ -336,7 +335,7 @@ func validateTypeAgainstConstraint(t types.Type, tc *TypeConstraint, allowDanger
336335
elemType := getElementType(t)
337336
if elemType != nil {
338337
if err := validateTypeAgainstConstraint(elemType, tc.ElementConstraint, allowDangerousTypes); err != nil {
339-
return fmt.Errorf("array element: %w", err)
338+
return &InvalidElementConstraintError{Err: err}
340339
}
341340
}
342341
}

pkg/analysis/markerscope/errors.go

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,67 @@ limitations under the License.
1515
*/
1616
package markerscope
1717

18-
import "errors"
18+
import (
19+
"errors"
20+
"fmt"
21+
)
1922

2023
var (
21-
errScopeNonZero = errors.New("scope must be non-zero")
22-
errInvalidScopeBits = errors.New("invalid scope bits")
23-
errInvalidSchemaType = errors.New("invalid schema type")
24-
errMarkerShouldBeOnTypeDefinition = errors.New("marker should be declared on the type definition")
24+
errScopeNonZero = errors.New("scope must be non-zero")
25+
errInvalidScopeBits = errors.New("invalid scope bits")
2526
)
27+
28+
// InvalidSchemaTypeError represents an error when a schema type is invalid.
29+
type InvalidSchemaTypeError struct {
30+
SchemaType string
31+
}
32+
33+
func (e *InvalidSchemaTypeError) Error() string {
34+
return fmt.Sprintf("invalid schema type: %q", e.SchemaType)
35+
}
36+
37+
// InvalidTypeConstraintError represents an error when a type constraint is invalid.
38+
type InvalidTypeConstraintError struct {
39+
Err error
40+
}
41+
42+
func (e *InvalidTypeConstraintError) Error() string {
43+
return fmt.Sprintf("invalid type constraint: %v", e.Err)
44+
}
45+
46+
// InvalidElementConstraintError represents an error when an element constraint is invalid.
47+
type InvalidElementConstraintError struct {
48+
Err error
49+
}
50+
51+
func (e *InvalidElementConstraintError) Error() string {
52+
return fmt.Sprintf("array element: %v", e.Err)
53+
}
54+
55+
// MarkerShouldBeOnTypeDefinitionError represents an error when a marker should be declared on the type definition.
56+
type MarkerShouldBeOnTypeDefinitionError struct {
57+
TypeName string
58+
}
59+
60+
func (e *MarkerShouldBeOnTypeDefinitionError) Error() string {
61+
return fmt.Sprintf("marker should be declared on the type definition of %s instead of the field", e.TypeName)
62+
}
63+
64+
// DengerousTypeError represents an error when a dangerous type is used.
65+
type DengerousTypeError struct {
66+
Type string
67+
}
68+
69+
func (e *DengerousTypeError) Error() string {
70+
return fmt.Sprintf("type %s is dangerous and not allowed (set allowDangerousTypes to true to permit)", e.Type)
71+
}
72+
73+
// TypeNotAllowedError represents an error when a type is not allowed.
74+
type TypeNotAllowedError struct {
75+
Type SchemaType
76+
AllowedTypes []SchemaType
77+
}
78+
79+
func (e *TypeNotAllowedError) Error() string {
80+
return fmt.Sprintf("type %s is not allowed (expected one of: %v)", e.Type, e.AllowedTypes)
81+
}

pkg/analysis/markerscope/initializer.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ func validateMarkerRule(rule MarkerScopeRule) error {
8282
// Validate type constraint if present
8383
if rule.TypeConstraint != nil {
8484
if err := validateTypeConstraint(rule.TypeConstraint); err != nil {
85-
return fmt.Errorf("invalid type constraint: %w", err)
85+
return &InvalidTypeConstraintError{
86+
Err: err,
87+
}
8688
}
8789
}
8890

@@ -97,7 +99,7 @@ func validateTypeConstraint(tc *TypeConstraint) error {
9799
// Validate schema types if specified
98100
for _, st := range tc.AllowedSchemaTypes {
99101
if !isValidSchemaType(st) {
100-
return fmt.Errorf("%w: %q", errInvalidSchemaType, st)
102+
return &InvalidSchemaTypeError{SchemaType: string(st)}
101103
}
102104
}
103105

0 commit comments

Comments
 (0)