|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +// Package mdlerrors provides structured error types for the MDL executor. |
| 4 | +// |
| 5 | +// Typed errors support errors.As for programmatic classification. |
| 6 | +// Sentinel or wrapped errors may also support errors.Is where applicable |
| 7 | +// (for example, ErrExit and BackendError via Unwrap). |
| 8 | +// Every error preserves the original message via Error() for backward-compatible |
| 9 | +// string output — callers that only use %v or .Error() see no change. |
| 10 | +// |
| 11 | +// Only BackendError supports Unwrap — it wraps an underlying storage/IO error. |
| 12 | +// All other error types are leaf errors with no wrapped cause. |
| 13 | +package mdlerrors |
| 14 | + |
| 15 | +import ( |
| 16 | + "errors" |
| 17 | + "fmt" |
| 18 | +) |
| 19 | + |
| 20 | +// ErrExit is a sentinel error indicating clean script/session termination. |
| 21 | +// Use errors.Is(err, ErrExit) to detect exit requests. |
| 22 | +var ErrExit = errors.New("exit") |
| 23 | + |
| 24 | +// NotConnectedError indicates an operation was attempted without an active project connection. |
| 25 | +type NotConnectedError struct { |
| 26 | + // WriteMode is true when write access was required but not available. |
| 27 | + WriteMode bool |
| 28 | + msg string |
| 29 | +} |
| 30 | + |
| 31 | +// NewNotConnected creates a NotConnectedError for read access. |
| 32 | +func NewNotConnected() *NotConnectedError { |
| 33 | + return &NotConnectedError{msg: "not connected to a project"} |
| 34 | +} |
| 35 | + |
| 36 | +// NewNotConnectedWrite creates a NotConnectedError for write access. |
| 37 | +func NewNotConnectedWrite() *NotConnectedError { |
| 38 | + return &NotConnectedError{WriteMode: true, msg: "not connected to a project in write mode"} |
| 39 | +} |
| 40 | + |
| 41 | +func (e *NotConnectedError) Error() string { return e.msg } |
| 42 | + |
| 43 | +// NotFoundError indicates a named element was not found. |
| 44 | +type NotFoundError struct { |
| 45 | + // Kind is the element type (e.g. "entity", "module", "microflow"). |
| 46 | + Kind string |
| 47 | + // Name is the qualified or simple name of the element. |
| 48 | + Name string |
| 49 | + msg string |
| 50 | +} |
| 51 | + |
| 52 | +// NewNotFound creates a NotFoundError. |
| 53 | +func NewNotFound(kind, name string) *NotFoundError { |
| 54 | + return &NotFoundError{ |
| 55 | + Kind: kind, |
| 56 | + Name: name, |
| 57 | + msg: fmt.Sprintf("%s not found: %s", kind, name), |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// NewNotFoundMsg creates a NotFoundError with a custom message. |
| 62 | +func NewNotFoundMsg(kind, name, msg string) *NotFoundError { |
| 63 | + return &NotFoundError{Kind: kind, Name: name, msg: msg} |
| 64 | +} |
| 65 | + |
| 66 | +func (e *NotFoundError) Error() string { return e.msg } |
| 67 | + |
| 68 | +// AlreadyExistsError indicates an element already exists when creating. |
| 69 | +type AlreadyExistsError struct { |
| 70 | + // Kind is the element type. |
| 71 | + Kind string |
| 72 | + // Name is the qualified or simple name. |
| 73 | + Name string |
| 74 | + msg string |
| 75 | +} |
| 76 | + |
| 77 | +// NewAlreadyExists creates an AlreadyExistsError. |
| 78 | +func NewAlreadyExists(kind, name string) *AlreadyExistsError { |
| 79 | + return &AlreadyExistsError{ |
| 80 | + Kind: kind, |
| 81 | + Name: name, |
| 82 | + msg: fmt.Sprintf("%s already exists: %s", kind, name), |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// NewAlreadyExistsMsg creates an AlreadyExistsError with a custom message. |
| 87 | +func NewAlreadyExistsMsg(kind, name, msg string) *AlreadyExistsError { |
| 88 | + return &AlreadyExistsError{Kind: kind, Name: name, msg: msg} |
| 89 | +} |
| 90 | + |
| 91 | +func (e *AlreadyExistsError) Error() string { return e.msg } |
| 92 | + |
| 93 | +// UnsupportedError indicates an unsupported operation, feature, or property. |
| 94 | +type UnsupportedError struct { |
| 95 | + // What holds the full error message describing what is unsupported |
| 96 | + // (e.g. "unsupported attribute type: Binary"). |
| 97 | + What string |
| 98 | + msg string |
| 99 | +} |
| 100 | + |
| 101 | +// NewUnsupported creates an UnsupportedError. |
| 102 | +func NewUnsupported(msg string) *UnsupportedError { |
| 103 | + return &UnsupportedError{What: msg, msg: msg} |
| 104 | +} |
| 105 | + |
| 106 | +func (e *UnsupportedError) Error() string { return e.msg } |
| 107 | + |
| 108 | +// ValidationError indicates invalid input or configuration. |
| 109 | +type ValidationError struct { |
| 110 | + msg string |
| 111 | +} |
| 112 | + |
| 113 | +// NewValidation creates a ValidationError. |
| 114 | +func NewValidation(msg string) *ValidationError { |
| 115 | + return &ValidationError{msg: msg} |
| 116 | +} |
| 117 | + |
| 118 | +// NewValidationf creates a ValidationError with formatted message. |
| 119 | +func NewValidationf(format string, args ...any) *ValidationError { |
| 120 | + return &ValidationError{msg: fmt.Sprintf(format, args...)} |
| 121 | +} |
| 122 | + |
| 123 | +func (e *ValidationError) Error() string { return e.msg } |
| 124 | + |
| 125 | +// BackendError wraps an error from the underlying storage layer (mpr/SDK). |
| 126 | +type BackendError struct { |
| 127 | + // Op describes the operation that failed (e.g. "get domain model", "write entity"). |
| 128 | + Op string |
| 129 | + Err error |
| 130 | +} |
| 131 | + |
| 132 | +// NewBackend creates a BackendError wrapping a cause. |
| 133 | +func NewBackend(op string, err error) *BackendError { |
| 134 | + return &BackendError{Op: op, Err: err} |
| 135 | +} |
| 136 | + |
| 137 | +func (e *BackendError) Error() string { |
| 138 | + if e.Err == nil { |
| 139 | + return fmt.Sprintf("failed to %s", e.Op) |
| 140 | + } |
| 141 | + return fmt.Sprintf("failed to %s: %v", e.Op, e.Err) |
| 142 | +} |
| 143 | + |
| 144 | +func (e *BackendError) Unwrap() error { return e.Err } |
0 commit comments