catching wrong exception type

Bug #1655784 reported by Jin Li
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Gluon
Fix Released
Undecided
Jin Li

Bug Description

In gluon/gluon/particleGenerator.py, line 42, the function get_table_class() catches the ValueError and that is wrong.

There is only line of code inside the try statement: self.db_models.get(api_name)[table_name].

self.db_models is a dictionary, self.db_models.get(api_name)return None if api_name is not a valid key, otherwise it will return another dictionary.

If api_name is not a valid key, calling self.db_models.get(api_name)[table_name] will throw TypeError: 'NoneType' object has no attribute '__getitem__'

if api_name is valid, but table_name is not a valid key, calling self.db_models.get(api_name)[table_name] will throw KeyError.

here is some definitions of the error types from https://docs.python.org/2/library/exceptions.html

exception ValueError
Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value, and the situation is not described by a more precise exception such as IndexError.

exception KeyError
Raised when a mapping (dictionary) key is not found in the set of existing keys.

exception TypeError
Raised when an operation or function is applied to an object of inappropriate type. The associated value is a string giving details about the type mismatch.

Jin Li (jl7351)
Changed in python-gluon:
assignee: nobody → Jin Li (jl7351)
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to gluon (master)

Fix proposed to branch: master
Review: https://review.openstack.org/419206

Changed in python-gluon:
status: New → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to gluon (master)

Reviewed: https://review.openstack.org/419206
Committed: https://git.openstack.org/cgit/openstack/gluon/commit/?id=6e34beb89a98aa58ec61bd4c89ae9a3dabd70f8f
Submitter: Jenkins
Branch: master

commit 6e34beb89a98aa58ec61bd4c89ae9a3dabd70f8f
Author: Jin Li <email address hidden>
Date: Wed Jan 11 15:53:28 2017 -0800

    Catch the right type of exceptions

    The function get_table_class() catches the ValueError exception.
    Change it to catch TypeError and KeyError because these two are
    the only possible exceptions threw inside the function.

    test case of this change is covered in:
    https://review.openstack.org/#/c/419210/

    Change-Id: I4b28d9bcee1dcff9f91b3535ea8dc07750e28abb
    Closes-Bug: #1655784

Changed in python-gluon:
status: In Progress → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.