1

I'm having a loop that reads a file line by line and calls to a library. However, sometimes the library makes it own error messages, and then my whole loop stops working because it terminates the loop. Is there a way that I can control for the messages in the library? How can I make my loop continue when receiving this error message (i.e., how do I check whether this error message exist so that I can skip it)?

The error I'm getting:

raise EchoNestAPIError(code, message, headers, http_status)
pyechonest.util.EchoNestAPIError: (u'Echo Nest API Error 5: The identifier specified does not exist [HTTP 200]',)

So this is the part of the code in the library that is handling the error:

class EchoNestAPIError(EchoNestException):
    """
    API Specific Errors.
    """
    def __init__(self, code, message, headers, http_status):
        if http_status:
            http_status_message_part = ' [HTTP %d]' % http_status
        else:
            http_status_message_part = ''
        self.http_status = http_status

        formatted_message = ('Echo Nest API Error %d: %s%s' %
                             (code, message, http_status_message_part),)
        super(EchoNestAPIError, self).__init__(code, formatted_message, headers)


class EchoNestIOError(EchoNestException):
    """
    URL and HTTP errors.
    """
    def __init__(self, code=None, error=None, headers=headers):
        formatted_message = ('Echo Nest IOError: %s' % headers,)
        super(EchoNestIOError, self).__init__(code, formatted_message, headers)

def get_successful_response(raw_json):
    if hasattr(raw_json, 'headers'):
        headers = raw_json.headers
    else:
        headers = {'Headers':'No Headers'}
    if hasattr(raw_json, 'getcode'):
        http_status = raw_json.getcode()
    else:
        http_status = None
    raw_json = raw_json.read()
    try:
        response_dict = json.loads(raw_json)
        status_dict = response_dict['response']['status']
        code = int(status_dict['code'])
        message = status_dict['message']
        if (code != 0):
            # do some cute exception handling
            raise EchoNestAPIError(code, message, headers, http_status)
        del response_dict['response']['status']
        return response_dict
    except ValueError:
        logger.debug(traceback.format_exc())
        raise EchoNestAPIError(-1, "Unknown error.", headers, http_status)

I tried using a general "except" without defining anything, and that works for when I reach the API limitations, but still not for the error that I was asking this question about. This error seems to come from the same class. I don't know why it works on limitations error, but not on the other. Below the error of the API limitation:

raise EchoNestAPIError(code, message, headers, http_status)
pyechonest.util.EchoNestAPIError: (u'Echo Nest API Error 3: 3|You are limited to 120 accesses every minute. You might be eligible for a rate limit increase, go to http://developer.echonest.com/account/upgrade [HTTP 429]',)
2
  • This is what error handlings are made for Commented May 18, 2015 at 19:09
  • Read the exception handling docs Commented May 18, 2015 at 19:10

1 Answer 1

1

You catch the exception in a try/except -block.

Example:

with open(your_file, "r") as f:
    for line in f:
        try:
            api_call(line)
        except pyechonest.util.EchoNestAPIError:
            pass # or continue if you wish to skip processing this line.

Each line of code that is executed inside the try-block is expected to possibly cause an exception, which is then 'caught' in the except-block (there's an additional finally-block, more on that in the docs). The above example simply suppresses the exception, but this might not be the ideal solution.

Exceptions are an essential feature of the language, and you should at least read the official docs to get started.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I kind of tried your solution in a similar fashion, but it won't get pass the error message (also yours is not working). What happens is that it prints out the error and then stops. I don't think the problem is how to skip it, but how to detect the error message that is causing the problem. It doesn't seem to capture it in order to pass or continue the loop (I tried printing some text when in the exception part, but it never prints it). I also tried including things like: "from pyechonest import util" at the top of the file or variations of the except part (eg "util.EchoNestAPIError")
@user40037 impossible to know what the problem is unless you show the offending piece of code.
I added it to my original question :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.