Page 1 of 1

API Internal Server Error

Posted: March 24th, 2012, 6:44 pm
by faint545
I'm currently building an application that interfaces with SABNzbd. I've used the API before and now, when I try to use the API to delete history items, the following error gets produced...

Code: Select all

Traceback (most recent call last):  File "/usr/share/sabnzbd/cherrypy/_cprequest.py", line 618, in respond    cherrypy.response.body = self.handler()  File "/usr/share/sabnzbd/cherrypy/_cpdispatch.py", line 25, in __call__    return self.callable(*self.args, **self.kwargs)  File "/usr/share/sabnzbd/sabnzbd/interface.py", line 398, in api    return api_handler(kwargs)  File "/usr/share/sabnzbd/sabnzbd/api.py", line 91, in api_handler    response = _api_table.get(mode, _api_undefined)(name, output, kwargs)  File "/usr/share/sabnzbd/sabnzbd/api.py", line 416, in _api_history    del_hist_job(job, del_files)  File "/usr/share/sabnzbd/sabnzbd/api.py", line 1398, in del_hist_job    path = history_db.get_path(job)  File "/usr/share/sabnzbd/sabnzbd/database.py", line 290, in get_path    return self.c.fetchone().get('path')AttributeError: 'NoneType' object has no attribute 'get'
I'm not familiar at all with the code for SABNzbd so if someone could point me in the direction of what this error means exactly, it would be greatly appreciated. From what this says, it seems like history_db.get_path(job) is returning null (maybe?) but I am not too sure why.

Re: API Internal Server Error

Posted: March 24th, 2012, 6:53 pm
by sander
What is the exact api command you use?

Re: API Internal Server Error

Posted: March 24th, 2012, 7:34 pm
by faint545
I use.. [...]/api?apikey=[key]&mode=history&name=delete&value=[nzo_ids]

Re: API Internal Server Error

Posted: March 25th, 2012, 1:45 am
by sander
Interesting: using the example from http://wiki.sabnzbd.org/api#toc42 I get the same error message as you:

Code: Select all

sander@R540:~$ lynx --dump 'http://localhost:8080/api?mode=history&name=delete&value=SABnzbd_nzo_zt2syz,SABnzbd_nzo_df2hyd,SABnzbd_nzo_op3shf'
500 Internal Server Error

   The server encountered an unexpected condition which prevented it from
   fulfilling the request.
Traceback (most recent call last):
  File "/home/sander/SABnzbd-0.7.0Alpha2/cherrypy/_cprequest.py", line 618, in r
espond
    cherrypy.response.body = self.handler()
  File "/home/sander/SABnzbd-0.7.0Alpha2/cherrypy/_cpdispatch.py", line 25, in _
_call__
    return self.callable(*self.args, **self.kwargs)
  File "/home/sander/SABnzbd-0.7.0Alpha2/sabnzbd/interface.py", line 403, in api
    return api_handler(kwargs)
  File "/home/sander/SABnzbd-0.7.0Alpha2/sabnzbd/api.py", line 99, in api_handle
r
    response = _api_table.get(mode, _api_undefined)(name, output, kwargs)
  File "/home/sander/SABnzbd-0.7.0Alpha2/sabnzbd/api.py", line 429, in _api_hist
ory
    del_hist_job(job, del_files)
  File "/home/sander/SABnzbd-0.7.0Alpha2/sabnzbd/api.py", line 1452, in del_hist
_job
    path = history_db.get_path(job)
  File "/home/sander/SABnzbd-0.7.0Alpha2/sabnzbd/database.py", line 291, in get_
path
    return self.c.fetchone().get('path')
AttributeError: 'NoneType' object has no attribute 'get'

   Powered by [1]CherryPy 3.2.0

References

   1. http://www.cherrypy.org/
sander@R540:~$ 
Another API command just works:

Code: Select all

