Discussion:
customize abort to use JSON
Daniel Watrous
2013-03-08 04:34:42 UTC
Permalink
I'm using abort when to return non-successful status to clients. I would
like to override the default HTML output that is generated and instead have
it produce JSON.

Is this possible? How would I go about doing this?
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.

---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Daniel Watrous
2013-03-09 00:31:37 UTC
Permalink
In bottle.py, ERROR_PAGE_TEMPLATE is defined and used as default for all
errors.

This can be easily overridden

@error(404)def error404(error):
return 'Nothing here, sorry'


The problem with this is that I can't figure out how to include the message
provided when I called abort. It will simply always print "Nothing here,
sorry"

It will also have a content type of text/html

I'm interested in having the flexibility to return a JSON document.

Daniel
Post by Daniel Watrous
I'm using abort when to return non-successful status to clients. I would
like to override the default HTML output that is generated and instead have
it produce JSON.
Is this possible? How would I go about doing this?
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.

---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Ian Epperson
2013-03-09 00:47:22 UTC
Permalink
I believe the proper syntax is:

@error(404)
def error404(error):
return "Nothing here, sorry: %s" % error.output
Post by Daniel Watrous
In bottle.py, ERROR_PAGE_TEMPLATE is defined and used as default for all
errors.
This can be easily overridden
return 'Nothing here, sorry'
The problem with this is that I can't figure out how to include the
message provided when I called abort. It will simply always print "Nothing
here, sorry"
It will also have a content type of text/html
I'm interested in having the flexibility to return a JSON document.
Daniel
Post by Daniel Watrous
I'm using abort when to return non-successful status to clients. I would
like to override the default HTML output that is generated and instead have
it produce JSON.
Is this possible? How would I go about doing this?
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.
---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
This email is intended for the use of the individual addressee(s) named
above and may contain information that is confidential, privileged or
unsuitable for overly sensitive persons with low self-esteem, no sense of
humor or irrational religious beliefs. If you are not the intended
recipient, any dissemination, distribution or copying of this email is not
authorized (either explicitly or implicitly) and constitutes an irritating
social faux pas. Unless the word absquatulation has been used in its
correct context somewhere other than in this warning, it does not have any
legal or grammatical use and may be ignored. No animals were harmed in the
transmission of this email, although the yorkshire terrier next door is
living on borrowed time, let me tell you. Those of you with an overwhelming
fear of the unknown will be gratified to learn that there is no hidden
message revealed by reading this warning backwards, so just ignore that
Alert Notice from Microsoft: However, by pouring a complete circle of salt
around yourself and your computer you can ensure that no harm befalls you
and your pets. If you have received this email in error, please add some
nutmeg and egg whites and place it in a warm oven for 40 minutes. Whisk
briefly and let it stand for 2 hours before icing.
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.

---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Daniel Watrous
2013-03-09 21:18:06 UTC
Permalink
Ian,

That does allow me to customize the message into a string that is valid
JSON. Is there some way to also override the Content-type to be
application/json rather than text/html?

Daniel
Post by Daniel Watrous
@error(404)
return "Nothing here, sorry: %s" % error.output
Post by Daniel Watrous
In bottle.py, ERROR_PAGE_TEMPLATE is defined and used as default for all
errors.
This can be easily overridden
return 'Nothing here, sorry'
The problem with this is that I can't figure out how to include the
message provided when I called abort. It will simply always print "Nothing
here, sorry"
It will also have a content type of text/html
I'm interested in having the flexibility to return a JSON document.
Daniel
Post by Daniel Watrous
I'm using abort when to return non-successful status to clients. I would
like to override the default HTML output that is generated and instead have
it produce JSON.
Is this possible? How would I go about doing this?
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.
---
You received this message because you are subscribed to the Google Groups
"bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
This email is intended for the use of the individual addressee(s) named
above and may contain information that is confidential, privileged or
unsuitable for overly sensitive persons with low self-esteem, no sense of
humor or irrational religious beliefs. If you are not the intended
recipient, any dissemination, distribution or copying of this email is not
authorized (either explicitly or implicitly) and constitutes an irritating
social faux pas. Unless the word absquatulation has been used in its
correct context somewhere other than in this warning, it does not have any
legal or grammatical use and may be ignored. No animals were harmed in the
transmission of this email, although the yorkshire terrier next door is
living on borrowed time, let me tell you. Those of you with an overwhelming
fear of the unknown will be gratified to learn that there is no hidden
message revealed by reading this warning backwards, so just ignore that
Alert Notice from Microsoft: However, by pouring a complete circle of salt
around yourself and your computer you can ensure that no harm befalls you
and your pets. If you have received this email in error, please add some
nutmeg and egg whites and place it in a warm oven for 40 minutes. Whisk
briefly and let it stand for 2 hours before icing.
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.

