Skip to content

Commit 0b0d34a

Browse files
committed
handle bad gateway errors with custom exception
1 parent ae12042 commit 0b0d34a

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

fmrest/exceptions.py

+8
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,13 @@ class FileMakerError(FMRestException):
5757
def __init__(self, error_code: Optional[int], error_message: str) -> None:
5858
super().__init__('FileMaker Server returned error {}, {}'.format(error_code, error_message))
5959

60+
61+
class BadGatewayError(FMRestException):
62+
"""Exception for Bad Gateway errors from FMS's web server.
63+
Usually, this happens when the web server is running, but the Data API is
64+
not enabled or not responding.
65+
"""
66+
67+
6068
class RecordError(FMRestException):
6169
"""Error with the local Record instance."""

fmrest/server.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
filename_from_url, PlaceholderDict)
1010
from .const import (PORTAL_PREFIX, FMSErrorCode, API_VERSIONS, API_DATE_FORMATS, API_PATH_PREFIX,
1111
API_PATH)
12-
from .exceptions import BadJSON, FileMakerError, RecordError
12+
from .exceptions import BadJSON, FileMakerError, RecordError, BadGatewayError
1313
from .record import Record
1414
from .foundset import Foundset
1515

@@ -132,7 +132,8 @@ def __enter__(self) -> 'Server':
132132
return self
133133

134134
def __exit__(self, exc_type, exc_val, exc_traceback) -> None:
135-
self.logout()
135+
if self._token:
136+
self.logout()
136137

137138
def __repr__(self) -> str:
138139
return '<Server logged_in={} database={} layout={}>'.format(
@@ -921,6 +922,10 @@ def _call_filemaker(self, method: str, path: str,
921922
proxies=self.proxies,
922923
**kwargs)
923924

925+
if response.status_code == 502:
926+
raise BadGatewayError("Web server responded with a Bad Gateway "
927+
"error. Check if the Data API is enabled "
928+
"and responding.")
924929
try:
925930
response_data = response.json()
926931
except json.decoder.JSONDecodeError as ex:

0 commit comments

Comments
 (0)