OrestF / readymade

MIT License
9 stars 4 forks source link

Add Readymade error #13

Open mikeKniaz opened 12 months ago

mikeKniaz commented 12 months ago

Add Readymade error inherited from StandardError. It should have an error_response method returning the Response object with failure or validation_fail status.

  def call
    build_record
    build_form
    return response(:validation_fail, errors: record.errors.messages) unless form_valid_and_sync?

    assign_attributes
    prepare_sequence
    return sequence_error_response if sequence_error_response.present?

    calculate_order_price
    return calculate_order_response unless calculate_order_response.success?

    build_hotel_product_items

    success_response
  rescue ReadymadeError => e
    e.error_response # response(:failure, errors: data from error object)
  end
mikeKniaz commented 12 months ago

For example:

class BaseError < StandardError
  attr_reader :data

  def initialize(data)
    super
    @data = data
  end

  def error_response
    BaseResponse.new(:fail, data)
  end
end