Module: LcpRuby::CustomFields::Utils
- Defined in:
- lib/lcp_ruby/custom_fields/utils.rb
Class Method Summary collapse
-
.safe_parse_json(raw, fallback: {}, context: nil) ⇒ Object
Parse a JSON string with environment-aware error handling.
-
.safe_to_decimal(value, context: nil) ⇒ BigDecimal?
Convert a value to BigDecimal with environment-aware error handling.
Class Method Details
.safe_parse_json(raw, fallback: {}, context: nil) ⇒ Object
Parse a JSON string with environment-aware error handling. In dev/test: raises on parse error so bad data is immediately visible. In production: logs the error and returns the fallback.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/lcp_ruby/custom_fields/utils.rb', line 12 def self.safe_parse_json(raw, fallback: {}, context: nil) return fallback if raw.blank? JSON.parse(raw) rescue JSON::ParserError => e raise if Rails.env.local? LcpRuby.record_error(e, subsystem: "custom_fields", operation: "parse_json", source: context) fallback end |
.safe_to_decimal(value, context: nil) ⇒ BigDecimal?
Convert a value to BigDecimal with environment-aware error handling. In dev/test: raises on invalid input. In production: logs the error and returns nil.
30 31 32 33 34 35 36 37 |
# File 'lib/lcp_ruby/custom_fields/utils.rb', line 30 def self.safe_to_decimal(value, context: nil) BigDecimal(value.to_s) rescue ArgumentError, TypeError => e raise if Rails.env.local? LcpRuby.record_error(e, subsystem: "custom_fields", operation: "to_decimal", source: context, value: value.inspect) nil end |