This repository was archived by the owner on Sep 12, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +41
-1
lines changed
Expand file tree Collapse file tree 2 files changed +41
-1
lines changed Original file line number Diff line number Diff line change 1+ from datetime import date
2+ import os
3+
4+ from duffel_api import Duffel
5+ from duffel_api .http_client import ApiError
6+
7+
8+ if __name__ == "__main__" :
9+ print ("Duffel Flights API - Python Example on handling errors" )
10+
11+ os .environ ["DUFFEL_ACCESS_TOKEN" ] = "some-invalid-token-to-trigger-an-error"
12+
13+ client = Duffel ()
14+ departure_date = date .today ().replace (date .today ().year + 1 )
15+ slices = [
16+ {
17+ "origin" : "LHR" ,
18+ "destination" : "STN" ,
19+ "departure_date" : departure_date .strftime ("%Y-%m-%d" ),
20+ },
21+ ]
22+ try :
23+ offer_request = (
24+ client .offer_requests .create ()
25+ .passengers (
26+ [{"type" : "adult" }, {"age" : 1 }, {"age" : (date .today ().year - 2003 )}]
27+ )
28+ .slices (slices )
29+ .execute ()
30+ )
31+ except ApiError as exc :
32+ # This is super useful when contacting Duffel support
33+ print (f"Request ID: { exc .meta ['request_id' ]} " )
34+ print (f"Status Code: { exc .meta ['status' ]} " )
35+ print ("Errors: " )
36+ for error in exc .errors :
37+ print (f" Title: { error ['title' ]} " )
38+ print (f" Code: { error ['code' ]} " )
39+ print (f" Message: { error ['message' ]} " )
Original file line number Diff line number Diff line change @@ -27,5 +27,6 @@ def test_http_client_error(requests_mock):
2727 client = HttpClient ("some_token" , "http://someaddress" , "v1" )
2828 with pytest .raises (
2929 ApiError , match = "The airline responded with an unexpected error"
30- ):
30+ ) as excinfo :
3131 client .do_get ("/api/stuff" )
32+ assert excinfo .value .meta ["request_id" ] == "FmXeZifDA60QOlgAAODB"
You can’t perform that action at this time.
0 commit comments