sander@R540:~$ lynx --dump 'http://localhost:8080/api?mode=history&output=xml' | tail -4
   /home/sander/SABnzbd-0.7.0Alpha2/interfaces/Config/templates False
   False True http://wiki.sabnzbd.org/ 10m 53.4 G 39.8 G /home/sander 12.2
   G 0.7.0Alpha2 /home/sander/.sabnzbd gold False Idle 0 False None 0 B
   1000.0 G 0 B 0.00 8.08 8.08 0:00:00 True 0 unknown 0.00
sander@R540:~$
Ah, I think I found the reason:

Code: Select all

sander@R540:~$ lynx --dump 'http://localhost:8080/api?mode=history&name=delete&value=SABnzbd_nzo_U2ooDd,SABnzbd_nzo__CGnOP' 
ok

sander@R540:~$
These are (were) existing nzo_id's. Running it again (with the nzo_id's already deleted), gives the error message again:

Code: Select all

sander@R540:~$ lynx --dump 'http://localhost:8080/api?mode=history&name=delete&value=SABnzbd_nzo_U2ooDd,SABnzbd_nzo__CGnOP' 
500 Internal Server Error

   The server encountered an unexpected condition which prevented it from
   fulfilling the request.
Traceback (most recent call last):
  File "/home/sander/oude-SABnzbd's/SABnzbd-0.6.1/cherrypy/_cprequest.py", line
618, in respond
    cherrypy.response.body = self.handler()
  File "/home/sander/oude-SABnzbd's/SABnzbd-0.6.1/cherrypy/_cpdispatch.py", line
 25, in __call__
    return self.callable(*self.args, **self.kwargs)
  File "/home/sander/oude-SABnzbd's/SABnzbd-0.6.1/sabnzbd/interface.py", line 39
6, in api
    return api_handler(kwargs)
  File "/home/sander/oude-SABnzbd's/SABnzbd-0.6.1/sabnzbd/api.py", line 83, in a
pi_handler
    response = _api_table.get(mode, _api_undefined)(name, output, kwargs)
  File "/home/sander/oude-SABnzbd's/SABnzbd-0.6.1/sabnzbd/api.py", line 406, in
_api_history
    del_hist_job(job, del_files)
  File "/home/sander/oude-SABnzbd's/SABnzbd-0.6.1/sabnzbd/api.py", line 1355, in
 del_hist_job
    path = history_db.get_path(job)
  File "/home/sander/oude-SABnzbd's/SABnzbd-0.6.1/sabnzbd/database.py", line 282
, in get_path
    return self.c.fetchone().get('path')
AttributeError: 'NoneType' object has no attribute 'get'

   Powered by [1]CherryPy 3.2.0

References

   1. http://www.cherrypy.org/
sander@R540:~$ 

So:
1) use existing nzo_id's and it works
2) use non-existing nzo_id's and SAB gives an exception. I guess that's a bug.

Re: API Internal Server Error

Posted: March 25th, 2012, 1:54 am
by sander
As a proof of concept, I've put a try-except combo in the sabnzbd/api.py source code (see below), and now SAB does not give an error with a non-existing nzo_id:

Code: Select all

sander@R540:~$ lynx --dump 'http://localhost:8080/api?mode=history&name=delete&value=SABnzbd_nzo_blablalbla' 
ok

sander@R540:~$ 
I'll ask shypike for a real solution.

Code: Select all

#------------------------------------------------------------------------------
def del_hist_job(job, del_files):
    """ Remove history element """
    if job:
        path = PostProcessor.do.get_path(job)
        if path:
            PostProcessor.do.delete(job, del_files=del_files)
        else:
            history_db = cherrypy.thread_data.history_db
	    try:
		    path = history_db.get_path(job)
		    PostProcessor.do.delete(job, del_files=del_files)
		    history_db.remove_history(job)
	    except:
	            pass

        if path and del_files and path.lower().startswith(cfg.download_dir.get_path().lower()):
            remove_all(path, recursive=True)
    return True

#------------------------------------------------------------------------------


Re: API Internal Server Error

Posted: March 25th, 2012, 5:09 am
by shypike
It will be fixed in 0.7.0
(Is now fixed in source.)

Re: API Internal Server Error

Posted: March 25th, 2012, 9:30 am
by faint545
Thank you!