---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Iuri
2013-03-09 21:23:58 UTC
Permalink
Yeap.

@error(404)
def error404(error):
response.content_type = 'application/json'
return "Nothing here, sorry: %s" % error.output
Post by Daniel Watrous
Ian,
That does allow me to customize the message into a string that is valid
JSON. Is there some way to also override the Content-type to be
application/json rather than text/html?
Daniel
Post by Daniel Watrous
@error(404)
return "Nothing here, sorry: %s" % error.output
Post by Daniel Watrous
In bottle.py, ERROR_PAGE_TEMPLATE is defined and used as default for all
errors.
This can be easily overridden
return 'Nothing here, sorry'
The problem with this is that I can't figure out how to include the
message provided when I called abort. It will simply always print "Nothing
here, sorry"
It will also have a content type of text/html
I'm interested in having the flexibility to return a JSON document.
Daniel
Post by Daniel Watrous
I'm using abort when to return non-successful status to clients. I
would like to override the default HTML output that is generated and
instead have it produce JSON.
Is this possible? How would I go about doing this?
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/**bottlepy<http://groups.google.de/group/bottlepy>for mailing list options.
See http://bottlepy.org/ for news and documentation.
---
You received this message because you are subscribed to the Google
Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
.
--
This email is intended for the use of the individual addressee(s) named
above and may contain information that is confidential, privileged or
unsuitable for overly sensitive persons with low self-esteem, no sense of
humor or irrational religious beliefs. If you are not the intended
recipient, any dissemination, distribution or copying of this email is not
authorized (either explicitly or implicitly) and constitutes an irritating
social faux pas. Unless the word absquatulation has been used in its
correct context somewhere other than in this warning, it does not have any
legal or grammatical use and may be ignored. No animals were harmed in the
transmission of this email, although the yorkshire terrier next door is
living on borrowed time, let me tell you. Those of you with an overwhelming
fear of the unknown will be gratified to learn that there is no hidden
message revealed by reading this warning backwards, so just ignore that
Alert Notice from Microsoft: However, by pouring a complete circle of salt
around yourself and your computer you can ensure that no harm befalls you
and your pets. If you have received this email in error, please add some
nutmeg and egg whites and place it in a warm oven for 40 minutes. Whisk
briefly and let it stand for 2 hours before icing.
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.
---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.

---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Daniel Watrous
2013-03-10 05:09:05 UTC
Permalink
Works like a charm. Thank you.
Post by Daniel Watrous
Yeap.
@error(404)
response.content_type = 'application/json'
return "Nothing here, sorry: %s" % error.output
Post by Daniel Watrous
Ian,
That does allow me to customize the message into a string that is valid
JSON. Is there some way to also override the Content-type to be
application/json rather than text/html?
Daniel
Post by Daniel Watrous
@error(404)
return "Nothing here, sorry: %s" % error.output
Post by Daniel Watrous
In bottle.py, ERROR_PAGE_TEMPLATE is defined and used as default for
all errors.
This can be easily overridden
return 'Nothing here, sorry'
The problem with this is that I can't figure out how to include the
message provided when I called abort. It will simply always print "Nothing
here, sorry"
It will also have a content type of text/html
I'm interested in having the flexibility to return a JSON document.
Daniel
Post by Daniel Watrous
I'm using abort when to return non-successful status to clients. I
would like to override the default HTML output that is generated and
instead have it produce JSON.
Is this possible? How would I go about doing this?
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/**bottlepy<http://groups.google.de/group/bottlepy>for mailing list options.
See http://bottlepy.org/ for news and documentation.
---
You received this message because you are subscribed to the Google
Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
.
--
This email is intended for the use of the individual addressee(s) named
above and may contain information that is confidential, privileged or
unsuitable for overly sensitive persons with low self-esteem, no sense of
humor or irrational religious beliefs. If you are not the intended
recipient, any dissemination, distribution or copying of this email is not
authorized (either explicitly or implicitly) and constitutes an irritating
social faux pas. Unless the word absquatulation has been used in its
correct context somewhere other than in this warning, it does not have any
legal or grammatical use and may be ignored. No animals were harmed in the
transmission of this email, although the yorkshire terrier next door is
living on borrowed time, let me tell you. Those of you with an overwhelming
fear of the unknown will be gratified to learn that there is no hidden
message revealed by reading this warning backwards, so just ignore that
Alert Notice from Microsoft: However, by pouring a complete circle of salt
around yourself and your computer you can ensure that no harm befalls you
and your pets. If you have received this email in error, please add some
nutmeg and egg whites and place it in a warm oven for 40 minutes. Whisk
briefly and let it stand for 2 hours before icing.
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.
---
You received this message because you are subscribed to the Google Groups
"bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.

---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Daniel Watrous
2014-10-14 20:21:52 UTC
Permalink
This has since changed to
"error.body"
Post by Daniel Watrous
Works like a charm. Thank you.
Post by Daniel Watrous
Yeap.
@error(404)
response.content_type = 'application/json'
return "Nothing here, sorry: %s" % error.output
Post by Daniel Watrous
Ian,
That does allow me to customize the message into a string that is valid
JSON. Is there some way to also override the Content-type to be
application/json rather than text/html?
Daniel
Post by Daniel Watrous
@error(404)
return "Nothing here, sorry: %s" % error.output
Post by Daniel Watrous
In bottle.py, ERROR_PAGE_TEMPLATE is defined and used as default for
all errors.
This can be easily overridden
return 'Nothing here, sorry'
The problem with this is that I can't figure out how to include the
message provided when I called abort. It will simply always print "Nothing
here, sorry"
It will also have a content type of text/html
I'm interested in having the flexibility to return a JSON document.
Daniel
Post by Daniel Watrous
I'm using abort when to return non-successful status to clients. I
would like to override the default HTML output that is generated and
instead have it produce JSON.
Is this possible? How would I go about doing this?
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.
---
You received this message because you are subscribed to the Google
Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/groups/opt_out.
--
This email is intended for the use of the individual addressee(s) named
above and may contain information that is confidential, privileged or
unsuitable for overly sensitive persons with low self-esteem, no sense of
humor or irrational religious beliefs. If you are not the intended
recipient, any dissemination, distribution or copying of this email is not
authorized (either explicitly or implicitly) and constitutes an irritating
social faux pas. Unless the word absquatulation has been used in its
correct context somewhere other than in this warning, it does not have any
legal or grammatical use and may be ignored. No animals were harmed in the
transmission of this email, although the yorkshire terrier next door is
living on borrowed time, let me tell you. Those of you with an overwhelming
fear of the unknown will be gratified to learn that there is no hidden
message revealed by reading this warning backwards, so just ignore that
Alert Notice from Microsoft: However, by pouring a complete circle of salt
around yourself and your computer you can ensure that no harm befalls you
and your pets. If you have received this email in error, please add some
nutmeg and egg whites and place it in a warm oven for 40 minutes. Whisk
briefly and let it stand for 2 hours before icing.
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.
---
You received this message because you are subscribed to the Google
Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/groups/opt_out.
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.

---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...