Skip to content

Commit 5161c1d

Browse files
fix err messages
1 parent f371731 commit 5161c1d

2 files changed

Lines changed: 18 additions & 14 deletions

File tree

examples/zone-export.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,23 @@
1515
# to load an alternate configuration file:
1616
# api = NS1(configFile='/etc/ns1/api.json')
1717

18+
# Define the zone to export
19+
zone_name = "example.com"
20+
1821
# Export a zone to BIND format
1922
# The export() method will:
2023
# 1. Initiate the export job
2124
# 2. Poll the status until complete or failed
2225
# 3. Download and return the zone file content
23-
zone = api.loadZone("example.com")
26+
zone = api.loadZone(zone_name)
2427

25-
print("Exporting zone example.com...")
28+
print(f"Exporting zone {zone_name}...")
2629
zone_file = zone.export()
2730
print("Export complete!")
2831
print(zone_file)
2932

3033
# Save to a file
31-
with open("example.com.txt", "w") as f:
34+
output_file = f"{zone_name}.txt"
35+
with open(output_file, "w") as f:
3236
f.write(zone_file)
33-
print("Zone file saved to example.com.txt")
37+
print(f"Zone file saved to {output_file}")

ns1/rest/zones.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#
66

77
from . import resource
8+
from .errors import ResourceException
89

910

1011
class Zones(resource.BaseResource):
@@ -227,8 +228,6 @@ def get_zonefile_export(self, zone, callback=None, errback=None):
227228
# Note: This endpoint returns raw zone file text, not JSON
228229
# The transport layer will try to parse it as JSON and fail
229230
# We catch that exception and extract the raw body text
230-
from ns1.rest.errors import ResourceException
231-
232231
try:
233232
return self._make_request(
234233
"GET",
@@ -237,14 +236,15 @@ def get_zonefile_export(self, zone, callback=None, errback=None):
237236
errback=errback,
238237
)
239238
except ResourceException as e:
240-
# If it's about invalid JSON, that's expected - extract the body
241-
if "invalid json in response" in str(e):
242-
# The body is the third argument in ResourceException
243-
if hasattr(e, "args") and len(e.args) >= 3:
244-
body = e.args[2]
245-
if callback:
246-
return callback(body)
247-
return body
239+
# Check if this is a valid zonefile response (plain text)
240+
# The response should be 200 OK with text/plain content
241+
if e.response and e.response.getcode() == 200:
242+
# Check content-type header for text/plain
243+
content_type = e.response.getheader("Content-Type", "")
244+
if "text/plain" in content_type or "text" in content_type:
245+
# This is the expected plain text zonefile
246+
return e.body if e.body else ""
247+
# Otherwise, this is a real error - re-raise it
248248
raise
249249

250250

0 commit comments

Comments
 (0)