-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.go
More file actions
97 lines (87 loc) · 3.21 KB
/
errors.go
File metadata and controls
97 lines (87 loc) · 3.21 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
// Package jsonlogic2sql exports error types for programmatic error handling.
package jsonlogic2sql
import (
"errors"
tperrors "github.com/h22rana/jsonlogic2sql/internal/errors"
)
// TranspileError is the structured error type returned by transpilation operations.
// Use errors.As to extract this type for programmatic error handling.
//
// Example:
//
// sql, err := transpiler.Transpile(jsonLogic)
// if err != nil {
// var tpErr *TranspileError
// if errors.As(err, &tpErr) {
// fmt.Printf("Error code: %s\n", tpErr.Code)
// fmt.Printf("Path: %s\n", tpErr.Path)
// fmt.Printf("Operator: %s\n", tpErr.Operator)
// }
// }
type TranspileError = tperrors.TranspileError
// ErrorCode represents a specific error condition.
// Codes are organized by category:
// - E001-E099: Structural/validation errors
// - E100-E199: Operator-specific errors
// - E200-E299: Type/schema errors
// - E300-E399: Argument errors
type ErrorCode = tperrors.ErrorCode
// Error codes for programmatic error handling.
const (
// Structural/validation errors (E001-E099).
ErrInvalidExpression = tperrors.ErrInvalidExpression
ErrEmptyArray = tperrors.ErrEmptyArray
ErrMultipleKeys = tperrors.ErrMultipleKeys
ErrPrimitiveNotAllowed = tperrors.ErrPrimitiveNotAllowed
ErrArrayNotAllowed = tperrors.ErrArrayNotAllowed
ErrValidation = tperrors.ErrValidation
ErrInvalidJSON = tperrors.ErrInvalidJSON
// Operator-specific errors (E100-E199).
ErrUnsupportedOperator = tperrors.ErrUnsupportedOperator
ErrOperatorRequiresArray = tperrors.ErrOperatorRequiresArray
ErrCustomOperatorFailed = tperrors.ErrCustomOperatorFailed
// Type/schema errors (E200-E299).
ErrTypeMismatch = tperrors.ErrTypeMismatch
ErrFieldNotInSchema = tperrors.ErrFieldNotInSchema
ErrInvalidFieldType = tperrors.ErrInvalidFieldType
ErrInvalidEnumValue = tperrors.ErrInvalidEnumValue
// Argument errors (E300-E399).
ErrInsufficientArgs = tperrors.ErrInsufficientArgs
ErrTooManyArgs = tperrors.ErrTooManyArgs
ErrInvalidArgument = tperrors.ErrInvalidArgument
ErrInvalidArgType = tperrors.ErrInvalidArgType
ErrInvalidDefaultValue = tperrors.ErrInvalidDefaultValue
ErrUnreferencedPlaceholder = tperrors.ErrUnreferencedPlaceholder
)
// AsTranspileError attempts to extract a TranspileError from an error.
// Returns the TranspileError and true if the error is or wraps a TranspileError,
// otherwise returns nil and false.
//
// Example:
//
// sql, err := transpiler.Transpile(jsonLogic)
// if tpErr, ok := jsonlogic2sql.AsTranspileError(err); ok {
// fmt.Printf("Error code: %s\n", tpErr.Code)
// }
func AsTranspileError(err error) (*TranspileError, bool) {
var tpErr *TranspileError
if errors.As(err, &tpErr) {
return tpErr, true
}
return nil, false
}
// IsErrorCode checks if an error has a specific error code.
// Returns true if the error is or wraps a TranspileError with the given code.
//
// Example:
//
// if jsonlogic2sql.IsErrorCode(err, jsonlogic2sql.ErrUnsupportedOperator) {
// fmt.Println("Unknown operator used")
// }
func IsErrorCode(err error, code ErrorCode) bool {
tpErr, ok := AsTranspileError(err)
if !ok {
return false
}
return tpErr.Code == code
}