Skip to main content Link Menu Expand (external link) Document Search Copy Copied

You can raise an HTTPException inside handlers or middlewares.

Consider the following code:

from tremolo import Tremolo
from tremolo.exceptions import ServiceUnavailable

app = Tremolo()

@app.route('/hello')
async def hello_world(**server):
    raise ServiceUnavailable

    return 'Hello, World!'

if __name__ == '__main__':
    app.run('0.0.0.0', 8000)

A Hello, World! will not be displayed, but rather Service Unavailable as follows (with header):

HTTP/1.1 503 Service Unavailable
Content-Type: text/html; charset=utf-8
Content-Length: 19
Connection: close
Date: Wed, 12 Apr 2023 22:48:40 GMT
Server: Tremolo

Service Unavailable

You can fine-tune the ServiceUnavailable:

raise ServiceUnavailable(
    'Site down for maintenance',
    message='Under Maintenance',
    content_type='text/plain'
)

So it will result as follows:

HTTP/1.1 503 Under Maintenance
Content-Type: text/plain
Content-Length: 25
Connection: close
Date: Wed, 12 Apr 2023 23:01:08 GMT
Server: Tremolo

Site down for maintenance

Custom HTTP Exceptions

You can also create your custom HTTPException:

from tremolo.exceptions import HTTPException

raise HTTPException(
    'Site down for maintenance',
    code=503,
    message='Under Maintenance'
)