|
| 1 | +package com.flowforge.core.error |
| 2 | + |
| 3 | +import com.flowforge.core.types.Metadata |
| 4 | + |
| 5 | +// =============================== |
| 6 | +// VALIDATION ERROR TYPES |
| 7 | +// =============================== |
| 8 | + |
| 9 | +/** |
| 10 | + * Comprehensive validation error hierarchy. Each error type provides specific context about what |
| 11 | + * went wrong. |
| 12 | + */ |
| 13 | +sealed abstract class ValidationError( |
| 14 | + val field: String, |
| 15 | + val message: String, |
| 16 | + val code: String, |
| 17 | + val context: Metadata = Map.empty |
| 18 | + ) extends Product |
| 19 | + with Serializable { |
| 20 | + |
| 21 | + /** |
| 22 | + * Add context to a validation error. Enables building rich error information. |
| 23 | + */ |
| 24 | + def withContext(key: String, value: String): ValidationError = { |
| 25 | + val newContext = context + (key -> value) |
| 26 | + this match { |
| 27 | + case e: RequiredFieldError => e.copy(context = newContext) |
| 28 | + case e: FormatError => e.copy(context = newContext) |
| 29 | + case e: RangeError => e.copy(context = newContext) |
| 30 | + case e: LengthError => e.copy(context = newContext) |
| 31 | + case e: PatternError => e.copy(context = newContext) |
| 32 | + case e: CustomError => e.copy(context = newContext) |
| 33 | + case e: CrossFieldError => e.copy(context = newContext) |
| 34 | + case e: BusinessRuleError => e.copy(context = newContext) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Convert validation error to a user-friendly message. |
| 40 | + */ |
| 41 | + def toUserMessage: String = s"$field: $message" |
| 42 | + |
| 43 | + /** |
| 44 | + * Convert validation error to a detailed technical message. |
| 45 | + */ |
| 46 | + def toTechnicalMessage: String = |
| 47 | + s"ValidationError(field=$field, code=$code, message=$message, context=$context)" |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Required field is missing or null. |
| 52 | + */ |
| 53 | +case class RequiredFieldError( |
| 54 | + override val field: String, |
| 55 | + override val context: Metadata = Map.empty |
| 56 | + ) extends ValidationError( |
| 57 | + field = field, |
| 58 | + message = s"Field '$field' is required but was not provided", |
| 59 | + code = "REQUIRED_FIELD_MISSING", |
| 60 | + context = context |
| 61 | +) |
| 62 | + |
| 63 | +/** |
| 64 | + * Field value has invalid format. |
| 65 | + */ |
| 66 | +case class FormatError( |
| 67 | + override val field: String, |
| 68 | + expectedFormat: String, |
| 69 | + actualValue: String, |
| 70 | + override val context: Metadata = Map.empty |
| 71 | + ) extends ValidationError( |
| 72 | + field = field, |
| 73 | + message = |
| 74 | + s"Field '$field' has invalid format. Expected: $expectedFormat, got: $actualValue", |
| 75 | + code = "INVALID_FORMAT", |
| 76 | + context = context |
| 77 | +) |
| 78 | + |
| 79 | +/** |
| 80 | + * Field value is outside expected range. |
| 81 | + */ |
| 82 | +case class RangeError( |
| 83 | + override val field: String, |
| 84 | + min: Option[Double], |
| 85 | + max: Option[Double], |
| 86 | + actualValue: Double, |
| 87 | + override val context: Metadata = Map.empty |
| 88 | + ) extends ValidationError( |
| 89 | + field = field, |
| 90 | + message = { |
| 91 | + val bounds = (min, max) match { |
| 92 | + case (Some(minVal), Some(maxVal)) => s"between $minVal and $maxVal" |
| 93 | + case (Some(minVal), None) => s"at least $minVal" |
| 94 | + case (None, Some(maxVal)) => s"at most $maxVal" |
| 95 | + case (None, None) => "within valid range" |
| 96 | + } |
| 97 | + s"Field '$field' must be $bounds, got: $actualValue" |
| 98 | + }, |
| 99 | + code = "VALUE_OUT_OF_RANGE", |
| 100 | + context = context |
| 101 | +) |
| 102 | + |
| 103 | +/** |
| 104 | + * Field value has invalid length. |
| 105 | + */ |
| 106 | +case class LengthError( |
| 107 | + override val field: String, |
| 108 | + minLength: Option[Int], |
| 109 | + maxLength: Option[Int], |
| 110 | + actualLength: Int, |
| 111 | + override val context: Metadata = Map.empty |
| 112 | + ) extends ValidationError( |
| 113 | + field = field, |
| 114 | + message = { |
| 115 | + val bounds = (minLength, maxLength) match { |
| 116 | + case (Some(min), Some(max)) => s"between $min and $max characters" |
| 117 | + case (Some(min), None) => s"at least $min characters" |
| 118 | + case (None, Some(max)) => s"at most $max characters" |
| 119 | + case (None, None) => "valid length" |
| 120 | + } |
| 121 | + s"Field '$field' must be $bounds, got: $actualLength characters" |
| 122 | + }, |
| 123 | + code = "INVALID_LENGTH", |
| 124 | + context = context |
| 125 | +) |
| 126 | + |
| 127 | +/** |
| 128 | + * Field value doesn't match expected pattern. |
| 129 | + */ |
| 130 | +case class PatternError( |
| 131 | + override val field: String, |
| 132 | + pattern: String, |
| 133 | + actualValue: String, |
| 134 | + override val context: Metadata = Map.empty |
| 135 | + ) extends ValidationError( |
| 136 | + field = field, |
| 137 | + message = s"Field '$field' must match pattern '$pattern', got: '$actualValue'", |
| 138 | + code = "PATTERN_MISMATCH", |
| 139 | + context = context |
| 140 | +) |
| 141 | + |
| 142 | +/** |
| 143 | + * Custom validation error. |
| 144 | + */ |
| 145 | +case class CustomError( |
| 146 | + override val field: String, |
| 147 | + override val message: String, |
| 148 | + override val code: String = "CUSTOM_VALIDATION_ERROR", |
| 149 | + override val context: Metadata = Map.empty |
| 150 | + ) extends ValidationError(field, message, code, context) |
| 151 | + |
| 152 | +/** |
| 153 | + * Cross-field validation error. |
| 154 | + */ |
| 155 | +case class CrossFieldError( |
| 156 | + fields: List[String], |
| 157 | + override val message: String, |
| 158 | + override val code: String = "CROSS_FIELD_VALIDATION_ERROR", |
| 159 | + override val context: Metadata = Map.empty |
| 160 | + ) extends ValidationError( |
| 161 | + field = fields.mkString(","), |
| 162 | + message = message, |
| 163 | + code = code, |
| 164 | + context = context |
| 165 | +) |
| 166 | + |
| 167 | +/** |
| 168 | + * Business rule validation error. |
| 169 | + */ |
| 170 | +case class BusinessRuleError( |
| 171 | + rule: String, |
| 172 | + override val field: String, |
| 173 | + override val message: String, |
| 174 | + override val context: Metadata = Map.empty |
| 175 | + ) extends ValidationError( |
| 176 | + field = field, |
| 177 | + message = s"Business rule '$rule' violation: $message", |
| 178 | + code = "BUSINESS_RULE_VIOLATION", |
| 179 | + context = context |
| 180 | +) |
| 181 | + |
| 182 | +/** |
| 183 | + * Decode error. |
| 184 | + */ |
| 185 | +case class DecodeError( |
| 186 | + override val field: String, |
| 187 | + override val context: Metadata = Map.empty |
| 188 | + ) extends ValidationError( |
| 189 | + field = field, |
| 190 | + message = s"Cannot decode '$field'", |
| 191 | + code = "DECODE_ERROR", |
| 192 | + context = context |
| 193 | +) |
| 194 | + |
| 195 | +/** |
| 196 | + * Timeout error. |
| 197 | + */ |
| 198 | +case class TimeoutError( |
| 199 | + override val field: String, |
| 200 | + error: Throwable, |
| 201 | + override val context: Metadata = Map.empty |
| 202 | + ) extends ValidationError( |
| 203 | + field = field, |
| 204 | + message = s"Time out, duration: $field. [${error.getMessage}]", |
| 205 | + code = "TIME_OUT", |
| 206 | + context = context |
| 207 | +) |
| 208 | + |
0 commit comments