Module: LcpRuby::CustomFields::Utils

Defined in:
lib/lcp_ruby/custom_fields/utils.rb

Class Method Summary collapse

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.

Parameters:

  • raw (String)

    JSON string to parse

  • fallback (Object) (defaults to: {})

    value to return on failure in production

  • context (String, nil) (defaults to: nil)

    description for log message (e.g., “project#custom_data (id: 42)”)

Returns:

  • (Object)

    parsed JSON or 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.

Parameters:

  • value (Object)

    value to convert

  • context (String, nil) (defaults to: nil)

    description for log message

Returns:

  • (BigDecimal, 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