просто хочу попросить несколько рекомендаций о том, как обрабатывать эту begin rescue
или обработку ошибок в рельсах.
Можете ли вы порекомендовать или помочь мне, как сделать обработку ошибок на этом?
Я рефакторинг это и положить его в один begin rescue
, но я не знаю, достаточно ли этого? Спасибо
def generate_shipping
packages = []
packages << ActiveShipping::Package.new(20,
[15, 10, 4.5], # 15x10x4.5 inches Height x Width x Length
units: :imperial) # not grams, not centimetres
# FIXME: after error should redirect to workflows
begin
order = Spree::Workorder.find(params[:workorder_id]).order
buyer_information = order.ship_address
rescue StandardError => e
flash[:error] = "Record Not Found"
redirect_to(admin_workorder_product_worksteps_path)
end
origin = ActiveShipping::Location.new(country:'US',
state: 'CA',
city: 'Torrance',
zip: '90501',
phone: '424-558-8726',
address1: '357 Van Ness Way #250',
name: 'dentca-test')
destination = ActiveShipping::Location.new(country: buyer_information.country.iso,
state: buyer_information.state.abbr,
city: buyer_information.city,
zip: buyer_information.zipcode,
phone: buyer_information.phone,
address1: buyer_information.address1,
address2: buyer_information.address2,
name: "#{buyer_information.firstname} #{buyer_information.lastname}")
# this service type should be dynamic and data will be comming from order.shipments.first.shipping_method
# per product will have a different shippment
options = {
# service_type: 'FEDEX GROUND'
service_type: params[:shipping_method]
}
begin
shipment_data_response = @fedex.create_shipment(origin, destination, packages, options)
fedex_rates = @fedex.find_rates(origin, destination, packages)
blob_img = shipment_data_response.params["ProcessShipmentReply"]["CompletedShipmentDetail"]["CompletedPackageDetails"]["Label"]["Parts"]["Image"]
tracking_number = shipment_data_response.params["ProcessShipmentReply"]["CompletedShipmentDetail"]["CompletedPackageDetails"]["TrackingIds"]["TrackingNumber"]
# img = "base64encodedstring"
image = Magick::Image.from_blob(Base64.decode64(blob_img))
path = "./shipping_label/#{tracking_number}.png"
image[0].write(path)
# json format
# render json: {rates: fedex_rates, fedex: @fedex.methods, details:shipment_data_response.params, trackingNum:tracking_number}
# render html img with blob file
respond_to do |format|
format.html { render html:"<img src=data:image/png;base64,#{blob_img}>".html_safe}
end
rescue StandardError => e
flash[:error] = e
redirect_to admin_workorder_path(params[:workorder_id])
end
end
Таким образом, первое начало восстановления не обнаружит никакой ошибки. Я не знаю, почему, и в конце восстановления я добавил return
, чтобы он не запускался / не вызывал Active::Shipping