|
5 | 5 | # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
6 | 6 |
|
7 | 7 | import base64 |
| 8 | +import logging |
8 | 9 | from io import BytesIO |
9 | 10 |
|
10 | 11 | from PIL import Image |
11 | 12 |
|
12 | 13 | from odoo import fields, models |
| 14 | +from odoo.exceptions import UserError |
13 | 15 |
|
14 | 16 | from .ups_request import UpsRequest |
15 | 17 |
|
| 18 | +_logger = logging.getLogger(__name__) |
16 | 19 |
|
17 | 20 | class DeliveryCarrier(models.Model): |
18 | 21 | _inherit = "delivery.carrier" |
@@ -121,16 +124,27 @@ def _ups_get_response_price(self, total_charges, currency, company): |
121 | 124 |
|
122 | 125 | def ups_rate_shipment(self, order): |
123 | 126 | ups_request = UpsRequest(self) |
124 | | - response = ups_request.rate_shipment(order) |
125 | | - price = self._ups_get_response_price( |
126 | | - response, order.currency_id, order.company_id |
127 | | - ) |
128 | | - return { |
129 | | - "success": True, |
130 | | - "price": price, |
131 | | - "error_message": False, |
132 | | - "warning_message": False, |
133 | | - } |
| 127 | + try: |
| 128 | + response = ups_request.rate_shipment(order) |
| 129 | + price = self._ups_get_response_price( |
| 130 | + response, order.currency_id, order.company_id |
| 131 | + ) |
| 132 | + return { |
| 133 | + "success": True, |
| 134 | + "price": price, |
| 135 | + "error_message": False, |
| 136 | + "warning_message": False, |
| 137 | + } |
| 138 | + except UserError as e: |
| 139 | + # During rate shopping (checkout), return failure instead of |
| 140 | + # raising thus gracefully hide unavailable shipping methods. |
| 141 | + _logger.debug("UPS rate shipment failed: %s", e) |
| 142 | + return { |
| 143 | + "success": False, |
| 144 | + "price": 0.0, |
| 145 | + "error_message": str(e), |
| 146 | + "warning_message": False, |
| 147 | + } |
134 | 148 |
|
135 | 149 | def ups_create_shipping(self, picking): |
136 | 150 | """Send packages of the picking to UPS |
|
0 commit comments