Niel Niel - 11 months ago 71
Python Question

Handle Pyramid HTTP error messages in Javascript

I'm sure there must be an easy solution for this, but I have been unable to find it. I'm using Python Pyramid in my server and handling requests/responses with AngularJS in Javascript. I'm using the Pyramid HTTP errors to handle some cases, and I'm able to catch them using the Angular


But this is where I'm stuck, I want to do something with the error messages but if I log the response I just see html code.

In Pyramid this is happening:

if exists:
# continue execution
return HTTPNotFound("The object does not exist")

And in Angular:

.success(function(data) {
// do something
// do something with the error message

How would I get that specific error message from the HTTP error? (I'm using JSON as my renderer btw)

Answer Source

Your render is not considered if you do not return a value for renderer but return a response or exception - so do not expect json on the output unless your error handler supports that.

You can do something like this instead:

return HTTPUnprocessableEntity(body=json.dumps(yourobj/string))

And then you can retrieve the JSON in your error handler and use angular.fromJson() to read it.