Bug 974893 - by_name() methods should convert NoResultFound to something more meaningful
Summary: by_name() methods should convert NoResultFound to something more meaningful
Keywords:
Status: CLOSED WONTFIX
Alias: None
Product: Beaker
Classification: Retired
Component: web UI
Version: 0.12
Hardware: Unspecified
OS: Unspecified
unspecified
unspecified
Target Milestone: ---
Assignee: beaker-dev-list
QA Contact: tools-bugs
URL:
Whiteboard:
Depends On: 978225
Blocks:
TreeView+ depends on / blocked
 
Reported: 2013-06-17 04:09 UTC by Nick Coghlan
Modified: 2020-06-02 11:37 UTC (History)
5 users (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2020-06-02 11:35:03 UTC
Embargoed:


Attachments (Terms of Use)

Description Nick Coghlan 2013-06-17 04:09:53 UTC
Description of problem:

The server error logs are full of entries like:

Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/bkr/server/xmlrpccontroller.py", line 54, in RPC2
    response = self.process_rpc(method,params)
  File "/usr/lib/python2.6/site-packages/bkr/server/xmlrpccontroller.py", line 43, in process_rpc
    response = obj(*params)
  File "/usr/lib/python2.6/site-packages/bkr/server/tasks.py", line 532, in to_dict
    return Task.by_name(name, valid).to_dict()
  File "/usr/lib/python2.6/site-packages/bkr/server/model.py", line 6317, in by_name
    return query.one()
  File "/usr/lib64/python2.6/site-packages/sqlalchemy/orm/query.py", line 1684, in one
    raise orm_exc.NoResultFound("No row was found for one()")
NoResultFound: No row was found for one()


How reproducible:

Always

Steps to Reproduce:
1. Call the Task API to_dict() XML-RPC method with an unrecognised task name
2.
3.

Actual results:

The raw SQL Alchemy NoResultFound error appears in the logs

Expected results:

A more suitable exception like "Unknown task: <name>" is reported.

Additional info:

This should be done for all of the by_name() methods in the data model, not just Task.by_name.

Comment 1 Nick Coghlan 2013-06-26 06:58:03 UTC
Using an appropriate custom exception should also make it possible to handle these in a relatively generic fashion in the web UI layer (converting them to a 200 OK response with a suitable error message rather than 500 error).

Comment 2 Raymond Mancy 2014-02-17 07:42:26 UTC
Is there any reason why any lookup method (i.e by_id()) not be included in this?

Comment 3 Nick Coghlan 2014-02-17 07:45:01 UTC
I happened to notice lots of "by_name" instances in the logs, but yes, it would probably be applicable to other lookup mechanisms as well (since we do expose lookups by ID in a few places)

Comment 4 Amit Saha 2014-05-06 03:41:35 UTC
We catch the NoResultException in some of these queries and raise a ValueError with a friendly error message. 

Is the proposed fix run *all* such queries in a try..catch block and have it raise a custom exception with a friendly message?

For eg.

try:
   SomeClass.by_name('something')
except NoResultFound:
   raise MyCustomClass('Something bad happened')

Comment 5 Dan Callaghan 2014-05-06 22:48:28 UTC
(In reply to Amit Saha from comment #4)
> We catch the NoResultException in some of these queries and raise a
> ValueError with a friendly error message. 
> 
> Is the proposed fix run *all* such queries in a try..catch block and have it
> raise a custom exception with a friendly message?
> 
> For eg.
> 
> try:
>    SomeClass.by_name('something')
> except NoResultFound:
>    raise MyCustomClass('Something bad happened')

Yes except it would be

try:
    return cls.query.filter(cls.name == name).one()
except NoResultFound:
    raise DatabaseLookupError('No such blah %r' % name)

That is, the try-except-raise should be in the by_name method itself (and this applies to all the other by_* lookup methods, in spite of the bug title).

Ray implemented this in one of his patches a while back but it got bogged down in deciding what the exception type and exception message should be, so we abandoned it.

Comment 6 matt jia 2014-05-09 05:07:39 UTC
I think for the exception type ,we could borrow some ideas from openstack.

http://docs.openstack.org/developer/python-openstackclient/api/openstackclient.common.exceptions.html

Cheers, Matt Jia

Comment 7 wangdong 2015-04-21 06:11:54 UTC
If I give a non-existent pool name in cli, it will return "No row was found for one()"  See below.

[wangdong@localhost tmp]$ bkr system-modify beaker-test-vm1 --pool-policy PoolBaa
/usr/lib/python2.7/site-packages/requests/packages/urllib3/connection.py:251: SecurityWarning: Certificate has no `subjectAltName`, falling back to check for a `commonName` for now. This feature is being removed by major browsers and deprecated by RFC 2818. (See https://github.com/shazow/urllib3/issues/497 for details.)
  SecurityWarning
HTTP error: 400 Client Error: BAD REQUEST
No row was found for one()


I think it should tell user "There isn't a pool named <PoolBaa>"

Comment 8 Martin Styk 2020-06-02 11:35:03 UTC
Hello,

thank you for opening issue in Beaker project.
This issue was marked with component "web ui".
As we are not planning to address any further issues in current UI, due to technical stack and not being able to work with Python 3 codebase, I'm closing this issue as WONTFIX.
New UI will be reimplemented within new versions of Beaker.

If you have any questions feel free to reach out to me.

Best regards,
Martin <martin.styk>


Note You need to log in before you can comment on or make changes to this bug.