return vs raise

158 views
Skip to first unread message

m25...@gmail.com

unread,
Dec 10, 2020, 7:57:00 AM12/10/20
to web.py
Dear friends:
I hope everyone on this group is fine and in a good health, following is a snippet from a code of training REST project i am working on:

def POST(self):
task = json.loads(web.data())
if not task or not task['title']:
raise web.HTTPError('400 Bad Resource!', data=json.dumps({
'error': 'Bad Resource!'
}))
else:
new_task = {
'title': task['title'],
'description': task.get('description', ''),
'done': False
}
db.insert('tasks', **new_task)
#web.created()
raise web.Created(data=json.dumps({
'task': new_task
}))
the question is regarding the behavior web types (HTTPError, Created) when using return vs raise.
when i use raise for example as in the red line i get the required result :

201 Created
Headers
Cache-Control: no-cache 
Content-Type: application/json 
Transfer-Encoding: chunked 
Date: Thu, 10 Dec 2020 12:43:09 GMT
Server: localhost
{ "task": { "title": "Learn Webpy", "description": "", "done": false } }  

but when i use return i get:
201 Created
Headers
Cache-Control: no-cache 
Content-Type: application/json 
Transfer-Encoding: chunked 
Date: Thu, 10 Dec 2020 12:43:09 GMT
Server: localhost
201 created   

i did not understand this behavior , same thing regarding the debate previously discussed on the group regarding return web.seeother vs raise web.seeother.

is this issue concerning webpy or else is it problem with my code, for give am just an amateur and for give me if my language is not clear.

Thank you very much
Reply all
Reply to author
Forward
0 new messages