<@U04S8PLJWMT> how can i handle errors with in my ...
# ask-ai
o
@kapa.ai how can i handle errors with in my custom Python connector stream
k
To handle errors within your custom Python connector stream in Airbyte, you should implement error handling logic in your stream classes, especially in methods that interact with external APIs (such as
parse_response
). Here’s how you can approach this: 1. Implement Error Handling in `parse_response`: - Check the API response for error codes or messages. - Raise a user-friendly exception if an error is detected. For example, if you detect authentication errors, you can raise an
AirbyteTracedException
with a clear message for the user and an internal message for debugging.
Copy code
python    from airbyte_cdk.utils.traced_exception import AirbyteTracedException, FailureType     def parse_response(self, response: requests.Response, **kwargs) -> Iterable[Mapping]:        response_json = response.json()        # Example: handle authentication errors        if response_json.get("error") in (1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018):            internal_message = f"Unauthorized credentials. Response: {response_json}"            external_message = "Cannot get metadata with unauthorized credentials. Try to re-authenticate in source settings."            raise AirbyteTracedException(                message=external_message,                internal_message=internal_message,                failure_type=FailureType.config_error            )        elif self._data_field:            yield from response_json[self._data_field]        else:            yield from response_json
The
external_message
is shown to the user, while the
internal_message
is logged for troubleshooting purposes. This approach helps surface clear, actionable errors to users and maintainers alike. 2. Disable Legacy Availability Strategies (if not needed): - If you want to handle stream availability yourself, you can disable the default availability strategy by returning
None
from the
availability_strategy
property.
Copy code
python    from airbyte_cdk.sources.streams.availability_strategy import AvailabilityStrategy     @property    def availability_strategy(self) -> Optional[AvailabilityStrategy]:        return None
3. Check Connection Logic: - In your
check_connection
method, attempt to read a record from the stream. If it fails, return a failure message.
Copy code
python    def check_connection(self, logger, config) -> Tuple[bool, any]:        first_stream = next(iter(self.streams(config)))        stream_slice = next(iter(first_stream.stream_slices(sync_mode=SyncMode.full_refresh)))        try:            read_stream = first_stream.read_records(sync_mode=SyncMode.full_refresh, stream_slice=stream_slice)            first_record = None            while not first_record:                first_record = next(read_stream)                if isinstance(first_record, AirbyteMessage):                    if first_record.type == "RECORD":                        first_record = first_record.record                        return True, None                    else:                        first_record = None            return True, None        except Exception as e:            return False, f"Unable to connect to the API with the provided credentials - {str(e)}"
This ensures that connection checks fail gracefully and informatively if the connector cannot access the data source. For more details and code examples, see the official Airbyte documentation on [Check and error handling](https://docs.airbyte.com/platform/next/connector-development/tutorials/custom-python-connector/check-and-error-handling)<source>https://docs.airbyte.com/platform/next/connector-development/tutorials/custom-python-connector/check-and-error-handling</source>.
o
@kapa.ai i get 404 error before getting a response how can i handle it?
k
A message from kapa.ai
If you are getting a 404 error in your custom Python connector and want to handle it gracefully, you can do so by customizing your error handling logic. In Airbyte, you can handle HTTP status codes like 404 in your stream’s error handling, either by ignoring the error, retrying, or failing the sync, depending on your use case. For config-based connectors (YAML): You can configure the error handler in your YAML file to ignore or retry 404 errors. For example, to ignore 404 errors:
Copy code
yaml requester:   <...>   error_handler:     response_filters:       - http_codes: [404]         action: IGNORE
Or, to retry on 404 errors:
Copy code
yaml requester:   <...>   error_handler:     response_filters:       - http_codes: [404]         action: RETRY
You can also combine multiple filters for different codes or error messages as needed. This approach is documented in the [error handling section](https://docs.airbyte.com/platform/next/connector-development/config-based/understanding-the-yaml-file/error-handling)<source>https://docs.airbyte.com/platform/next/connector-development/config-based/understanding-the-yaml-file/error-handling</source>. For custom Python connectors: You should check the response status code in your stream logic (e.g., in
parse_response
or wherever you handle HTTP responses). For example:
Copy code
python def parse_response(self, response: requests.Response, **kwargs) -> Iterable[Mapping]:     if response.status_code == 404:         # Decide how to handle: ignore, log, or raise a custom exception         return []     # Handle other status codes or normal processing     ...
This will allow your connector to skip over 404 errors without failing the entire sync. If you want to log or handle it differently, you can add custom logic as needed. Summary: - In YAML: use
response_filters
with
http_codes: [404]
and set
action
to
IGNORE
or
RETRY
. - In Python: check for
response.status_code == 404
and handle accordingly in your code. For more details and examples, see the [Airbyte error handling documentation](https://docs.airbyte.com/platform/next/connector-development/config-based/understanding-the-yaml-file/error-handling)<source>https://docs.airbyte.com/platform/next/connector-development/config-based/understanding-the-yaml-file/error-handling</source>.