Skip to content
Snippets Groups Projects
Commit bac3146d authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Added a more friendly default 404 response for the nomad app.

parent fd8e62d2
Branches
Tags
2 merge requests!283Merge v0.10.0 into master for release,!277Added a more friendly default 404 response for the nomad app.
Pipeline #95568 passed
......@@ -34,3 +34,4 @@ parser.osio.log
gui/src/metainfo.json
gui/src/searchQuantities.json
examples/workdir/
*/node_modules/
......@@ -17,8 +17,11 @@
#
from fastapi import FastAPI, status, Response
from fastapi.responses import JSONResponse, HTMLResponse
from fastapi.middleware.wsgi import WSGIMiddleware
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.exceptions import HTTPException as StarletteHTTPException
from fastapi.exception_handlers import http_exception_handler as default_http_exception_handler
from nomad import config, infrastructure
......@@ -70,3 +73,57 @@ async def startup_event():
pass
infrastructure.setup()
@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request, exc):
if exc.status_code != 404:
return await default_http_exception_handler(request, exc)
try:
accept = request.headers['accept']
except Exception:
accept = None
if accept is not None and 'html' in accept:
return HTMLResponse(status_code=404, content=f'''
<html>
<head><title>{config.meta.name}</title></head>
<body>
<h1>NOMAD app</h1>
<h2>info</h2>
{'<br/>'.join(f'{key}: {value}' for key, value in config.meta.items())}
<h2>apis</h2>
<a href="{app_base}/api">NOMAD API v0</a><br/>
<a href="{app_base}/api/v1/extensions/docs">NOMAD API v1</a><br/>
<a href="{app_base}/optimade/v1/extensions/docs">Optimade API</a><br/>
<a href="{app_base}/dcat">DCAT API</a><br/>
</body>
</html>
''')
return JSONResponse(status_code=404, content={
'detail': 'Not found',
'info': {
'app': config.meta,
'apis': {
'v0': {
'root': f'{app_base}/api',
'dashboard': f'{app_base}/api',
},
'v1': {
'root': f'{app_base}/api/v1',
'dashboard': f'{app_base}/api/v1/extensions/docs',
'documentation': f'{app_base}/api/v1/extensions/redoc',
},
'optimade': {
'root': f'{app_base}/optimade/v1',
'dashboard': f'{app_base}/optimade/v1/extensions/docs'
},
'dcat': {
'root': f'{app_base}/dcat',
'dashboard': f'{app_base}/dcat'
}
}
}
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment