mgmonteleone / python-atlasapi

python package for MongoDB Atlas Cloud provider
Apache License 2.0
21 stars 22 forks source link

400 errors do not return enough useful information to debug. #183

Closed mgmonteleone closed 1 year ago

mgmonteleone commented 1 year ago

This needs to ensure that the full error message from the API is returned,

Otherwise it is very difficult to debug 400 errors.

class ErrAtlasBadRequest(ErrAtlasGeneric):
    """Atlas : Bad Request
    Constructor
    Args:
        c (int): HTTP code
        details (dict): Response payload
    """

    def __init__(self, c, details):

        if details.get('errorCode', None) == 'DUPLICATE_CLUSTER_NAME':
            raise (ErrAtlasDuplicateClusterName(c, details))
        if details.get('errorCode', None) == 'RESOURCE_NOT_FOUND_FOR_JOB':
            raise (ErrAtlasJobError(c, details))
        if details.get('errorCode', None) == 'CANNOT_CANCEL_AUTOMATED_RESTORE':
            raise (ErrAtlasBackupError(c,details))
        if details.get('errorCode', None) in ['ATLAS_MAINTENANCE_ALREADY_SCHEDULED',
                                              'ATLAS_NUM_MAINTENANCE_DEFERRALS_EXCEEDED']:
            raise (ErrMaintenanceError(c,details))
        else:
            logger.critical(f"A generic error was raised")
            logger.critical(details)

        super().__init__("Something was wrong with the client request.", c, details)
mgmonteleone commented 1 year ago

Hmmm, in testing I do get back details...

Test:

 def test_00_handle_400s(self):
        """
        Force a 400 error so we can test details being passed through.
        """

        cluster = self.a.Clusters.get_single_cluster(cluster=self.TEST_CLUSTER_NAME)
        update = self.a.Clusters.modify_cluster(cluster=self.TEST_CLUSTER_NAME,cluster_config=dict(backupEnabled="whatevs"))
        pprint(update)
Error_Handler: CRITICAL: A generic error was raised
Error_Handler: CRITICAL: {'detail': 'An invalid enumeration value whatevs was specified.', 'error': 400, 'errorCode': 'INVALID_ENUM_VALUE', 'parameters': ['whatevs'], 'reason': 'Bad Request'}

So this should show up in logs....

But perhaps should pass the details all the way up?

mgmonteleone commented 1 year ago

The getAtlasResponse method of the error object will return the full error information from the API.

That said, I will go ahead and submit a PR to include more of the origin error information in the base error.